I am making a game and it should draw rectangles, so I created a method that draws rectangles. But I don't want just one rectangle, but many. So in a for-loop I try to call the paint method I created. And that is where it gives a NullPointerException
.
Method:
public void paint(Graphics g, int i) {
super.paint(g);
g.drawRect(i * 30, 0, 30, 30);
}
for-loop:
for(int i = 0; i < (ScreenSize.screenwidth); i++) {
paint(null, i);
}
Whole class:
public class World extends JPanel {
public void World() {
// Venster
JFrame World = new JFrame("World");
World.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
World.setUndecorated(true);
World.setLayout(null);
// Objecten aanmaken
JPanel panel = new JPanel();
// Objecten toevoegen
World.add(panel);
// Teken vierkantjes
for(int i = 0; i < (ScreenSize.screenwidth); i++) {
paint(null, i);
}
World.setVisible(true);
// Fullscreen, moet als laatste!
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(World);
}
// Functie om vierkantjes te tekenen
public void paint(Graphics g, int i) {
super.paint(g);
g.drawRect(i * 30, 0, 30, 30);
}
}