what made paintComponent() method to be called twice??
Consider the following code :
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
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:
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: