Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes not able to get the coloured oval Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "not able to get the coloured oval" Watch "not able to get the coloured oval" New topic
Author

not able to get the coloured oval

rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
hi
i am executing this prograame and it is executing also. but when i click the "Change colour" button, the progrrame is not showing the colured Oval.
can any body please tell me where i am going wrong ?



i have also attached the screen shot of the output.


[Thumbnail for output.JPG]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
You usually paint on a panel, which you add to the frame. You should probably subclass the panel, and override its paintComponent() (not paint()) method, so it starts like this
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

And to understand why you don't see the painting on your JFrame, read the API for JFrame and one of the interfaces it implements, RootPaneContainer. Then look up JRootPane.


luck, db
There are no new questions, but there may be new answers.
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
ok, i got this working.

here is the code i used




but i have couple of questions.
I am following "Head first Java" book.

it says that when a repaint() method is called , it calls paintComponent() method for every widget added to the frame.

My questions are
(1) is it not a overhead when a repaint() method is called , it calls paintComponent() method for every widget added to the frame. Although it is clear here that only Panel(here drawPanel) widget has provision of paintComponent() method being called and the button widget added to the frame here can not call paintComponent() method.

(2) My second question is .. when i executed this programme, how come at the first go itself the panel having colured oval is shown. where this should come only when the "Change Colour" button is clicked that is when an event ocurs then only the actioPerformed function is called and then repaint() function is called on the frame.


i hope i am clear to you guys ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
You have the colour in the paintComponent method, so it appears as orange when you first make the frame visible. Your change colour method doesn’t change anything.
It is probably quite efficient to paint all the components in turn, but maybe somebody else will have more information.
rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
Ok. So it means that with every call of frame.setVisible(true) method, paintComponent() method is called. right ?


And if it is called, then since the paintComponent() method here, which is overridden, is associated with panel , should be called only when it is panel's turn to call painComponent() method.

For frame, shouldnot it be that the default paintComponent() method is called rather than overridden one ?

Just asked this question, because of curiousity.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
As far as I know, the paintComponent method is called on the frame, and it then calls all its children recursively.
And you do in fact get changes in colour. Sorry, I was mistaken about that.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

Actually, JFrame does not have paintComponent; it's no descendant of JComponent. The rest of the story is true though - calling repaint() on the JFrame will cause all of its children to be painted, and this process goes on recursively until everything is repainted. Somewhere in between there is the DrawingPanel, so that's repainted as well. That causes paintComponent to be called.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: not able to get the coloured oval