Imagem nao aparece

galera o que está de errado no código q minha imagem não aparece?


public class GaleriaDeImagens extends JPanel {

private static final long serialVersionUID = 1L;
private BufferedImage imgBK;

public void imagens() {
	
	try {
		imgBK = ImageIO.read(getClass().getResource("IM/BK.jpg"));
		
	} catch (IOException e) {
	
		e.printStackTrace();
	}	
}	
public void paintComponente(Graphics g) {
	super.paintComponent(g);
	g.drawImage(imgBK, 10, 10, 100, 100, null);	
}

}

public class Janelas{

	private JFrame janela1 = new JFrame("Cadastro");
	private JFrame janela2 = new JFrame("Dados");
	private GaleriaDeImagens imgBK = new GaleriaDeImagens();
	private JButton enter;
	private JButton exit;
	
	public Janelas() {
		Janela_1();
		
	}
	
	public void Janela_1() {
		
		this.janela1.setSize(500,500);
	
		//
		this.janela1.add(imgBK);
		//
		componentes_j1();
		this.janela1.add(enter);
		//
		this.janela1.setLayout(null);
		this.janela1.setLocationRelativeTo(null);
		this.janela1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.janela1.setVisible(true);
		
		
		this.janela2.setSize(500,500);
		
		componentes_j2();
		this.janela2.add(exit);
		
		this.janela2.setLayout(null);
		this.janela2.setLocationRelativeTo(null);
		this.janela2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.janela2.setVisible(false);
	}
	
	public void componentes_j1() {
		enter= new JButton("Enter");
		enter.setBounds(10,10,80,50);
		enter.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			
				janela1.setVisible(false);
				janela2.setVisible(true);
				
			}
		});
		
	}
	public void componentes_j2() {
		exit= new JButton("Exit");
		exit.setBounds(10,10,80,50);
		exit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			
				janela1.setVisible(true);
				janela2.setVisible(false);				
			}
		});		
	}	
}

Essa linha está retornando a imagem ou retorna null?

nao entendo bem não usei o método return usei o local da imagem como parâmetro

Logo após a execução dessa linha:

imgBK = ImageIO.read(getClass().getResource("IM/BK.jpg"));

A variável imgBK está NULL?

nao…

package PL;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class GaleriaDeImagens extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private BufferedImage imgBK;
	
	public GaleriaDeImagens(){
	
	}
	
	public void imagens() {
		
		try {
			imgBK = ImageIO.read(getClass().getResource("/BK.jpg"));
			
		} catch (IOException e) {
		
			e.printStackTrace();
		}
		repaint();
			
	}
	
	public void paint(Graphics g) {
		super.paintComponent(g);
		g.drawImage(imgBK, 10, 10, 500, 500, null);		
	}
}

Essa sobrescrita está errada, o nome do método é:

paintComponent

Você digitou

paintComponente

Essa sobrescrita também está errada.
Em componentes Swing você deve sobrescrever o método paintComponent.
O método paint é para ser sobrescrito em componentes AWT.

o brother pior erei a escrita mesmo, mas código o q ta de errado agora no método? eu usei outro método e funcionou mas sou meio persistente quero ver o q erei nesse.

public class GaleriaDeImagens extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private BufferedImage imgBK;
	
	public GaleriaDeImagens(){
		imagens();
	}
	
	public void imagens() {
		
		try {
			imgBK = ImageIO.read(getClass().getResource("/BK.jpg"));
			
		} catch (IOException e) {
		
			e.printStackTrace();
		}
		repaint();
			
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(imgBK, 10, 10, 100, 100,null);
		
	}

}

Provavelmente você deve apagar a chamada ao super.paintComponent, pois você não quer o comportamento padrão do JPanel ao se desenhar.
Acredito que no construtor da sua classe deva chamar o método setOpaque(true) pois por padrão os JPanel são transparentes.

deu não ainda esse setOpaque(true) não consegui encaixar. :frowning:
q borogodo zica

Posta seu código.

