• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

not able to get the coloured oval

 
rammie singh
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
output.JPG
[Thumbnail for output.JPG]
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rammie singh
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic