Carregando imagem diretamente do resources

2 respostas
S

Galera, como faço pra carregar uma imagem q joguei numa pasta resource que está localizada na /src?

package teste
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Menu {


	public static void main(String[] args)
	{
		Menu chamando=new Menu();
		chamando.Chama();
		
	}
	
	public static void Chama(){
		BufferedImage image;
		
		
		

		
		JLabel label=new JLabel(new ImageIcon("C:\Users\Eduardo\Desktop\Aki.jpg"));
		label.setLocation(0,0);
		label.setSize(1024,700); 
		
		Font bigFont=new Font("secif",Font.BOLD,28);
		
		JButton button1=new JButton("Jogar! ");
		button1.setFont(bigFont);
		button1.setSize(130,50);
		button1.setLocation(200,360);
		button1.setBackground(Color.black);
		button1.setOpaque(false);
		button1.addActionListener(new Action_Button1());
		
		JButton button2=new JButton("Instruções ");
		button2.setFont(bigFont);
		button2.setSize(200,50);
		button2.setLocation(200,420);
		button2.setBackground(Color.black);
		button2.setOpaque(false);
		button2.addActionListener(new Action_Button2());
		
		JFrame frame=new JFrame();
		frame.add(button1);
		frame.add(button2);
		frame.add(label);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(1024,700));
		frame.pack();
		frame.setVisible(true);
	
}
	
	
	
	
	static class Action_Button1 implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			
			JFrame frame2=new JFrame("Jogo");
			JLabel label=new JLabel("Iniciará o jogo nesse frame");
			JPanel panel=new JPanel();
			frame2.setSize(300,300);
			frame2.add(panel);
			frame2.setVisible(true);
			panel.add(label);
		}
		
	}
	
	static class Action_Button2 implements ActionListener{
		
		public void actionPerformed(ActionEvent ex){
			
			
			JFrame frame3=new JFrame("Instruções");
			JLabel label=new JLabel("Aqui terão as instruções do jogo");
			JPanel panel=new JPanel();
			frame3.setSize(300,300);
			frame3.add(panel);
			frame3.setVisible(true);
			panel.add(label);
			
			
		}
	}
	

}

2 Respostas

davidbuzatto

Está no pacote default?

new ImageIcon( getClass().getResource( "/imagem.png" ) );

Está dentro do pacote projeto.imagens?

new ImageIcon( getClass().getResource( "/projeto/imagens/imagem.png" ) );

[]´s

S

getClass() dá erro aki…

EDIT: RESOLVIDO.

Obrigado

Criado 15 de novembro de 2010
Ultima resposta 15 de nov. de 2010
Respostas 2
Participantes 2