2 Attachment(s)
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
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
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:
Attachment 1865
the browser only gives this:
Attachment 1866
Raw Menu Beta
and the question is ofcourse: How to solve/fix this?
Thanks in advance,
Resk
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
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