Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

repaint() not erasing/redrawing strings?

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. Here's my dilemma: I'm trying to create an applet that asks for simple multiplication answers, and draws a string giving feedback after the question is answered ("Very good!" or "No, try again."). The problem is that the book says to draw everything from paint(), and gives the impression that repaint() should magically refresh everything on the applet, which it does not. How can I make the strings I drew in paint() go away and give way to new strings based on user answer (whether correct or not), instead of piling up on top of each other, because they don't refresh? Thanks for your time!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Please don't post the same question to more than one forum. See here for an explanation of why I just deleted the other copies of this thread.

JApplet works differently from the old-fashioned Applet class, and in particular, it's not really intended to have its paint() method overridden. If you need to do graphics, you're supposed to do the graphics on a JPanel, and add the JPanel to the JApplet's root pane.

My recommendation is just to use java.applet.Applet instead of javax.swing.JApplet to write this kind of code. If you really want to use JApplet, then subclass JPanel, do the graphics in the paintComponent() method of the JPanel (calling super.paintComponent() first) and then add this JPanel to your JApplet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic