aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes override repaint() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "override repaint()" Watch "override repaint()" New topic
Author

override repaint()

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

In Marcus Green test 1 question 34, he gives correct answer as only answer 2. I chose answer 1 and 2. why is answer 1 wrong?
Question 34)
You have created an applet that draws lines. You have overriden the paint operation and used the graphics drawLine method, and increase one of its parameters to multiple lines across the screen. When you first test the applet you find that the news lines are redrawn, but the old lines are erased. How can you modify your code to allow the old lines to stay on the screen instead of being cleared.
1) Override repaint thus
public void repaint(Graphics g){
paint(g);
}
2)Override update thus
public void update(Graphics g) {
paint(g);
}
3) turn off clearing with the method setClear();
4) Remove the drawing from the paint Method and place in the calling code

SCJP
Visit my download page
David Roberts
Ranch Hand

Joined: Nov 03, 2000
Posts: 142
A theory would be:
Update schedules a call to repaint. If you don't override update, then the original repaint() is called. What you're doing when you answer #1 is overloading repaint, since there is not repaint(Graphics g).
i.e.
you make repaint(Graphics g)
you call update(Graphics g)
update(Graphics g) //not overriden calls repaint() the orginal version.
by overridding update(Graphics g) you force the graphics object to be passed to paint, which already accepts a graphics object.
I think that's right, but I could be wrong. Maybe others could confirm it.
- David

David Roberts - SCJP2,MCP
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

repaint() calls update(Graphics g) not the other way around. However you spotted the problem thank you. There is no repaint(Graphics g) in Component class.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
If you want to keep what is already there then you need to override update(). What update() does is clear the screen and then paints it again. When you override update(), you just paint it, you don't clear the screen.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: override repaint()