我的覆盖绘制方法没有被调用

我有一个JPanel,可以作为我游戏的HUD,当然,我已经覆盖了paint方法来做我自己的自定义显示,这确实被调用,但只有在resize或最大化时,最小化框架,而不是在我的游戏循环告诉它重绘()。 由于我的另外两个面板被重新绘制得很好,我觉得特别奇怪。

这是我的HUD课程:

package base; import java.awt.Color; import java.awt.Graphics; import javax.swing.BoxLayout; import javax.swing.JPanel; public class HUD extends JPanel { private Shiphud[] shiphuds; public HUD(Ship[] ships) { shiphuds = new Shiphud[ships.length]; this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); for (int i = 0; i < shiphuds.length; i++) { shiphuds[i] = new Shiphud(ships[i]); this.add(shiphuds[i]); } } @Override public void paint(Graphics g) { for (int i = 0; i < shiphuds.length; i++) { shiphuds[i].repaint(); } } public void run() { repaint(); } private class Shiphud extends JPanel { private Ship ship; public Shiphud(Ship ship) { this.ship = ship; } @Override public void paint(Graphics g) { if (ship != null) { g.setColor(Color.BLACK); g.fillRect(0, 0, this.getWidth(), this.getHeight()); int fullbar = (int) (this.getWidth() * 0.8); g.setColor(Color.GREEN); g.fillRoundRect(getWidth() / 10, getHeight() / 10, (int) ((ship.energy / 1000) * fullbar), getHeight() / 6, 10, 10); g.setColor(Color.blue); g.fillRoundRect(getWidth() / 10, (int) (getHeight() * 0.4), (int) ((ship.fuel / 1000) * fullbar), getHeight() / 6, 10, 10); g.setColor(Color.YELLOW); g.fillRoundRect(getWidth() / 10, (int) (getHeight() * 0.6), (int) ((ship.ammo / 1000) * fullbar), getHeight() / 6, 10, 10); g.setColor(Color.MAGENTA); g.fillRoundRect(getWidth() / 10, (int) (getHeight() * 0.8), (int) ((ship.special / 1000) * fullbar), getHeight() / 6, 10, 10); System.out.println("here" + System.currentTimeMillis()); } } } } 

它在我的Game类更新中与我的其他两个面板一起被调用

 public void update() { ... display.run(); display2.run(); hud.run(); } 

我的JFrame会调用它

 public void runFrame() { Thread thread = new Thread() { @Override public void run() { gameLoop(); } }; thread.start(); } public void gameLoop() { while (true) { long beginTime = System.nanoTime(); game.update(); long timeTaken = System.nanoTime() - beginTime; long timeLeft = (UPDATE_PERIOD - timeTaken) / 1000000; // in milliseconds if (timeLeft < 10) { timeLeft = 10; // set a minimum } try { // Provides the necessary delay and also yields control so that other thread can do work. Thread.sleep(timeLeft); } catch (InterruptedException ex) { } } } 

一直试图解决这个问题很长一段时间,我只是不明白,任何帮助都会被高度评价

JPanel是一个轻量级容器。 为了让它的孩子得到绘画,你需要在重写的paint()方法中调用super.paint(g)。

不要覆盖Swing中的paint方法。

改为覆盖paintComponent

然后,您不必为子组件调用paintrepaint – 这将自动完成。

好吧,伙计们,感谢您的帮助,最终需要将所有解决方案组合起来才能让它发挥作用,最令我惊讶的是它需要成为一个JComponent才能正确地重新绘制。 这是有效的代码

 package base; import java.awt.Color; import java.awt.Graphics; import javax.swing.BoxLayout; import javax.swing.JComponent; import javax.swing.JPanel; public class HUD extends JPanel { private Shiphud[] shiphuds; public HUD(Ship[] ships) { shiphuds = new Shiphud[ships.length]; this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); for (int i = 0; i < shiphuds.length; i++) { shiphuds[i] = new Shiphud(ships[i]); this.add(shiphuds[i]); } } @Override public void paint(Graphics g) { super.paint(g); for (int i = 0; i < shiphuds.length; i++) { shiphuds[i].repaint(); } } public void run() { repaint(); } private class Shiphud extends JComponent { private Ship ship; public Shiphud(Ship ship) { this.ship = ship; } @Override public void paintComponent(Graphics g) { if (ship != null) { g.setColor(Color.BLACK); g.fillRect(0, 0, this.getWidth(), this.getHeight()); int fullbar = (int) (this.getWidth() * 0.8); g.setColor(Color.GREEN); g.fillRoundRect(getWidth() / 10, getHeight() / 10, (int) ((ship.energy / 1000) * fullbar), getHeight() / 6, 10, 10); g.setColor(Color.blue); g.fillRoundRect(getWidth() / 10, (int) (getHeight() * 0.4), (int) ((ship.fuel / 1000) * fullbar), getHeight() / 6, 10, 10); g.setColor(Color.YELLOW); g.fillRoundRect(getWidth() / 10, (int) (getHeight() * 0.6), (int) ((ship.ammo / 1000) * fullbar), getHeight() / 6, 10, 10); g.setColor(Color.MAGENTA); g.fillRoundRect(getWidth() / 10, (int) (getHeight() * 0.8), (int) ((ship.special / 1000) * fullbar), getHeight() / 6, 10, 10); } } } } 

干杯老兄!