I'm learning awt at the moment, and can't seem to figure out why my paint method iterates twice..
Here is my main class 'Tutorial': ``` import java.awt.*; import javax.swing.*; public class Tutorial { public static void main(String[] args) { new Show(); } } ``` And my 2nd class which does the actual making of the JFrame & drawing on the canvas: ``` import java.awt.*; import javax.swing.*; public class Show extends Canvas { public Show(){ JFrame jf = new JFrame("Draw"); jf.setSize(600, 400); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.add(this); jf.setVisible(true); } public void paint(Graphics g){ g.setColor(JColorChooser.showDialog(null, "Choose Head Color", Color.CYAN)); g.fillOval(10, 10, 300, 200); g.setColor(JColorChooser.showDialog(null, "Choose Eye Color", Color.RED)); g.fillOval(60, 45, 50, 20); g.fillOval(200, 45, 50, 20); g.setColor(JColorChooser.showDialog(null, "Choose Mouth Color", Color.BLUE)); g.drawArc(145, 150, 20, 35, 70, 110); } } ``` I put in a System.out.println("0"); in the paint method, and found that the paint method iterates twice when I run the program.. thus each JColorChooser window will show up twice.. how would I go about the program to only have the paint method iterate once?
I'm still unsure why 2 instances of the paint method were made.. maybe one when I create an instance of the Show class from the main method -- and another when I use the this keyword in the constructor of Show.. this cheesy line of code is able to make it so it only iterates once - though i'd still like to understand it better: ``` import java.awt.*; import javax.swing.*; public class Show extends Canvas { public static int iterator = 0; public Show(){ JFrame jf = new JFrame("Draw"); jf.setSize(600, 400); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.add(this); jf.setVisible(true); } public void paint(Graphics g){ if(iterator == 0) { iterator++; g.setColor(JColorChooser.showDialog(null, "Choose Head Color", Color.CYAN)); g.fillOval(10, 10, 300, 200); g.setColor(JColorChooser.showDialog(null, "Choose Eye Color", Color.RED)); g.fillOval(60, 45, 50, 20); g.fillOval(200, 45, 50, 20); g.setColor(JColorChooser.showDialog(null, "Choose Mouth Color", Color.BLUE)); g.drawArc(145, 150, 20, 35, 70, 110); System.out.println(iterator); } } } ```
"The paint method is called by the Event Dispatch Thread (EDT) and is basically out of your control. It works as follows: When you realize a user interface (call setVisible(true) in your case), Swing starts the EDT. This EDT thread then runs in the background and, whenever your component needs to be painted, it calls the paint method with an appropriate Graphics instance for you to use for painting. So, when is a component "needed" to be repainted? -- For instance when The window is resized The component is made visible When you call repaint Simply assume that it will be called, whenever it is necessary." http://stackoverflow.com/questions/7764602/how-is-paint-running-without-being-called-in-the-main-method
I see - thanks. I could also have 3 color objects as fields in the class, and establish the colors in the constructor instead of in the paint method itself.
Yup, that's probably a better solution since you might lose some functionality in your GUI otherwise.
Join our real-time social learning platform and learn together with your friends!