• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I paint something on my frame?

 
Joseph Alrawi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!

I simply want to paint an image on my frame, just once each time the paint() method is called, but I got no idea how to call the paint method in any good way. This is how it looks:


I have a frame in another class, and from my assumption I need to somehow tell my paint method to paint on that specific frame right?

Thanks for your help!
 
Stevens Miller
Bartender
Posts: 1422
29
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joseph, there's probably no reason to make your methods static here. But, more importantly, what is going to call the paint method of instances of your Image class?

Now, the way I do this is to create a subclass of JPanel, and override JPanel's (actually JComponent's) paint method. Then, whenever Swing decides to repaint the screen, it will eventually call the paint method of all instances of my subclass that need to be repainted. That's when my code gets its chance to run. I get a lot of flack here for this, but I don't always have my own paint method call the superclass's paint method, but that's because my paint method tends to take full responsibility for repainting the entire client window of my subclass. If you have other things in their, like controls, and so forth, you'll need to call the superclass method too. Try experimenting with that a bit, and it will all become clear very quickly.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote: . . . there's probably no reason to make your methods static . . .
By probably I take it you mean definitely.
You need to paint on something, and you usually use a panel. I think you are supposed to override that panel's paintComponent method (rather than paint) with protected access, but I shall move this discussion to the GUIs forum and people there will know for sure.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the section from the Swing tutorial on Custom Painting for details and working examples.

Also, there is no need to do custom painting. You can use a JLabel which can use an Icon to display an Image. The tutorial also has a section on "How to Use Icons".
 
Stevens Miller
Bartender
Posts: 1422
29
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Stevens Miller wrote: . . . there's probably no reason to make your methods static . . .
By probably I take it you mean definitely.

Heh. I have learned that, in computer programming, there is only one thing that is definite.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!