File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame with image" Watch "JFrame with image" New topic
Author

JFrame with image

Conrad McLaughlin
Greenhorn

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
Author
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:

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!


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: http://aspose.com/file-tools
 
subject: JFrame with image
 
Similar Threads
'Stale' image reference is retained by JFrame desktop app--latest image won't display
how to run sample swing program
JFrame - Always on top?
ImageIcon
JButton slow response time