File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JFrame with image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame with image" Watch "JFrame with image" New topic

JFrame with image

Conrad McLaughlin

Joined: Jan 31, 2006
Posts: 27
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
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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:

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!

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
I agree. Here's the link:
subject: JFrame with image
jQuery in Action, 3rd edition