My overriden paint method is not getting called

若如初见. 提交于 2019-12-04 02:27:43

问题


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.

Here is my HUD class:

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();
}

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


回答1:


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.




回答2:


Do not override the paint method in Swing.

Override paintComponent instead.

Then you don't have to call paint or repaint for your subcomponents - this will be done automatically.




回答3:


Alright guys, thanks for your help, in the end it required a combination of all your solutions to get it to work, what surprised me most was that it needed to be a JComponent for it to repaint properly. Here's the code that works

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);

            }
        }
    }
}

Cheers dudes!



来源:https://stackoverflow.com/questions/6818443/my-overriden-paint-method-is-not-getting-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!