Results 1 to 6 of 6
  1. #1
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default what made paintComponent() method to be called twice??

    Consider the following code :

    Java Code:
    import java.io.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    
    import java.awt.Graphics;
    import java.awt.Color;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class JPanel01 extends JPanel {
    
        private boolean flag = false;
        private JButton jButton;
    
        public JPanel01() {
            jButton = new JButton("Draw Line");
            add(jButton);
    
            jButton.addActionListener(
                    new ActionListener() {
    
                        public void actionPerformed(ActionEvent event) {
                            if (event.getSource() == jButton) {
                                flag = true;
                                repaint();
                            }
                        }
                    });
    
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
           
            if (!flag) {
                System.out.println("its false");
            }
            if (flag) {
                System.out.println("its true");
                g.drawLine(100, 100, 500, 500);
            }
        }
    
        public static void main(String args[]) throws IOException {
            JFrame frame = new JFrame("default");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JPanel01());
            frame.setSize(1280, 500);
            frame.setVisible(true);
        }
    }
    this code when is made to run returns "its false" every time the window is resized until the button is not pressed i.e., what to make motice of is that for the very first time when the window is displayed the output is single "its false".


    Now consider the following code : in which the class JPanel01 extends the JPanel GraphPaper

    Java Code:
    import java.io.*;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    
    import java.awt.Graphics;
    import java.awt.Color;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class JPanel01 extends GraphPaper {
    
        private boolean flag = false;
        private JButton jButton;
    
        public JPanel01() {
            jButton = new JButton("Draw Line");
            add(jButton);
    
            jButton.addActionListener(
                    new ActionListener() {
    
                        public void actionPerformed(ActionEvent event) {
                            if (event.getSource() == jButton) {
                                flag = true;
                                repaint();
                            }
                        }
                    });
    
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
    
            if (!flag) {
                System.out.println("its false");
            }
            if (flag) {
                System.out.println("its true");
                g.drawLine(100, 100, 500, 500);
            }
        }
    
        public static void main(String args[]) throws IOException {
            JFrame frame = new JFrame("default");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JPanel01());
            frame.setSize(1280, 500);
            frame.setVisible(true);
        }
    }
    extended JPanel is:

    Java Code:
    import java.awt.Graphics;
    import java.awt.Color;
    
    public class GraphPaper extends javax.swing.JPanel {
        
        @Override
        public void paintComponent(Graphics g)
        {        
            super.paintComponent(g);
            this.setBackground(Color.WHITE);
                    
        }
    }
    these code is when made to run outputs "its false" twice initially.. It happened due the presence of the statement -this.setBackground(Color.WHITE);
    in the extended JPanel i.e., GraphPaper.


    But Why it happened so .. and how come the paintComponent() method in the
    JPanel01 is called twice initially. [If you remove this above mentioned statement the output is "its false" only once]
    plz explain.thnx:eek:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't know why it's calling paintComponent twice, but I agree that it sure is. Likely it has to do with your setting the background color in the wrong location as it you shouldn't be set from within paintComponent but rather elsewhere such as the constructor:

    Java Code:
    class GraphPaper extends javax.swing.JPanel {
    
      public GraphPaper() {
        setBackground(Color.white);
      }
      
      @Override
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //this.setBackground(Color.WHITE);
        // this method is now somewhat superfluous 
        //unless you're doing other painting here
      }
    }

  3. #3
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    Yes you are absolutely right the statement when placed inside the constructor doesn't creates problem any more.. thnx.

  4. #4
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    setting the background color in the wrong location
    In what senses is the declaration of the background color inside the paintComponent() method is wrong ?? plz explain.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Y. Progammer View Post
    In what senses is the declaration of the background color inside the paintComponent() method is wrong ?? plz explain.
    The method only needs to be called once when setting up the JPanel. If it is located within the paintComponent method it will be needlessly called whenever painting occurs.

  6. #6
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

Similar Threads

  1. No class made
    By ChuckLS in forum New To Java
    Replies: 1
    Last Post: 04-28-2009, 05:54 PM
  2. [SOLVED] Method from one thread called on another thread
    By Ypsilon IV in forum Threads and Synchronization
    Replies: 7
    Last Post: 04-24-2009, 03:07 PM
  3. Replies: 2
    Last Post: 03-22-2009, 07:43 PM
  4. Replies: 0
    Last Post: 02-18-2009, 06:20 AM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •