所以我的项目是创建一个射击类型的游戏,我希望当我按下箭头时,我的方块(这将是宇宙飞船之后)改变x;y位置。我们设法测试了keylistener是否正常工作,因此我们确定问题来自重绘功能。
请帮忙,这是我们的代码:
等级 n°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);}
}
}
等级 n°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();
您正在创建在 keyPressed
方法中修改的Panneau
实例。但是随着
this.setContentPane(new Panneau());
您正在向帧添加 Panneau
的新实例 - keyPressed
方法中不会修改此实例。
代码还有其他一些问题,但实现所需效果所需的最小更改是将此行更改为
this.setContentPane(this.pan);
为了清除面板的背景(即删除先前绘制的矩形),您还必须在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é ");
}