• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

override repaint()

 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Roberts
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic