It's my first question there so please don't be so cruel .
I've got problem with my program , it means i don't know why i have got NullpointerException , anybody knows why ?
My code : `package ex2;
public class Screen extends JPanel {
private BufferedImage image ;
private Timer tm ;
private Graphics g ;
private static int index = 0 ;
private ArrayList<String> paths = new Test().getArrayList();
public Screen(){
System.out.println(paths);
String interval ;
interval = JOptionPane.showInputDialog("Please write time interval "
+ "between images in miliseconds");
tm = new Timer(Integer.parseInt(interval), new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
paint(index);
index += 1 ;
if (index >= paths.size())
System.exit(index);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
tm.start();
}
public void paint(int i) throws IOException{
image = ImageIO.read(new File(paths.get(i)));
g.drawImage(image, 40, 30, image.getWidth() , image.getHeight() , null);
}
}
private Graphics g ;
Looks null to me.
If you want to do custom painting then you need to override the paintComponent()
method of your JPanel
and use the Graphics object that is passed to the method.
Read the section from the Swing tutorial on Custom Painting for working examples that show the proper way to do painting.
Java slideshow without using ImageIcon
Why are you trying to do this without an ImageIcon
? Why reinvent the wheel?
Just read the Image and create an ImageIcon
and add the icon to a JLabel
and the label to the frame. Then you change the image using the setIcon(...)
method. The tutorial also has a section on How to Use Icons
to get you started.