public class GaleriaDeImagens extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private BufferedImage imgBK;
	
	public GaleriaDeImagens(){
		imagens();
	}
	
	public void imagens() {
		
		try {
			imgBK = ImageIO.read(getClass().getResource("/BK.jpg"));
			
		} catch (IOException e) {
		
			e.printStackTrace();
		}
		repaint();
			
	}
	
	public void paintComponent(Graphics g) {
		
		g.drawImage(imgBK, 10, 10, 100, 100,null);	
	}
}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Janelas{

	private JFrame janela1 = new JFrame("Cadastro");
	private JFrame janela2 = new JFrame("Dados");
	private GaleriaDeImagens imgBK = new GaleriaDeImagens();
	private JButton enter;
	private JButton exit;
	
	public Janelas() {
		
		Janela_1();
		
	}
	
	public void Janela_1() {
		
		this.janela1.setSize(500,500);
	
		
		
		this.janela1.add(imgBK);
		//
		componentes_j1();
		this.janela1.add(enter);
		//
		this.janela1.setLayout(null);
		this.janela1.setLocationRelativeTo(null);
		this.janela1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.janela1.setVisible(true);
		
	
		this.janela2.setSize(500,500);
		
		componentes_j2();
		this.janela2.add(exit);
		this.janela2.setLayout(null);
		this.janela2.setLocationRelativeTo(null);
		this.janela2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.janela2.setVisible(false);
	}
	
	public void componentes_j1() {
		enter= new JButton("Enter");
		enter.setBounds(10,10,80,50);
		enter.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			
				janela1.setVisible(false);
				janela2.setVisible(true);
				
			}
		});
		
	}
	public void componentes_j2() {
		exit= new JButton("Exit");
		exit.setBounds(10,10,80,50);
		exit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			
				janela1.setVisible(true);
				janela2.setVisible(false);			
			}
		});	
	}
}

Como eu havia dito, chame esse método no construtor da sua classe:

public class GaleriaDeImagens extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private BufferedImage imgBK;
	
	public GaleriaDeImagens(){
		setOpaque(true);
		try {
			imgBK = ImageIO.read(getClass().getResourceAsStream("/BK.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	public void paintComponent(Graphics g) {
		g.drawImage(imgBK, 10, 10, 100, 100, null);	
	}
}

boa noite brother cheguei agora fui olha o programa e agora por algum motivo ele está dando erro na linha …
imgBK = ImageIO.read(getClass().getResource("/BK.jpg"));
erro que não tinha antes. bom eu fiz de outra maneira e que funcionou mas to com outra duvida…

to querendo tratar essa imagem como um botão, sendo assim quando eu clica-se ou passasse o mouse por cima ela muda-se de imagem…
uso o gets e o seters para isso?

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Imagem extends JPanel {
	
	public ImageIcon imI= new ImageIcon(getClass().getResource("/BK.jpg"));
	public ImageIcon imI1= new ImageIcon(getClass().getResource("/BK1.jpg"));
	//public ImageIcon imI2= new ImageIcon(getClass().getResource("/BK.jpg2"));
	public Image im = imI.getImage();
	public Image im1 = imI1.getImage();
	

	
	public Image getIm1() {
		return im1;
	}


	public void setIm1(Image im1) {
		this.im1 = im1;
	}


	public Image getIm() {
		return im;
	}


	public void setIm(Image im) {
		this.im = im;
	}


	public void paintComponent(Graphics g) {
		//super.paintComponent(g);
		g.drawImage(im, 200, 200,80,80, this);
		
	}


	public ImageIcon getImI1() {
		return imI1;
	}

 void setImI1(ImageIcon imI1) {
		this.imI1 = imI1;
	}	
}
public class Principal  {

	JFrame j1 = new JFrame();
	Imagem im = new Imagem();
	
	public Principal() {
		janelas();
	}

	public void janelas() {

		

		j1.setSize(500, 500);
		// j1.setLayout(null);
		j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		j1.setLocationRelativeTo(j1);
		//
		j1.add(im);
		//
		j1.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent e) {
				
				im.setIm(im.getIm1());
			}
			public void mousePressed(MouseEvent e) {
			
				
			}
			public void mouseReleased(MouseEvent e) {
			
				
			}
			public void mouseEntered(MouseEvent e) {
			
				
			}
			public void mouseExited(MouseEvent e) {
			
				
			}
			
		});
		
		//
		j1.setVisible(true);

	}

	public static void main(String[] args) {

		new Principal();
	}

}