Tadas
Tadas

Reputation: 89

Repaint function

I need some help with repaint() function. When I run the program it's makes a image flashing effect. What's wrong?

public class Game extends JFrame {
    private static final long serialVersionUID = 1L;

    public Game() {
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(Toolkit.getDefaultToolkit().getImage("Images/bg.jpg"),0,0,this);
        g.drawImage(Toolkit.getDefaultToolkit().getImage("Images/player.png"),0,448,this);
        repaint();
    }

    public static void main(String[] args) {
        Game langas = new Game();
        langas.setSize(900,550);
        langas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        langas.setTitle("Best Game EVER! LOL");
        langas.setVisible(true);
        langas.setLocationRelativeTo(null);
        langas.setResizable(false);
    }
}

Sorry for my bad English and sorry if my code look stupid I am beginner.

Upvotes: 0

Views: 142

Answers (1)

blueygh2
blueygh2

Reputation: 1538

You wrote an endless recursion, because:

repaint calls paint, which calls repaint, which calls paint,...

You're caught in a repaint loop, therefore the image is flickering.

Upvotes: 2

Related Questions