I am a total beginner, just struggling through my first Java book: Head First Java.
In the book we made a GUI which has 2 buttons, a label and a panel (the panel is a subclass of JPanel). What the program does is the following: When I press one of the two buttons, in the panel there is a rectangle that changes its color. When I press the other button it has to change the text on the label. Now the problem is that when I start the program, the FIRST time I press the button on which the label must change, this also changes the color in the rectangle, which it should not (I also noticed that when i FIRST click the rectangle shifts a little bit to the left). After that the program works fine.
The paintComponent method of your MyDrawPanel2 class will be called whenever the Swing software deems it to be necessary. That certainly includes when repaint() is called, but there are other reasons to repaint the component. (Try minimizing and then restoring the application to see one reason.) So if you want to control when the colour changes, don't put the code which changes the colour there. Put it in a method which you control, and call that method when the button is clicked.
:) Yeah I see, everytime I minimize and maximize it changes the color in the rectangle.
I want to try your solution, but I am a bit confused. Should I add a new method in the TwoButton class which is called by the listener and then recalculate the colors? But how do I send the colors to the paintComponent when I invoke the rePaint() method?
Paul Clapham wrote:The paintComponent method of your MyDrawPanel2 class will be called whenever the Swing software deems it to be necessary. That certainly includes when repaint() is called, but there are other reasons to repaint the component. (Try minimizing and then restoring the application to see one reason.) So if you want to control when the colour changes, don't put the code which changes the colour there. Put it in a method which you control, and call that method when the button is clicked.
Pieter Ramaekers wrote:Should I add a new method in the TwoButton class which is called by the listener and then recalculate the colors?
Yes, exactly.
Edit: Well, no, not exactly. The colours belong to the panel so they should be part of the MyDrawPanel2 class.
But how do I send the colors to the paintComponent when I invoke the rePaint() method?
You don't "send" them anywhere. They should be attributes (i.e. class-level variables) of the MyDrawPanel2 class, so that all public methods of that class have access to them.
1) Instead of have two methods setStartColor/setEndColor, you should only have a single method, setGradientColors and pass in two Color objects.
2) Then in the setGradientColors() method you should invoke repaint(), instead of invokeing fr.repaint() in your main class. That is your custom panel class should be responsible for repainting itself when any of its properties change.
3) You should be invoking super.paintComponent() at the start of the paintComponent() method.
Rob Camick wrote:1) Instead of have two methods setStartColor/setEndColor, you should only have a single method, setGradientColors and pass in two Color objects.
2) Then in the setGradientColors() method you should invoke repaint(), instead of invokeing fr.repaint() in your main class. That is your custom panel class should be responsible for repainting itself when any of its properties change.
3) You should be invoking super.paintComponent() at the start of the paintComponent() method.
I did all of it and it works like a charm! But can you please enlighten me why I have to invoke super.paintComponent(g); at the beginning of the paintComponent() method? I first did not include this to see what would happen, and idd the GUI had some weird behavior. After clicking one of the buttons it drawed a new frame inside the existing frame,... but it only does this once when not invoking the super.paintComponent(g); What is this about?
For other beginners who want to play with this little program, here the final code:
The super.paintComponent() paints the background of the panel. If you don't clear the background every time you can have painting artifacts.
Read the section from the Swing tutorial on Custom Painting for more information and examples. Also, take a look at the table of contents for more information about Swing basics.
Rob Camick wrote:The super.paintComponent() paints the background of the panel. If you don't clear the background every time you can have painting artifacts.
Read the section from the Swing tutorial on Custom Painting for more information and examples. Also, take a look at the table of contents for more information about Swing basics.