Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Image  RSS feed

 
Kaby Khan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me how to add a .gif image in a Frame ?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of ways. One way is to create an ImageIcon of the gif and then apply that to a component like a JLabel or JButton.
Another way to to set a gif as your background Image.
After you create a new ImageIcon of your gif and then you add that to a JLabel the size of your JFrame but to make sure everything gets put on top of that image, you have to add this line.
myJFrame.add(myImageIcon, new Integer(Integer.MIN_VALUE))
 
Kaby Khan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool thanks ione thing though , I am using Frame not JFrame ... will it work like that too?
well I ll try thank alot though.
 
Kaby Khan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a Frame would that make a difference?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes... using AWT makes a difference... No ImageIcon class in AWT...

To display an Image in a frame you either have to override the paint() method of the Frame and draw your image there, or use another component ( like an extended Panel or Canvas ) to draw your image on, and then add that component to the Frame. I prefer the latter... Here's a class called ImagePanel that I just had lying around...



And here's a class that illustrates how to use it...



HTH,
-Nate
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nate is the man. If I were you, however, I would look at what I was programming and my design, and (depending on how far into it you are) evaluate the reasons you chose to use the AWT instead of SWING, for your Frame if not your whole App.
The AWT still serves it's purpose. I just really dig SWING alot.
Just my two cents.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!