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

时间:2011-07-25 15:26:29

标签: java multithreading swing paint

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

这是我的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 classes更新中调用它以及我的其他两个面板

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) {
        }
    }
}

现在已经试图解决这个问题很长一段时间了,我只是没有得到它,任何帮助都会被高度评价

3 个答案:

答案 0 :(得分:4)

请勿覆盖Swing中的paint方法。

改为覆盖paintComponent

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

答案 1 :(得分:1)

答案 2 :(得分:-1)

好的家伙,感谢您的帮助,最终需要将所有解决方案组合起来才能让它发挥作用,最令我惊讶的是它需要成为一个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);

            }
        }
    }
}

干杯老兄!