Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,305 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 780 people online right now. Registration is fast and FREE... Join Now!




What does the super.paintComponent(g) do?

 
Reply to this topicStart new topic

What does the super.paintComponent(g) do?

bastones
11 Jan, 2009 - 12:07 PM
Post #1

New D.I.C Head
*

Joined: 3 Jan, 2009
Posts: 22

When I am extending the JPanel I have to super.paintComponent(g) - I know that it is required because a panel like JPanel is opaque but what is it really used for? I don't understand that the code itself paints the panel with the background colour - how does it lose it in the first place? Is there a method in JComponent that the JPanel inherits from JComponent class that makes the JPanel opaque but loses it? If this is true does it lose it as we're using the paintComponent class or as we're extending the JPanel?

Any help is appreciated...

Cheers

User is offlineProfile CardPM
+Quote Post


pbl
RE: What Does The Super.paintComponent(g) Do?
11 Jan, 2009 - 12:32 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,954



Thanked: 673 times
Dream Kudos: 200
My Contributions
QUOTE(bastones @ 11 Jan, 2009 - 12:07 PM) *

When I am extending the JPanel I have to super.paintComponent(g) - I know that it is required because a panel like JPanel is opaque but what is it really used for? I don't understand that the code itself paints the panel with the background colour - how does it lose it in the first place? Is there a method in JComponent that the JPanel inherits from JComponent class that makes the JPanel opaque but loses it? If this is true does it lose it as we're using the paintComponent class or as we're extending the JPanel?

Any help is appreciated...

Cheers


You do not always have to call super.paint(g)
you only have to do it when you draw something.

Let's have an example.

CODE

class MyPanel extends JPanel {
    MyPanel() {
       add(new JLabel("Hello");
       add(new JButton("OK");
       ... addd other JComponent
    }
}

In that case you do not have to overload paint. Panel has a method call paint() that "knows" how to draw JLabel and JButton

Now if you draw something like Oval and Rectangle then you have to overload paint

CODE

class MyPanel extends JPanel {
    MyPanel() {
       add(new JLabel("Hello");
       add(new JButton("OK");
       ... addd other JComponent
    }
    // I am overloading piant to do my own stuff
    public void paint(Graphics g) {
         // now that I have overload paint... do I know how to draw the JLabel and JButton ?
         // obviously not. So let the super method do the job
         super.paint(g);
         // now that the JLabel and JButton are paint les me do my own drawing/animation
         g.drawOval(......
    }
}


So you have to overload and call super if you have "standard" components (that you don't know how to paint) and other

Now if your code is 100% your own drawing... you do not need to call super, just do your drawing

CODE

class MyPanel extends JPanel {
    MyPanel() {
    }
    // I am overloading paint to do my own stuff there are no "standard" GUI elements to draw
    public void paint(Graphics g) {
         // now that the JLabel and JButton are paint les me do my own drawing/animation
         g.drawOval(......
    }
}


Hope this help
User is online!Profile CardPM
+Quote Post

bastones
RE: What Does The Super.paintComponent(g) Do?
11 Jan, 2009 - 12:39 PM
Post #3

New D.I.C Head
*

Joined: 3 Jan, 2009
Posts: 22

Hi, thanks for your reply. So am I right in saying that when I use the paintComponent method it is overloading specific methods that make panels (like JPanel) opaque? I understand that super invokes the methods we've overloaded...

QUOTE(pbl @ 11 Jan, 2009 - 12:32 PM) *

QUOTE(bastones @ 11 Jan, 2009 - 12:07 PM) *

When I am extending the JPanel I have to super.paintComponent(g) - I know that it is required because a panel like JPanel is opaque but what is it really used for? I don't understand that the code itself paints the panel with the background colour - how does it lose it in the first place? Is there a method in JComponent that the JPanel inherits from JComponent class that makes the JPanel opaque but loses it? If this is true does it lose it as we're using the paintComponent class or as we're extending the JPanel?

Any help is appreciated...

Cheers


You do not always have to call super.paint(g)
you only have to do it when you draw something.

Let's have an example.

CODE

class MyPanel extends JPanel {
    MyPanel() {
       add(new JLabel("Hello");
       add(new JButton("OK");
       ... addd other JComponent
    }
}

In that case you do not have to overload paint. Panel has a method call paint() that "knows" how to draw JLabel and JButton

Now if you draw something like Oval and Rectangle then you have to overload paint

CODE

class MyPanel extends JPanel {
    MyPanel() {
       add(new JLabel("Hello");
       add(new JButton("OK");
       ... addd other JComponent
    }
    // I am overloading piant to do my own stuff
    public void paint(Graphics g) {
         // now that I have overload paint... do I know how to draw the JLabel and JButton ?
         // obviously not. So let the super method do the job
         super.paint(g);
         // now that the JLabel and JButton are paint les me do my own drawing/animation
         g.drawOval(......
    }
}


So you have to overload and call super if you have "standard" components (that you don't know how to paint) and other

Now if your code is 100% your own drawing... you do not need to call super, just do your drawing

CODE

class MyPanel extends JPanel {
    MyPanel() {
    }
    // I am overloading paint to do my own stuff there are no "standard" GUI elements to draw
    public void paint(Graphics g) {
         // now that the JLabel and JButton are paint les me do my own drawing/animation
         g.drawOval(......
    }
}


Hope this help


User is offlineProfile CardPM
+Quote Post

pbl
RE: What Does The Super.paintComponent(g) Do?
11 Jan, 2009 - 12:59 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,954



Thanked: 673 times
Dream Kudos: 200
My Contributions
QUOTE(bastones @ 11 Jan, 2009 - 12:39 PM) *

Hi, thanks for your reply. So am I right in saying that when I use the paintComponent method it is overloading specific methods that make panels (like JPanel) opaque? I understand that super invokes the methods we've overloaded...

Opaque has nothing to do here
I just draw the component that it kowns about
It know abot them because you panel.add(component) before
When component repaint() themselve they know if they have to be opaque or not

Actually in the example I gave you you can call super to paint the background

CODE

class MyPanel extends JPanel {
    MyPanel() {
    }
    // I am overloading paint to do my own stuff there are no "standard" GUI elements to draw
    public void paint(Graphics g) {
         // call father to repaint the background
         super.paint(g);
         // now that the JLabel and JButton are paint les me do my own drawing/animation
         g.drawOval(......
    }
}


This post has been edited by pbl: 11 Jan, 2009 - 01:01 PM
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:17PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month