Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

///paint///

 
chao-long liao
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JohnJuntMockExam..
In order to cause the paint(Graphics) method to execute, which of the following is the most appropriate method to call:
a.paint()
b.repaint()
c.paint(Graphics)
d.update(Graphics)
e.None – you should never cause paint(Graphics) to execute
Select the most appropriate answer.
The answer is B,I don't know the difference between a,b and c,can any one help me to figure out the concept.thanks.
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Chao-long
RHE book fully describes repaint(), update(),paint() methods. A small quote from this book:

.....
The code above shows the preferred approach to handling events that cause the screen to be changed: event handlers store information in instance variables and then call repaint(), and paint() draws the screen according to the information in the instance variables. The two benefits of this approach are
� The screen is correctly repaired when the environment spontaneously calls paint().
� The Virtual Machine never gets overwhelmed by events.
If you want to accumulate dots, rather than have each dot cleared away when a new one is to be drawn, you can always override update() so that it does not clear. All update() needs to do in this case is call paint(), as shown below:
1. public void update(Graphics g) {
2. paint(g);
3. }

repaint() calls update() and update() calls paint().
Jamal Hasanov
www.j-think.com
 
chao-long liao
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,you are so kind.
Actually I have read the page you mentioned in RHE book,but I still confuesd.
Let me simplify my question.Why the answer of following question is "b" ??how about "a"?
In order to cause the paint(Graphics) method to execute, which of the following is the most appropriate method to call:
a.paint()
b.repaint()
c.paint(Graphics)
d.update(Graphics)
e.None – you should never cause paint(Graphics) to execute
Select the most appropriate answer.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no method called paint().
there is paint(Graphics g ).
since you cant (or wouldnt advise) create a graphics pbject then you should use the repaint() method who does the job for you.
 
Alan Chong
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of calling paint() method, calling repaint() method make sure that the screen is
not updated more than ten times per second.
Too many updates will degrade performance.
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- Calling paint(Graphics g) will work, because the method will draw the screen again (assuming you have created a Graphics object using the getGraphics() method of the Component class).
- Calling update() will also work, because it calls the paint() method.
However, the question asks for the most appropriate answer (which is the call to repaint() ), because in addition to calling update() (which in turn call paint() ), it also schedules the calls to these methods for optimum CPU usage, as well as other benefits.
There are cases when repaint() isn't the best choice to use (i.e. in animation), but in most cases, repaint() will do the job fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic