Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Don't repaint?

 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a JFrame that draws an image, but as soon as the image is drawn, the window repaints. How do I force the image to remain drawn? The image is drawn in a method, and as soon as that method exits, the frame is emptied.
 
Joe Ess
Bartender
Posts: 9311
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Share some code with us so we know what you are talking about.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the image drawing code in the paint() or paintComponent() method. If you're painting to the component anywhere else it won't be a lasting change (as you've found).
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I override the paint method for only one JFrame? I might have forgotten to mention it in my first post, I've got two JFrames which will be visible at the same time, if I override paint, won't it paint the image over both frames?
As for code, I don't know if there is anything spectacular about it that will highlight my problem. What I do is create the image, then draw it on the JFrame jf_work using jf_work.drawImage(bi, 0, 0, this);
 
Joe Ess
Bartender
Posts: 9311
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carl Pettersson:
How do I override the paint method for only one JFrame?

Subclass JFrame and use that to create one of your frames. Use a regular JFrame to create the other.


As for code, I don't know if there is anything spectacular about it that will highlight my problem.

Guess I could use my Magic 8 Ball. . .
The answer is VERY DOUBTFUL
Make of it what you will.
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked fine, thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic