Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFrame with image

 
Conrad McLaughlin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really basic. I'm trying to create a splash screen for my program.
What is the best way?

How do I have a Jframe with has a jpg image?

At the moment I have a button with an imageicon as this is the only way I know how to show an image. But it does not look good.

 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, let me just say that in Java SE 6.0, there will be a new splash screen facility which makes this much easier, and loads the screen faster since it takes loading priority over the rest of the application (as a splash screen should). For more information:

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/mustang/splashscreen/index.html

To do it the old way, or to draw images in general, there are 3 steps:

(1) Load the image file into a BufferedImage object
(2) Create a window (e.g. AWT's Window or Swing's JWindow are good)
(3) Draw the BufferedImage onto the window

I'm assuming you've got JS2E 1.4 or higher, so I'm going to make use of the Image IO API, found in javax.imageio. In particular, to obtain a BufferedImage of a JPEG file, all we need to do is:



That's (1) done; now we need to create a Window and paint the image on its canvas - I'm going to use a JWindow, since you're using a JFrame in your example, and I'm only going to outline what to do - you can fill in the details. So:



I think that should do the trick - I haven't run this example, so if there are any errors, please let me know and I'll be happy to correct myself!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic