问题描述
我有一个JPanel打算充当游戏的HUD,自然地,我重写了paint方法来进行自己的自定义显示,但确实会调用此方法,但是只有在调整大小或最大化,最小化框架时,而不是当我的游戏循环告诉它repaint()时.由于另外两个面板完全重新粉刷,对我来说,这尤其奇怪.
I have a JPanel that is meant to act as a HUD for my game, naturally, I have overridden the paint method to do my own custom display, this does get called, but only upon resizing or maximizing, minimizing the frame, and not when my game loop tells it to repaint(). It seems particularly strange to me on account of my two other panels being repainted completely fine.
这是我的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());
}
}
}
}
它在我的游戏类更新中与其他两个面板一起被调用
It gets called in my Game classes update along with my other two panels
public void update() {
...
display.run();
display2.run();
hud.run();
}
哪个被我的JFrame调用
Which gets called by my 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) {
}
}
}
很长一段时间以来,我一直想弄清楚这个问题,但我不明白,任何帮助都会得到高度重视
Been trying to figure this for a long time now, and I just don't get it, any help would be highly appreaciated
推荐答案
JPanel是轻量级的容器. 为了让孩子画画,您需要调用super.paint (g)在覆盖的paint()方法中.
JPanel is a lightweight container. In order for it's children to get painted, you need to call super.paint(g) in the overridden paint() method.
这篇关于我的覆盖绘画方法没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!