Results 1 to 5 of 5
- 03-18-2011, 11:42 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Picture not showing when create JAR file
When I run the below code from the console or in Eclipse it run perfect with the "logo.jpg" showing in the center of my screen.
When I extract it into a JAR File and them try to run it. It seem like it can't find the picture. I can't figure out what I'm doing wrong.
Java Code:class LogoFlashScreen { String logo = "logo.jpg"; Toolkit toolkit; int logoWidth; int logoHeight; int x; int y; public void runFlashScreen() { JWindow flashWindow = new JWindow(); LogoPanel logoPanel = new LogoPanel(); flashWindow.add(logoPanel); getLogoSizeAndLocation(); flashWindow.setSize(logoWidth, logoHeight); flashWindow.setLocation(x, y); flashWindow.setVisible(true); for (int i=0; i<50; i++) { try { Thread.sleep(20); }catch(Exception ex) { } } flashWindow.setVisible(false); } public void getLogoSizeAndLocation() { logoWidth = new ImageIcon(logo).getIconWidth(); logoHeight = new ImageIcon(logo).getIconHeight(); toolkit = Toolkit.getDefaultToolkit(); Dimension screenSize = toolkit.getScreenSize(); x = (int) (screenSize.getWidth() - logoWidth)/2; y = (int) (screenSize.getHeight() - logoHeight)/2; } @SuppressWarnings("serial") class LogoPanel extends JPanel { public void paintComponent(Graphics g) { Image image = new ImageIcon(logo).getImage(); g.drawImage(image,0,0,this); } } }Last edited by kmckinley820; 03-18-2011 at 11:44 PM.
- 03-19-2011, 12:04 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
When I run the below code from the console or in Eclipse it run perfect with the "logo.jpg" showing in the center of my screen.
When I extract it into a JAR File and them try to run it. It seem like it can't find the picture.
Actually that code lacks a main() method so what follows is, in part, guesswork.
The code repeatedly includes the expression "new ImageIcon(logo)" where logo is a String. According to the ImageIcon API docs this constructor "Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image. The specified String can be a file name or a file path".
I am guessing that the image is actually included as part of the jar archive. It is the reliance in your code on filenames that is the source of the problem. CLearly once you have an executable jar it can be moved to and run from any location at all. References to specific file locations that make sense in one context will not make sense in another.
The solution is to use a different constructor: ImageIcon(URL location) or ImageIcon(Image image) perhaps.
The Class method getResource() is useful for constructing a URL if the image is included in the jar file. And the ImageIO class provides ways of constructing an image from a URL.
-----
A different matter is the fact you are continually using this expression. It might be better to read the image and construct the icon once. (Just as the String logo is given a value just once and then reused.)
- 03-19-2011, 07:40 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
The main() method is in another class that is part of this program.
I've made some of the changes you suggested:
I can't figure out how the use the Class method getResource. I just started coding with Java about three weeks ago, so i have alot to learn still.
Java Code:class LogoFlashScreen { ImageIcon logo = new ImageIcon("logo.jpg"); Toolkit toolkit; int logoWidth; int logoHeight; int x; int y; public void runFlashScreen() { JWindow flashWindow = new JWindow(); LogoPanel logoPanel = new LogoPanel(); flashWindow.add(logoPanel); getLogoSizeAndLocation(); flashWindow.setSize(logoWidth, logoHeight); flashWindow.setLocation(x, y); flashWindow.setVisible(true); for (int i=0; i<50; i++) { try { Thread.sleep(60); }catch(Exception ex) { } } flashWindow.setVisible(false); } public void getLogoSizeAndLocation() { logoWidth = logo.getIconWidth(); logoHeight = logo.getIconHeight(); toolkit = Toolkit.getDefaultToolkit(); Dimension screenSize = toolkit.getScreenSize(); x = (int) (screenSize.getWidth() - logoWidth)/2; y = (int) (screenSize.getHeight() - logoHeight)/2; } @SuppressWarnings("serial") class LogoPanel extends JPanel { public void paintComponent(Graphics g) { Image image = logo.getImage(); g.drawImage(image,0,0,this); } } }
- 03-19-2011, 08:40 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
I figured it out. Below is the final code.
Thanks for all the help!!!!
Java Code:class LogoFlashScreen { URL logoURL = this.getClass().getResource("/resource/logo.jpg"); ImageIcon logo = new ImageIcon(logoURL); Toolkit toolkit; int logoWidth; int logoHeight; int x; int y; public void runFlashScreen() { JWindow flashWindow = new JWindow(); LogoPanel logoPanel = new LogoPanel(); flashWindow.add(logoPanel); getLogoSizeAndLocation(); flashWindow.setSize(logoWidth, logoHeight); flashWindow.setLocation(x, y); flashWindow.setVisible(true); try { Thread.sleep(2000); }catch(Exception ex) { } flashWindow.setVisible(false); } public void getLogoSizeAndLocation() { logoWidth = logo.getIconWidth(); logoHeight = logo.getIconHeight(); toolkit = Toolkit.getDefaultToolkit(); Dimension screenSize = toolkit.getScreenSize(); x = (int) (screenSize.getWidth() - logoWidth)/2; y = (int) (screenSize.getHeight() - logoHeight)/2; } @SuppressWarnings("serial") class LogoPanel extends JPanel { public void paintComponent(Graphics g) { Image image = logo.getImage(); g.drawImage(image,0,0,this); } } }
- 03-19-2011, 10:11 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
things necessary to take note of before going to learn EE
By kulangotski in forum New To JavaReplies: 0Last Post: 12-28-2010, 06:30 AM -
How to write the JNLP appln for installing the note.exe file in the client location
By srilatha in forum Advanced JavaReplies: 5Last Post: 07-26-2009, 03:37 PM -
note while compiling
By j2vdk in forum New To JavaReplies: 3Last Post: 09-03-2008, 11:52 PM -
how to creat collection
By STILET in forum Advanced JavaReplies: 2Last Post: 07-29-2008, 07:17 AM -
Print a picture file
By oli001 in forum New To JavaReplies: 0Last Post: 11-26-2007, 01:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks