Results 1 to 3 of 3
  1. #1
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Problems with loading images in a jar

    The context:

    I have a signed applet containing a custom JButton class and a JPanel, The JButton loads
    certain images that are visible in the Applet (a ImageIcon for the JButton).

    The problem:

    problem is that when I put the ButtonName.class and the Applet.class in a SIGNED .jar-file
    together with an images directory (which obviously contains my images) and load them on a
    site the ImageIcons (of the JButton) doesn't display. But the Java Console doesn't give any
    faults

    The code:

    The Applet

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    
    public class MODS extends JApplet {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	final String MENU = "Menu";
    	JPanel mainMenu;
    	CustomButton firstButton;
    	
    	public void init()
    	{
    		this.setSize(650, 400);
    		mainMenu = new JPanel();
    		this.add(mainMenu);
    		firstButton = new CustomButton(MENU);
    		mainMenu.setBackground(Color.black);
    		mainMenu.setSize(getSize());
    		mainMenu.setLayout(new BoxLayout(mainMenu, BoxLayout.Y_AXIS));
    		mainMenu.add(Box.createRigidArea(new Dimension(0,180)));
    		mainMenu.add(firstButton);
                    setContentPane(mainMenu);
    	}
    }
    CustomButton

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    
    
    public class CustomButton extends JButton implements MouseListener {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	Color activeColor = new Color(0,200,0);
    	Color sleepingColor  = new Color(0,150,0);
    	String buttonText;
    	int adv,hgt;
    	boolean mouseInside;
    	Font buttonFont = new Font("Verdana",Font.BOLD, 12);
    	FontMetrics fm=this.getFontMetrics(buttonFont);
    	ImageIcon active,sleeping;
    	
    public CustomButton(String buttonText) {
    	super();
    	sleeping = new ImageIcon("Images/sleepingIcon.gif");
    	active = new ImageIcon("Images/activeIcon.gif");
    	this.buttonText=buttonText;
    	setContentAreaFilled(false); 
    	setBorderPainted(false); 
    	addMouseListener(this);
    	mouseInside=false;
    	adv=fm.stringWidth(buttonText);
    	hgt=fm.getHeight();
    	setRolloverEnabled(true);
    	setIcon(sleeping);
    	setRolloverIcon(active);
    	setMaximumSize(new Dimension(100, 25));
    	setActionCommand(buttonText);
    	setAlignmentX(CENTER_ALIGNMENT);
    	}
    
    public void paintComponent(Graphics g)
    {
    	if (mouseInside){
    	g.setColor(activeColor);
    	active.paintIcon(this, g, 0, 0);
    	}
    	else {
    	g.setColor(sleepingColor);
    	sleeping.paintIcon(this, g, 0, 0);
    	}
    	g.setFont(buttonFont);
    	g.drawString(buttonText, getWidth()/2-adv/2, getHeight()/2+hgt/4);
    }
    
    @Override
    public void mouseClicked(MouseEvent arg0) {
    	// TODO Auto-generated method stub
    }
    
    @Override
    public void mouseEntered(MouseEvent e) {
    	mouseInside=true;
    }
    
    @Override
    public void mouseExited(MouseEvent arg0) {
    	mouseInside=false;
    }
    
    @Override
    public void mousePressed(MouseEvent arg0) {
    	// TODO Auto-generated method stub
    }
    
    @Override
    public void mouseReleased(MouseEvent arg0) {
    	// TODO Auto-generated method stub
    }
    
    }
    The Compilation:

    in the CMD Console (in the correct file):
    - javac -source 1.6 -target 1.6 MODS.java
    - jar -cf MODSS.jar *
    - keytool -genkey -keyalg rsa -alias mykey -validity 3650
    - jarsigner MODSS.jar mykey

    The result:

    Though eclipse gives me this result:

    Problems with loading images in a jar-eclipse.jpg

    the browser only gives this:

    Problems with loading images in a jar-site.jpg

    Raw Menu Beta

    and the question is ofcourse: How to solve/fix this?

    Thanks in advance,

    Resk

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Problems with loading images in a jar

    Load and set the images with the method of the class object(e.g. getResource). Then you will get an URL and can pass it to the ImageIcon cunstructor instead of the String/path!
    How to Use Labels (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    see the createImageIcon method in the examples code:
    http://download.oracle.com/javase/tu...LabelDemo.java

  3. #3
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Re: Problems with loading images in a jar

    :O that actually did the job...

    Thank alot for that!!! XD

    mmh maybe another question regarding me overusing this forum:
    -Are there any books about java about applets with all the ingredients to make dynamic JApplets? (Europe)

    Thnx again :)

    Resk

Similar Threads

  1. Loading Images
    By TacoManStan in forum Java Gaming
    Replies: 9
    Last Post: 09-22-2011, 11:18 PM
  2. Loading files(namely images)
    By jammas615 in forum New To Java
    Replies: 1
    Last Post: 07-11-2011, 10:06 AM
  3. Loading images from an Array
    By pinkette in forum New To Java
    Replies: 5
    Last Post: 04-15-2011, 11:37 AM
  4. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 09:33 AM

Posting Permissions

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