Jpanel Repaint Issue

Java JPanel repaint() issue?

Make sure you are calling super.paintComponent to prepare the Graphics context for painting

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g)
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
g.setColor(new Color(red, blue, green));
g.fillOval(20, 30, 50, 50);
}

The long and short of it is the Graphics context is a shared resource. Every component painted during a paint cycle will share the same Graphics context, mean that what ever was previously painted to it will still be there. You need to clear/prepare the Graphics context each time paintComponent is called.

This is essentially what paintComponent does...

repaint() not updating JPanel

All your buildCenter() method does is create an empty panel with a black background.

You then add this empty panel to the frame:

panel.add(buildCenter(), BorderLayout.CENTER);

Then in your ActionListener you do:

buildCenter().add(fire);
buildCenter().repaint();
buildCenter().validate();

All this does is create 3 more empty panels. You don't want to create 3 more panels. You want to add components to the existing panel.

What you need to do is create a single instance of your "center" panel and then keep a variable that references this panel so you can update the panel in the future.

So you need an instance variable to be defined in your class:

private JPanel centerPanel;

Then in the buildGui() method you create the panel:

//panel.add(buildCenter(), BorderLayout.CENTER);
centerPanel = buildCenter();
panel.add(buildCenter, BorderLayout.CENTER);

Then in your ActionListener you can add components to the center panel:

//buildCenter().add(fire);
//buildCenter().repaint();
//buildCenter().validate();
centerPanel.add( fire );
centerPanel.revalidate();
centerPanel.repaint();

repaint() Method Not Repainting JPanel

I think the problem is that you create a Content class and then add it to the frame.

However, in you MouseListener code you invoke the getInstance() method of your Content class and the "instance" variable is null so a new Content instance is created (but never added to the frame so it will never get painted).

So this basic logic is wrong you don't want to be creating a new instance.

Instead in the constructor of your Content class you would just do:

instance = this;

Then the getInstance() method will just return the variable because it will always have a value.

Also, the MouseListeners should be inner classes in of your Content class. That is you should create and add the listener to your class. When you do this then you don't even need the static getInstance() method because you can just access the instance variable directly.

but they only say to try using invalidate() and validate() which had no effect for me.

You should not use those methods. Those are used in AWT. In Swing you might use:

revalidate();
repaint();

when you add components to the GUI AFTER the GUI is visible or when you change a property in your custom class. The revalidate() will invoke the layout manager. The repaint() will paint the component. In your class you are not changing the size of anything so you only need repaint() in your setter methods.

Edit:

Mind sharing the code you added?

I added one line of code:

    public Content (){
int yLevel = (((int)Math.random())*(getHeight() / 4));
int xPos = ((int)Math.random())*getWidth();
for(int i = 0;!(i == 9); i++) {
//Create the randomly located targets
targets[i] = new Target(xPos, yLevel, xPos-15, yLevel-10);
}

instance = this; // added
}

Make one change at a time. Once you verified my assumption is valid by adding the single line of code. Then you can tidy up the rest of the class. If you make multiple changes at once you don't know which one is causing the problem.



Related Topics



Leave a reply



Submit