简体   繁体   中英

repaint only working after resize

I want to let a ball bounce in a screen with the following code. The problem is that it only moves when I Resize the frame. So i have my paintcomponent in my panel method. While my thread is running i run a loop where i move the ball, sleep the thread and repaint. The ball is only moving when i resize the frame. Can anyone help me?

public class SpelPaneel extends JPanel {

    private JLabel spelLabel;
    private JPanel spelPaneel;
    private Image background;
    private Bal bal;

    public SpelPaneel() {
        spelPaneel = new JPanel();
        spelLabel = new JLabel("spel");
        add(spelLabel);
        try {
            background = ImageIO.read(new File("src/Main/images/background2.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        bal = new Bal(spelPaneel, 50, 50, 15);
        bal.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(background, 0, 0, getWidth(), getHeight(), null);
        bal.teken(g, Color.red);
    }
}

class Bal extends Thread {

    private JPanel paneel;
    private int x, y, grootte;
    private int dx, dy;
    private boolean doorgaan;

    public Bal(JPanel paneel, int x, int y, int grootte) {
        this.paneel = paneel;
        this.grootte = grootte;
        this.x = x;
        this.y = y;
        dy = 2;
        doorgaan = true;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void run() {
        while (doorgaan) {
            paneel.repaint();
            slaap(10);
            verplaats();
        }
    }

    public void teken(Graphics g, Color kleur) {
        g.setColor(kleur);
        g.fillOval(x, y, 15, 15);
    }

    public void verplaats() {
        if (x > 335 || x < 50) {
            dx = -dx;
        }
        if (y > 235 || y < 50) {
            dy = -dy;
        }

        x += dx;
        y += dy;
        setX(x);
        setY(y);
    }

    private void slaap(int millisec) {
        try {
            Thread.sleep(millisec);

        } catch (InterruptedException e) {
        }
    }
}
spelPaneel = new JPanel(); //

Your SpelPaneel class extends JPanel so there is no need to create another panel. The above line of code just creates a JPanel in memory but does nothing with it.

bal = new Bal(spelPaneel, 50, 50, 15);

Then you create your Bal Thread and pass this dummy panel to it and later attempt to do a repaint on this dummy panel.

Intead I would guess the code should be:

bal = new Bal(this, 50, 50, 15);

since "this" refers to the actual instance of the SeplPaneel that you created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM