我正在为一个学校项目开发一个游戏,一个类似炸弹人的游戏。
我正在使用 swing,我正在使用 Canvas 绘制图形,但 KeyListener 不起作用,所以我退出使用Canvas
并开始使用paintComponent(Graphics g)
. repaint()
KeyListener 现在正在响应,但是当我的 while 循环调用这些方法时,我的图形不会刷新。
我的代码:
dispose();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
board b = new board();
b.setSize(630, 650);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setVisible(true);
direction dessin = new direction();
b.add(dessin);
b.setVisible(true);
dessin.setBackground(Color.BLACK);
}
});
然后:
package Bm;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import javax.swing.JComponent;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class direction extends JPanel implements Runnable {
static float bmx = 35;
static float bmy = 35;
static float ex = 520;
static float ey = 520;
static float v = 0.03f;
static boolean gauche;
static boolean droite;
static boolean haut;
static boolean bas;
static void movEnnemi() {
int r = 1 + (int) (Math.random() * ((4 - 1) + 1));
Ennemi.droite = false;
Ennemi.gauche = false;
Ennemi.bas = false;
Ennemi.haut = false;
switch (r) {
case 1:
Ennemi.droite = true;
break;
case 2:
Ennemi.gauche = true;
break;
case 3:
Ennemi.bas = true;
break;
case 4:
Ennemi.haut = true;
break;
}
try {
Thread.sleep(5);
} catch (Exception e) {
}
;
}
public direction() {
super();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int ligne = 0; ligne < board.gridHauteur; ligne++) {
for (int colonne = 0; colonne < board.gridLargeur; colonne++) {
switch (board.plateau1[ligne][colonne]) {
case 0:
g.setColor(Color.lightGray);
g.fillRect(30 * ligne, 30 * colonne, 30, 30);
break;
case 1:
g.setColor(Color.black);
g.fillRect(30 * ligne, 30 * colonne, 30, 30);
board.plateau1[ligne][colonne] = board.BLOCKED;
break;
case 2:
g.setColor(Color.darkGray);
g.fillRect(30 * ligne, 30 * colonne, 30, 30);
board.plateau1[ligne][colonne] = board.BLOCKED;
break;
}
}
}
g.setColor(Color.blue);
g.fillRect((int) ex, (int) ey, 20, 20);
g.setColor(Color.red);
g.fillRect((int) bmx, (int) bmy, 21, 21);
g.dispose();
}
public void run() {
long dernierTempsLoop = System.currentTimeMillis();
while (true) {
long delta = (System.currentTimeMillis() - dernierTempsLoop);
dernierTempsLoop = System.currentTimeMillis();
movEnnemi();
for (int i = 0; i < delta / 5; i++) {
logic(5);
Ennemi.logic(5);
}
if ((delta % 5) != 0) {
logic(delta % 5);
Ennemi.logic(delta % 5);
}
System.out.println((int) (bmx / 30) + " - " + (int) (bmy / 30));
try {
Thread.sleep(20);
} catch (Exception e) {
}
;
repaint(); // <== HERE
}
}
public static void logic(long delta) {
float dx = 0;
float dy = 0;
if (gauche) {
dx--;
}
if (droite) {
dx++;
}
if (haut) {
dy--;
}
if (bas) {
dy++;
}
if ((dx != 0) || (dy != 0)) {
joueur.mouvement(dx * delta * v, dy * delta * v);
if (joueur.mouvement((dx * delta * v), (dy * delta * v)) == false) {
if (joueur.mouvement(0, dy * delta * v)) {
joueur.mouvement(0, dy * delta * v);
}
if (joueur.mouvement(dx * delta * v, 0)) {
joueur.mouvement(dx * delta * v, 0);
}
}
}
}
}
和 :
package Bm;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import javax.swing.*;
@SuppressWarnings("serial")
class board extends JFrame implements KeyListener {
static JPanel p;
public Timer fpstimer;
public direction g;
static final int BLOCKED = 1;
static int gridLargeur = 21;
static int gridHauteur = 21;
int fenLargeur = 630;
int fenHauteur = 650;
public static int plateau1[][] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
public board() {
super("Bomberman");
g = new direction();
addKeyListener(this);
threadLoop p = new threadLoop("loop");
p.start();
}
public static boolean blocked(double d, double e) {
return plateau1[(int) d][(int) e] == BLOCKED;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
direction.gauche = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
direction.droite = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
direction.bas = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
direction.haut = true;
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
direction.gauche = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
direction.droite = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
direction.bas = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
direction.haut = false;
}
}
public void keyTyped(KeyEvent e) {
}
}
class threadLoop extends Thread {
public direction g;
threadLoop(String name) {
super(name);
}
public void run() {
g = new direction();
g.run();
}
}
public class Jouer {
}
我希望你能理解我的问题,并能帮助我,谢谢:)