简体   繁体   English

使用Keylistener后如何重绘图形?

[英]How to repaint graphics after using a Keylistener?

So my project is to create a shoot'em up type of game and i'd like that when i press arrows my square (that will after be the spaceship) change x;y position. 所以我的项目是创建一个射击游戏类型的游戏,我希望当我按下箭头时,我的方块(将成为太空飞船)改变x; y位置。 We managed to have a test that the keylistener works so we're sure that the problem comes from the repaint function. 我们设法测试了关键监听器是否正常工作,因此我们确定问题来自重绘功能。

Pls help, here's our code : 请帮助,这是我们的代码:

Class n°1 1级

import java.awt.Color; 


import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Dimension; 

import javax.swing.JFrame;

public class Fenetre extends JFrame implements KeyListener {
Panneau pan = new Panneau();
  public Fenetre(){             
    //nom de la fenetre
    this.setTitle("Projet");
    //taille
    this.setSize(450, 600);
    //centrer la fenetre
    this.setLocationRelativeTo(null);
    //Fenetre toujours en premier plan
    this.setAlwaysOnTop(true);
    //arrêt du processus à la fermeture de la fenêtre
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Fenetre non redimensionnable
    this.setResizable(false);
    //Couleur du fond
    this.setBackground(Color.DARK_GRAY);   

    //Instanciation d'un objet JPanel
    JPanel pan = new JPanel();
    JPanel tir = new JPanel();
    //On prévient notre JFrame que notre JPanel sera son content pane
    this.setContentPane(new Panneau());

    this.setVisible(true);
    this.addKeyListener(this);
  }
@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}


@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    int c = e.getKeyCode();
        if(c==KeyEvent.VK_RIGHT){
            int x = pan.getPosX();
            x=x+10;
            pan.setPosX(x);
            pan.repaint();
            System.out.print("Droite ");}
        if(c==KeyEvent.VK_LEFT){
            int x = pan.getPosX();
            x=x-10;
            pan.setPosX(x);
            pan.repaint();
            System.out.print("Gauche ");}
        if(c==KeyEvent.VK_UP){
            int y = pan.getPosY();
            y=y+10;
            pan.setPosY(y);
            pan.repaint();
            System.out.print("Haut ");}
        if(c==KeyEvent.VK_DOWN){
            int y = pan.getPosY();
            y=y-10;
            pan.setPosY(y);
            pan.repaint();
            System.out.print("Bas ");}
        if(c==KeyEvent.VK_SPACE){
             this.setBackground(Color.CYAN);



        }


}
@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    int c = e.getKeyCode();
    if(c==KeyEvent.VK_SPACE){
         this.setBackground(Color.DARK_GRAY);}
}





 }

Class n°2 2级

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import java.awt.Graphics;

import javax.swing.JPanel;

import java.awt.Graphics;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class Panneau extends JPanel {

  public int posX = 450/2-15;
  public int posY = 500;

public void paintComponent(Graphics g){
    g.setColor(Color.red);
    g.fillRect(posX, posY, 30, 30);
    System.out.print("Carré ");
 }
  public int getPosX() {
    return posX;
  }
  public void setPosX(int posX) {
    this.posX = posX;
  }
  public int getPosY() {
    return posY;
  }
  public void setPosY(int posY) {
    this.posY = posY;
  }        

}

And our main : 而我们的主要:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;


public class Main extends JFrame {
public static void main (String [] Args){
    Fenetre fen = new Fenetre();


}}

PS: We're beginners so we might well have done a awful lots of things wrong PS:我们是初学者,所以我们很可能做了很多错误的事情

With

Panneau pan = new Panneau();

you are creating the instance of Panneau which is modified in the keyPressed method. 您正在创建在keyPressed方法中修改的Panneau实例。 But with 但是随着

this.setContentPane(new Panneau());

you are adding a new instance of Panneau to the frame - this instance is not modified in the keyPressed method. 您要在框架中添加Panneau实例-在keyPressed方法中不会修改此实例。

There are some other issues with the code, but the minimal change that is necessary to achieve the desired effect is to change this line to 该代码还有其他一些问题,但是为达到预期效果所必需的最小更改是将这一行更改为

this.setContentPane(this.pan);

In order to clear the background of the panel (that is, remove the previously painted rectangle) you also have to call super.paintComponent(g) in the first line of your paintComponent method: 为了清除面板的背景(即删除先前绘制的矩形),您还必须在paintComponent方法的第一行中调用super.paintComponent(g)

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.red);
    g.fillRect(posX, posY, 30, 30);
    System.out.print("Carré ");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM