Reputation: 83
first i'd like to say that yes, i searched, i searched real hard and unfortunatelly, i did not find any answer good enough for me, i think i know what to do, but i don't know how to do it ! Well, i put a lot of images in my program, and it worked out great, when i run on my computer the images appear and there's no problem, but i can't make the images to appear in other pc's ! Everytime i put the .jar in another machine, the images disappear... Well, i think i have to create a package with the images folder right ? But i don't know how to do it, or what to do after that ! If someone could give me a fully example, that would be awesome... Here's my code, please let me know if can improve the question, i'm just new here !
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class IMC extends JFrame {
private static final long serialVersionUID = 1L;
private JButton ok;
private JButton sair;
private JTextField txt1;
private JTextField txt2;
private Dimension dim,dim2,dim3;
private JLabel l1,l2,l3,l4;
private Font letra;
private float tamanho;
private float peso;
private float imc;
public IMC (){
dim = new Dimension(650,500);
dim2 = new Dimension (340,25);
dim3 = new Dimension (75,35);
setTitle("Calcule seu IMC!");
setSize(dim);
setLocation(190,100);
setResizable(false);
setBackground(new Color(250,250,200));
Color lightGray = Color.gray.brighter();
getContentPane().setBackground(lightGray);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar mBar = new JMenuBar();
JMenu opcoes = new JMenu("Opções");
JMenuItem sobreIMC = new JMenuItem("Sobre");
JMenuItem sobreMim = new JMenuItem("Sobre mim");
opcoes.add(sobreIMC);
opcoes.add(sobreMim);
JMenu fechar = new JMenu("Fechar");
JMenuItem fecharIMC = new JMenuItem("Fechar");
fechar.add(fecharIMC);
mBar.add(opcoes);
mBar.add(fechar);
setJMenuBar(mBar);
tamanho = 0;
peso = 0;
letra = new Font("Arial",Font.PLAIN,14);
setFont(letra);
l1 = new JLabel();
l1.setText("Complete os campos abaixo:");
l1.setSize(250,70);
l1.setLocation(100,0);
l1.setFont(new Font("Arial", Font.BOLD,15));
this.add(l1);
l2 = new JLabel();
l2.setText("Insira sua altura:");
l2.setBounds(47,85,150,150);
l2.setFont(new Font("Arial", Font.BOLD,13));
this.add(l2);
l3 = new JLabel();
l3.setText("Insira seu peso:");
l3.setBounds(47,165,150,150);
l3.setFont(new Font("Arial", Font.BOLD,13));
this.add(l3);
l4 = new JLabel();
l4.setText("Gabriel Ozzy Santos");
l4.setBounds(230,270,150,100);
l4.setFont(new Font ("Verdana", Font.ITALIC,15));
this.add(l4);
ImageIcon im = new ImageIcon("C:/Users/Gabriel Ozzy/Downloads/Java3.1.jpg");
JLabel l5 = new JLabel(im);
l5.setBounds(255,350,120,120);
this.add(l5);
ok = new JButton();
ok.setText("OK");
ok.setLocation(180,390);
ok.setSize(dim3);
ok.setBackground(new Color(157,182,210));
ok.setForeground(Color.white);
this.add(ok);
sair = new JButton();
sair.setText("Sair");
sair.setLocation(370,390);
sair.setSize(dim3);
sair.setBackground(new Color (157,180,210));
sair.setForeground(Color.white);
this.add(sair);
txt1 = new JTextField();
txt1.setSize(dim2);
txt1.setLocation(160,150);
this.add(txt1);
txt2 = new JTextField();
txt2.setSize(dim2);
txt2.setLocation(160,230);
this.add(txt2);
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
String t1 = txt1.getText();
if(!"".equals(t1))
tamanho = Float.parseFloat(t1);
String t2 = txt2.getText();
if(!"".equals(t2))
peso = Float.parseFloat(t2);
imc = (peso/(tamanho*tamanho));
if (ok != null){
ImageIcon info = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
JOptionPane.showMessageDialog(null,"Seu IMC é:"+imc,"Resultado do IMC",0,info);
}
if (imc <18.5){
ImageIcon info2 = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
JOptionPane.showMessageDialog(null,"Você parece o esqueleto do HE-MAN!","Seu magrelo !",0, info2);
}
if (imc > 18.5 && imc < 24.9){
ImageIcon info3 = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
JOptionPane.showMessageDialog(null, "Ta no peso certinho :)", "Continue assim!",0,info3);
}
if (imc > 24.9 && imc < 29.9){
ImageIcon info4 = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
JOptionPane.showMessageDialog(null,"Você está ficando gordinho! :(", "Emagrece!",0,info4);
}
if (imc > 29.9 && imc < 34.9){
ImageIcon info5 = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
JOptionPane.showMessageDialog(null,"Gordo!","Baleia!",0,info5);
}
if (imc > 34.9){
ImageIcon info6 = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
JOptionPane.showMessageDialog(null,"Obeso mórbido!", "Orca Assassina!",0,info6);
}
tamanho = 0;
peso = 0;
txt1.setText("");
txt2.setText("");
}
});
sair.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.exit(0);
}
});
sobreIMC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
Icon about = new ImageIcon ("C:/Users/Gabriel Ozzy/Downloads/Java2.jpg");
JOptionPane.showMessageDialog(null,"Programado em Java 7 - Plataforma Eclipse","Sobre o software",0,about);
}
});
sobreMim.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
Icon about2 = new ImageIcon ("C:/Users/Gabriel Ozzy/Downloads/Java2.jpg");
JOptionPane.showMessageDialog(null,"Programador: Gabriel Ozzy Santos","Sobre o desenvolvedor",0,about2);
}
});
fecharIMC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.exit(0);
}
});
}
public static void main (String [] args){
new IMC().setVisible(true);
}
}
Upvotes: 1
Views: 4986
Reputation: 1499900
You should use ImageIcon(URL)
and get the resource URL from the classloader, e.g.
ImageIcon info = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg"));
(I'd avoid using non-ASCII characters in the filenames, by the way. It may well work, but it's just one more thing that can go wrong.)
Make sure the images are within your jar file (e.g. under "/images" in the above example).
Also note that you're loading the same image icon in each of your 5 bits of code. I'd strongly recommend loading it once and reusing it.
Upvotes: 2
Reputation: 1659
ImageIcon(URL)
Here is a small working example. In this case the image resides in src/resources.
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class ImageViewer {
private final static String TITLE_IMAGE = "/resource/myimage.png";
private final JFrame frame;
private BufferedImage titleImage;
private JLabel titleLabel;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ImageViewer();
}
});
}
public ImageViewer() {
frame = new JFrame();
init();
}
private void init() {
frame.setVisible(true);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try {
titleImage = ImageIO.read(getClass().getResource(TITLE_IMAGE));
titleLabel = new JLabel(new ImageIcon(titleImage));
frame.add(titleLabel);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Reputation: 3649
Don't use the swing part of the code below. That is crappy. Just use the way to load the image and it should work in your jar.
public class Test extends JPanel{
static BufferedImage image = null;
public static void main(String[] args) throws IOException {
image = ImageIO.read(Test.class.getClassLoader().getResourceAsStream("images/g.png"));
Test clz = new Test();
JFrame frame = new JFrame();
frame.add(clz);
frame.pack();
frame.setVisible(true);
}
@Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
}
}
Upvotes: 0