Connecting Tech Pros Worldwide Forums | Help | Site Map

super and paint component

Member
 
Join Date: Aug 2007
Posts: 82
#1: Jun 20 '08
I have a code something like this:
Expand|Select|Wrap|Line Numbers
  1. public class panelPic extends JPanel
  2. {
  3.                .......................
  4.  
  5.             protected void paintComponent(Graphics g)
  6.             {  
  7.                      //super.paintComponent(g);
  8.                      g.setColor(Color.RED);
  9.                      g.drawString ("hello",d.width/2, d.height/2);
  10.  
  11.                     ...................................
  12.                     super.paintComponent(g);
  13.             }
  14. }
  15.  
a)First what exactly super.paintComponent(g) do? I deleted this from my program and still works
b)Is there any difference if i put the super.paintComponent(g) in line 12 or in line 7 ?It works in both cases.Why?
Thanks

for a) i understand that we call the method paintComponent from the parent class
also i read that in JFrame the paint method (is like paintComponent) is called in order to work good ,example the title of the frame ....
In JPanel what do?

BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Jun 20 '08

re: super and paint component


Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class PaintExample extends JPanel {
  5.     public static void main(String[] args) {
  6.         EventQueue.invokeLater(new Runnable(){
  7.             public void run() {
  8.                 go();
  9.             }
  10.         });
  11.     }
  12.  
  13.     static void go() {
  14.         JPanel contentPane = new JPanel(new GridLayout(0,1, 5, 5));
  15.         contentPane.setPreferredSize(new Dimension(400, 300));
  16.         contentPane.add(new PaintExample(SuperCall.BEFORE));
  17.         contentPane.add(new PaintExample(SuperCall.AFTER));
  18.         contentPane.add(new PaintExample(SuperCall.NEVER));
  19.  
  20.         JFrame f = new JFrame("PaintExample");
  21.         f.setContentPane(contentPane);
  22.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.         f.pack();
  24.         f.setLocationRelativeTo(null);
  25.         f.setVisible(true);
  26.     }
  27.  
  28.     enum SuperCall {BEFORE, AFTER, NEVER};
  29.  
  30.     private SuperCall when;
  31.  
  32.     public PaintExample(SuperCall when) {
  33.         this.when = when;
  34.         setBackground(new Color(0xee, 0xee, 0xff));
  35.         setBorder(BorderFactory.createTitledBorder(when.toString()));
  36.     }
  37.  
  38.     @Override()
  39.     protected void paintComponent(Graphics g) {
  40.         if (when == SuperCall.BEFORE) {
  41.             super.paintComponent(g);
  42.         }
  43.  
  44.         g.setColor(Color.RED);
  45.         g.drawString ("hello",getWidth()/2, getHeight()/2);
  46.  
  47.         if (when == SuperCall.AFTER) {
  48.             super.paintComponent(g);
  49.         }
  50.     }
  51. }
I don't think I need to add anything. Study the code and its effect.
Member
 
Join Date: Aug 2007
Posts: 82
#3: Jun 21 '08

re: super and paint component


thank you @BigDaddyLH but let me know if i understad good.
The super.paintComponent method clears the panel. So if i call it after everything i draw
is deleted.If i never call it java call its for me. Right????
BUT why in my programm if i put before and after is the same thing?
i mean that if i put it after it doesn't delete what has been drawn?

Maybe the super is called always first , like static is always first initiates???

let me give you some code
Expand|Select|Wrap|Line Numbers
  1. public class panelPic extends JPanel
  2. {
  3.    ImageIcon pic = new ImageIcon(getClass().getResource("picture.jpg"));
  4.  
  5.    protected void paintComponent(Graphics g)
  6.    {  
  7.           Dimension d = getSize();
  8.           g.drawImage(vasiko_koutaki_Pic.getImage(),0, 0, d.width, d.height, null);
  9.  
  10.          super.paintComponent(g);
  11.   }
  12.  
  13. }
so this code it prints me correct the picture.If understand good from your example the super... should delete the picture
i also make setOpaque(false).
Why this happen???
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 21 '08

re: super and paint component


You're mixing up too many things; basically the paint() method of the JComponent
class does this:

Expand|Select|Wrap|Line Numbers
  1. public void paint(Graphics g) {
  2.    paintComponent(g);
  3.    paintBorder(g);
  4.    paintChildren(g);
  5. }
  6.  
Its paintComponent() method checks whether or not the component is opaque;
if so, it sets the entire component to its current background colour. If you override
the paintComponent() method you either call the super method or you don't.
If you don't you're on your own and the 'opaqueness' is your own business. If you
do, you probably should call it before you draw your own stuff otherwise the
super paintComponent() method will erase everything if the component happens
to be opaque.

kind regards,

Jos
Member
 
Join Date: Aug 2007
Posts: 82
#5: Jun 21 '08

re: super and paint component


I am confused know.I understand that my program works because i make setOpaque(false).But where did i paint my picture??? in the Jpanel ?? or back in the JFrame??
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#6: Jun 22 '08

re: super and paint component


Quote:

Originally Posted by kalar

I am confused know.I understand that my program works because i make setOpaque(false).But where did i paint my picture??? in the Jpanel ?? or back in the JFrame??

It's not clear what you are asking. Where=on the monitor.
Member
 
Join Date: Aug 2007
Posts: 82
#7: Jun 22 '08

re: super and paint component


where, i mean in the jpanel or in the jframe?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#8: Jun 22 '08

re: super and paint component


Quote:

Originally Posted by kalar

where, i mean in the jpanel or in the jframe?

The JPanel class extends the JComponent class and all the drawing is done
visually bounded by the JComponent/JPanel. The JPanel class itself doesn't
implement any paint method, i.e. it leaves that responsibility to its parent class.

If you extend the JPanel class and if you override the paintComponent() method
it's your code that paints in the area bounded by the JComponent (the ancestor
class of them all). Your class overrided the paintComponent() method so the
JCompontent's paint() method will call your paintComponent() method.

That's how overriding works and the JComponent class makes clever use of it.
If your paintComponent() method wants to clear the background area all it has
to do is call the super paintComponent() method and make the component
opaque when it is initialized.

The JComponent and therefore your extension of the JPanel class is part of the
container that happens to be a JFrame so yes, everything is visually drawn
inside the bounds of that JFrame.

kind regards,

Jos
Reply