super and paint component

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalar
    New Member
    • Aug 2007
    • 82

    super and paint component

    I have a code something like this:
    Code:
    public class panelPic extends JPanel
    {
                   .......................
    
                protected void paintComponent(Graphics g)
                {  
                         //super.paintComponent(g);
                         g.setColor(Color.RED);
                         g.drawString ("hello",d.width/2, d.height/2);
     
                        ...................................
                        super.paintComponent(g);
                }
    }
    a)First what exactly super.paintComp onent(g) do? I deleted this from my program and still works
    b)Is there any difference if i put the super.paintComp onent(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
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    [CODE=Java]import java.awt.*;
    import javax.swing.*;

    public class PaintExample extends JPanel {
    public static void main(String[] args) {
    EventQueue.invo keLater(new Runnable(){
    public void run() {
    go();
    }
    });
    }

    static void go() {
    JPanel contentPane = new JPanel(new GridLayout(0,1, 5, 5));
    contentPane.set PreferredSize(n ew Dimension(400, 300));
    contentPane.add (new PaintExample(Su perCall.BEFORE) );
    contentPane.add (new PaintExample(Su perCall.AFTER)) ;
    contentPane.add (new PaintExample(Su perCall.NEVER)) ;

    JFrame f = new JFrame("PaintEx ample");
    f.setContentPan e(contentPane);
    f.setDefaultClo seOperation(JFr ame.EXIT_ON_CLO SE);
    f.pack();
    f.setLocationRe lativeTo(null);
    f.setVisible(tr ue);
    }

    enum SuperCall {BEFORE, AFTER, NEVER};

    private SuperCall when;

    public PaintExample(Su perCall when) {
    this.when = when;
    setBackground(n ew Color(0xee, 0xee, 0xff));
    setBorder(Borde rFactory.create TitledBorder(wh en.toString())) ;
    }

    @Override()
    protected void paintComponent( Graphics g) {
    if (when == SuperCall.BEFOR E) {
    super.paintComp onent(g);
    }

    g.setColor(Colo r.RED);
    g.drawString ("hello",getWid th()/2, getHeight()/2);

    if (when == SuperCall.AFTER ) {
    super.paintComp onent(g);
    }
    }
    }[/CODE]
    I don't think I need to add anything. Study the code and its effect.

    Comment

    • kalar
      New Member
      • Aug 2007
      • 82

      #3
      thank you @BigDaddyLH but let me know if i understad good.
      The super.paintComp onent 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
      Code:
      public class panelPic extends JPanel
      {
         ImageIcon pic = new ImageIcon(getClass().getResource("picture.jpg"));
      
         protected void paintComponent(Graphics g)
         {  
                Dimension d = getSize();
                g.drawImage(vasiko_koutaki_Pic.getImage(),0, 0, d.width, d.height, null);
      
               super.paintComponent(g);
        }
      
      }
      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???

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

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

        [code=java]
        public void paint(Graphics g) {
        paintComponent( g);
        paintBorder(g);
        paintChildren(g );
        }
        [/code]

        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

        Comment

        • kalar
          New Member
          • Aug 2007
          • 82

          #5
          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??

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            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.

            Comment

            • kalar
              New Member
              • Aug 2007
              • 82

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

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                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

                Comment

                Working...