Mam pytanie, dlaczego nie widać obrazka na panelu?? Obrazek umieściłem w tym samym katalogu co Example1.java. Kompiluję Eclipse. Po włączeniu programu zwraca mi wyjątek, przy wczytywaniu obrazka.
import java.awt.;
import java.awt.image.BufferedImage;
import javax.swing.;
import java.net.URL;
import javax.imageio.ImageIO;
public class Example1 extends Canvas
{
public Example1()
{
}
public void paint(Graphics graphics)
{
graphics.drawLine(2, 2, 200, 200);
BufferedImage img = l("wrog.gif");
graphics.drawImage(img, 140, 40, null);
}
public BufferedImage l(String sciezka)
{
URL url = null;
try{
url = getClass().getClassLoader().getResource("wrog.gif");
return ImageIO.read(url);
}catch(Exception e){
System.out.print("Nie wczytale sie obrazek" + sciezka + " " + url);
System.exit(0);
return null;
}
}
public static void main(String[] args)
{
Example1 canvas = new Example1();
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(canvas);
frame.setVisible(true);
}
}