Results 1 to 6 of 6
- 02-21-2010, 09:17 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 68
- Rep Power
- 0
what made paintComponent() method to be called twice??
Consider the following code :
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".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); } }
Now consider the following code : in which the class JPanel01 extends the JPanel GraphPaper
extended JPanel is: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); } }
these code is when made to run outputs "its false" twice initially.. It happened due the presence of the statement -this.setBackground(Color.WHITE);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); } }
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:
-
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 } }
- 02-21-2010, 09:43 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 68
- Rep Power
- 0
Yes you are absolutely right the statement when placed inside the constructor doesn't creates problem any more.. thnx.
- 02-21-2010, 09:46 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 68
- Rep Power
- 0
In what senses is the declaration of the background color inside the paintComponent() method is wrong ?? plz explain.setting the background color in the wrong location
-
- 02-21-2010, 10:19 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 68
- Rep Power
- 0
Similar Threads
-
No class made
By ChuckLS in forum New To JavaReplies: 1Last Post: 04-28-2009, 04:54 PM -
[SOLVED] Method from one thread called on another thread
By Ypsilon IV in forum Threads and SynchronizationReplies: 7Last Post: 04-24-2009, 02:07 PM -
how to run my java program made in netbeans to another PC?
By kwink in forum AWT / SwingReplies: 2Last Post: 03-22-2009, 06:43 PM -
HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted
By R O C K Y in forum XMLReplies: 0Last Post: 02-18-2009, 05:20 AM -
paint() and paintComponent()
By goldhouse in forum Java 2DReplies: 1Last Post: 07-17-2007, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks