public class GamePanel extends JPanel {
public Snake snake;
public Wall wall;
public Food food; public void display(Snake snake,Wall wall,Food food) {
this.snake = snake;
this.wall = wall;
this.food = food;
this.repaint();
System.out.println("over");
}
@Override
public void paint(Graphics g) {
System.out.println("draw");
this.snake.drawBody(g);
this.wall.drawWall(g);
this.food.drawFood(g);
}
}
我的代码如上,但是over能打印出来,但是paint()方法没有被调用,一次都没有?
什么地方有问题吗? 哪位大哥知道?
public Snake snake;
public Wall wall;
public Food food; public void display(Snake snake,Wall wall,Food food) {
this.snake = snake;
this.wall = wall;
this.food = food;
this.repaint();
System.out.println("over");
}
@Override
public void paint(Graphics g) {
System.out.println("draw");
this.snake.drawBody(g);
this.wall.drawWall(g);
this.food.drawFood(g);
}
}
我的代码如上,但是over能打印出来,但是paint()方法没有被调用,一次都没有?
什么地方有问题吗? 哪位大哥知道?
看下有帮助没
由 Swing 调用,以绘制组件。应用程序不应直接调用 paint,而是应该使用 repaint 方法来安排重绘组件。
此方法实际上将绘制工作委托给三个受保护的方法:paintComponent、paintBorder 和 paintChildren。按列出的顺序调用这些方法,以确保子组件出现在组件本身的顶部。一般来说,不应在分配给边框的 insets 区域绘制组件及其子组件。子类可以始终重写此方法。只想特殊化 UI(外观)委托的 paint 方法的子类只需重写 paintComponent。 摘自java api 文档