0

I'd like to know how you are supposed to override a paint method for each panel in the same class and how to call them separately?

I only know about the repaint() call when you are in a class that extends JPanel (so in only one panel), not when you just make panels.

Thanks in advance.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
zuokuok
  • 323
  • 1
  • 6
  • 16
  • *"same class and how to call them separately?"* - Generally, you don't. You can use an instance of the object and call it's `repaint` method to schedule an update, or you can invalidate the container the components are on which might force a repaint of the child components. You question is vague and lacks context. Perhaps you would like to enlighten us with what you are trying to achieve in order to improve the quality of the potential answers... – MadProgrammer Dec 17 '14 at 23:40
  • You could also take a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details about the paint process – MadProgrammer Dec 17 '14 at 23:40
  • Thanks for the fast answer. Basically I have a JFrame in which I add two panels. One panel has to be repainted every time a click has been spotted in it, and the other has some information that need to be updated every time a move is made, and also a timer that is updated every second. Since the first panel has a lot of images and the other one doesn't, I want to avoid it to flicker because of the first one. I just thought, though... wouldn't adding these pannels to another one and overriding its paintComponent method solve the flickering problem, since it repaints both my panels separately? – zuokuok Dec 17 '14 at 23:54
  • Flickering will occur because you're doing something wrong (Swing components are double buffered by default). Components can be painted independently of each other and their parents, so adding it to another panel won't really fix the problem. Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates the code that paints the images (and which flickers) – MadProgrammer Dec 17 '14 at 23:58

2 Answers2

1

Typically you create a class that extends JPanel to override the paintComponent method:

public class Test extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // code here
    }

    public void doStuff() { repaint(); }
}

You might consider creating a nested class like so:

public class Test {
    public class MyPanel extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // code here
        }
    }
    JPanel panel = new MyPanel();
    panel.repaint();
}

Or you can do this without creating a class that extends JPanel:

JPanel panel1 = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // code here
    }
};

panel1.repaint();
TNT
  • 2,900
  • 3
  • 23
  • 34
  • Thanks for the answer, this is exactly what I was looking for. As I wrote above :"wouldn't adding these pannels to another one and overriding its paintComponent method solve the flickering problem, since it repaints both my panels separately?" – zuokuok Dec 17 '14 at 23:59
0

I think the normal thing is to extend JPanel for each unique panel you wish to create. In other words, each panel you create is its own class. Then you can overwrite the paint method for each individually.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41