Results 1 to 3 of 3
- 10-10-2011, 07:09 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
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); } }
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 } }
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:
the browser only gives this:
Raw Menu Beta
and the question is ofcourse: How to solve/fix this?
Thanks in advance,
Resk
- 10-10-2011, 08:03 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 785
- Rep Power
- 12
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
- 10-10-2011, 09:34 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
Similar Threads
-
Loading Images
By TacoManStan in forum Java GamingReplies: 9Last Post: 09-23-2011, 12:18 AM -
Loading files(namely images)
By jammas615 in forum New To JavaReplies: 1Last Post: 07-11-2011, 11:06 AM -
Loading images from an Array
By pinkette in forum New To JavaReplies: 5Last Post: 04-15-2011, 12:37 PM -
Loading Images - Imp
By Thulasiraman in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 10:33 AM
Bookmarks