У меня есть JPanel, который должен выступать в качестве HUD для моей игры, естественно, я переопределил метод рисования, чтобы сделать свое собственное отображение, это вызывается, но только после изменения размера или увеличения, минимизации фрейма а не когда мой игровой цикл говорит ему перерисовать (). Это кажется мне особенно странным из-за того, что мои две другие панели были полностью перекрашены.
Вот мой класс 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());
}
}
}
}
Он вызывается в моем обновлении классов игры вместе с двумя другими моими панелями
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) {
}
}
}
Давно пытаюсь понять это, а я просто не понимаю, любая помощь будет чрезвычайно оценена