Results 1 to 5 of 5
  1. #1
    kmckinley820 is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default 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-19-2011 at 12:44 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.)

  3. #3
    kmckinley820 is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    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);
    		}
    	}
    }

  4. #4
    kmckinley820 is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    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);
    		}
    	}
    }

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    URL logoURL = this.getClass().getResource("/resource/logo.jpg");

    You're welcome. I'm glad you've got it figured out.

Similar Threads

  1. things necessary to take note of before going to learn EE
    By kulangotski in forum New To Java
    Replies: 0
    Last Post: 12-28-2010, 07:30 AM
  2. Replies: 5
    Last Post: 07-26-2009, 04:37 PM
  3. note while compiling
    By j2vdk in forum New To Java
    Replies: 3
    Last Post: 09-04-2008, 12:52 AM
  4. how to creat collection
    By STILET in forum Advanced Java
    Replies: 2
    Last Post: 07-29-2008, 08:17 AM
  5. Print a picture file
    By oli001 in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 02:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •