Java 如何在一个类中编辑JPanel图形?
因此,我试图在JFrame上编写snake,并在JPanel上执行所有图形化操作(移动“蛇”、随机食物生成等)。我正处于开始阶段,所以我现在要做的就是用箭头键在我的框架上移动一个黑色的正方形。Panel类中的while循环不会被Snake类中的按键打断,那么有没有办法用我的所有其他代码编辑同一个类中的JPanel图形呢 这是所有的代码。底部的Panel类遵循我在这里找到的模板Java 如何在一个类中编辑JPanel图形?,java,swing,Java,Swing,因此,我试图在JFrame上编写snake,并在JPanel上执行所有图形化操作(移动“蛇”、随机食物生成等)。我正处于开始阶段,所以我现在要做的就是用箭头键在我的框架上移动一个黑色的正方形。Panel类中的while循环不会被Snake类中的按键打断,那么有没有办法用我的所有其他代码编辑同一个类中的JPanel图形呢 这是所有的代码。底部的Panel类遵循我在这里找到的模板 public class Snake { // panel width and height stat
public class Snake {
// panel width and height
static int pW;
static int pH;
static int x = 10;
static int y = 10;
static int k;
static JFrame frame = new JFrame("SNAKE");
// getters for panel class
public int getPW() { return pW; }
public int getPH() { return pH; }
public int getX() { return x; }
public int getY() { return y; }
public static void main(String[] args) {
// get screen dimensions
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int sH = (int) screenSize.getHeight();
int sW = (int) screenSize.getWidth();
pW = (int) sW/2;
pH = (int) sH/2;
// initialize frame
frame.setSize (pW/1,pH/1);
frame.setLocation(pW/2,pH/2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener( new KeyAdapter() {
public void keyPressed(KeyEvent e) {
k = e.getKeyCode();
switch(k) {
case 38: /* y -= square size */ break; // up
case 40: /* y += square size */ break; // down
case 37: /* x -= square size */ break; // left
case 39: /* x += square size */ break; // right
case 27: System.exit(0);
}
}
});
Panel panel = new Panel();
frame.add(panel);
frame.setVisible(true);
}
}
class Panel extends JPanel {
Snake snake = new Snake();
//square size and separation between squares
int sep = 0;
int size = 50;
// initial location of square on the panel/frame
int x = sep + size;
int y = sep + size;
// holding values to check if x or y have changed
int xH = x;
int yH = x;
public void paint(Graphics g) {
int pW = snake.getPW();
int pH = snake.getPH();
int i; int o;
Color on = Color.BLACK;
Color off = Color.GRAY;
// gray background
g.setColor(Color.GRAY);
g.fillRect(0,0,pW,pH);
// black square initialization
g.setColor(Color.BLACK);
g.fillRect(x, y, size, size);
/* this loop is supposed to check if the black
* rectangle has moved by repeatedly grabbing x & y
* values from the Snake class. When a key is pressed
* and the values change, a gray rectangle is placed at the old location
* and a black one is placed at the new location.
*
* When I run the program, I get stuck in this while loop.
* If I had the while loop in the same class I check for keys,
* I don't think I would have this problem
*/
while(true) {
x = snake.getX();
y = snake.getY();
if(x != xH || y != yH) {
g.setColor(off);
g.fillRect(xH, yH, size, size);
g.setColor(on);
g.fillRect(snake.getX(), snake.getY(), size, size);
xH = x;
yH = y;
}}
}
}
在绘制方法中不应该有
while(true)
循环。这只会导致无限循环,GUI将无法响应事件
相反,您需要向snake类添加方法来移动蛇。因此,当按下其中一个箭头键时,您将更新蛇的起始位置。然后,该方法将调用repaint(),当Swing调用paintComponent()方法时,snake将重新绘制自身
因此,绘制代码应该覆盖paintComponent()
not paint(),并且应该调用super.paintComponent(g)
作为方法中的第一条语句
不要将自定义类称为“Panel”,因为有一个AWT类具有该名称。使您的类名更具描述性