gpt4 book ai didi

Java PaintComponents 没有被调用

转载 作者:行者123 更新时间:2023-12-01 21:28:08 27 4
gpt4 key购买 nike

我正在尝试在 JPanel 上绘制一些内容,但似乎我遇到了 paintpaintComponent 方法的问题。可能与双缓冲有关,但我不确定。 public void PaintComponent(Graphics g) 方法由于某种原因没有被调用,知道为什么吗?

这是我的代码:

   @Override
public void paintComponents(Graphics g) {
super.paintComponents(g); //To change body of generated methods, choose Tools | Templates.
System.out.println("paintComponents!");

snakeHead.DrawSphere(g);

if(foodShoulBeRedrawn){
foodShoulBeRedrawn = false;
spawnFood();
}

if(shouldSpawnBodyPart){
shouldSpawnBodyPart = false;
snake.get(snake.size() - 1).DrawSphere(g);

}

//spawnSnake();
paintCalled = true;

repaint();
}

/*
@Override
public void paint(Graphics g) {
super.paint(g);

snakeHead.DrawSphere(g);

if(foodShoulBeRedrawn){
foodShoulBeRedrawn = false;
spawnFood();
}

if(shouldSpawnBodyPart){
shouldSpawnBodyPart = false;
snake.get(snake.size() - 1).DrawSphere(g);

}

//spawnSnake();
paintCalled = true;

repaint();
}
*/

最佳答案

你正在超越Container#paintComponents() ,它由您的 JFrame 继承。您不需要扩展 JFrame,而是需要扩展 JPanel因此您可以重写其 paintComponent()getPreferredSize()。然后您可以将面板add()添加到JFrame中,就像它们显示的here一样。和 here .

关于Java PaintComponents 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37789496/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com