简体   繁体   中英

Repaint() not calling paintComponent() Java

I'm trying to remake Snake to improve my programming, Ive been reading about other people having the same problem for hours but none of them seem to be similar to my own code. Here is my relevant code:

package snake;

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PlayGame extends JPanel implements Runnable{
    private JFrame jfr;
    private JPanel jpn;

    PickupBall b = new PickupBall();
    Snake bob = new Snake();

    public PlayGame(){
        jfr = new JFrame("Snake");            
        jfr.setSize(640,640);
        jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jpn = new JPanel();
        jpn.setBackground(Color.WHITE);

        jfr.add(jpn);            
        jfr.setVisible(true);             
    }

    @Override
    public void run(){            
        while(true){                
            repaint();
        }
    }        

    @Override
    public void paintComponent(Graphics g){            
        super.paintComponent(g);            
        b.draw(g);
    }        

    public static void main(String[] args) {            
        PlayGame p = new PlayGame();  
        Thread t = new Thread(p);

        t.start();           

    }


 }

Everything works, the while(true) loop is initiated, but nothing happens.

package snake;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class PickupBall {
//constructor
public PickupBall(){
    xPos = (int) (((Math.random()*31)+1)*20);
    yPos = (int) (((Math.random()*31)+1)*20);        
}
//constants
public final int balletjeSides = 20;

//variables
public int xPos;
public int yPos;
//methods
public void draw(Graphics g){
    g.setColor(Color.RED);
    g.fillRoundRect(this.xPos, this.yPos, balletjeSides, balletjeSides, 5, 5);
}

}

What am I missing?

just add jfr.setContentPane(this); within your onstructor , as what @Berger said, you actually have a JPanel , PlayGame instance here, but the JFrame you´re creating doesn´t use this JPanel instance, but instead simply adds an empty JPanel , called jpn in your code, to your jfr which doesn´t have any content.

The new constructor could look like this:

private JFrame jfr;
//private JPanel jpn;

PickupBall b = new PickupBall();
//Snake bob = new Snake();

public PlayGame() {
    jfr = new JFrame("Snake");
    jfr.setSize(640, 640);
    jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //jpn = new JPanel();
    this.setBackground(Color.WHITE);

    //jfr.add(jpn);
    jfr.setContentPane(this);
    jfr.setVisible(true);
}

As you see, i commented out the jpn variable, as the actual JPanel you´d like to work with (my guess) is the PlayGame class itself, and not an JPanel defined within this class.

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