所以我的项目是创建一个射击游戏类型的游戏,我希望当我按下箭头时,我的方块(将成为太空飞船)改变x; y位置。
我们设法测试了关键监听器是否正常工作,因此我们确定问题出在重绘功能上。

请帮助,这是我们的代码:

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);}
}





 }


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;
  }

}


而我们的主要:

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:我们是初学者,所以我们很可能做了很多错误的事情

最佳答案



Panneau pan = new Panneau();


您正在创建在Panneau方法中修改的keyPressed实例。但是随着

this.setContentPane(new Panneau());


您要在框架中添加Panneau的新实例-在keyPressed方法中不会修改此实例。

该代码还有其他一些问题,但是为达到预期效果所必需的最小更改是将这一行更改为

this.setContentPane(this.pan);


为了清除面板的背景(即删除先前绘制的矩形),您还必须在super.paintComponent(g)方法的第一行中调用paintComponent

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

10-06 13:51