0

I am sorry I am beginner in java. For my little adventure game that I am working on, I want to add an delay in my gameLoop actully for testing my graphics.

The problem is I have an loop where the things get printet/paintet on screen (call it how u want :D). When I do that usual I have no problems, but when I add a Thread.sleep (with throws or try&catch) I get an NullPointerExeption somewhere in my program wich has nothing to do with that part here is my code:

GUI CLASS:

package xxx.yyy.zzz;

import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUI extends JPanel {

private JFrame frame;
private Control ctr;

final int objectSize = 10;

public GUI() {
    frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(800,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.add(this);
    ctr = new Control(this);
}

public static void main(String[] args) {
    new GUI();
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    for(int x = 0; x < ctr.getScreenSizeX(); x = x + objectSize) {
        for(int y = 0; y < ctr.getScreenSizeY(); y = y + objectSize) {
            g.setColor(ctr.getScreen(x/objectSize, y/objectSize));
            g.drawRect(x, y, objectSize, objectSize);
            g.fillRect(x, y, objectSize, objectSize);
        }
    }

  }
}

CONTROL CLASS:

package xxx.yyy.zzz;

import java.awt.Color;

public class Control {

private GUI gui;
private Level l1;

private Color[][] screen;
private Player player;

private final int screenSizeX = 400;
private final int screenSizeY = 300;

public Control(GUI pGUI) {
    gui = pGUI;
    l1 = new Level(400, 30, true);
    screen = new Color[screenSizeX][screenSizeY];
    player = new Player(Color.WHITE);

    gameLoop();
}

private void gameLoop() {

    print(l1);

    for(int i = 0; i < 100; i++) {
        player.move(1);
        print(l1);

        try {
            Thread.sleep(100);                      //here is my problem!
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

private void print(Level pL) {
    if(pL.getSizeX() > 40 || pL.getSizeX() > 40) {
        for(int x = 0; x < 40; x++) {
            for(int y = 0; y < 30; y++) {
                screen[x][y] = pL.getObjectColor(x + (player.getX() - 10), 
y);
            }
        }
    }

    else {
        for(int x = 0; x < 40; x++) {
            for(int y = 0; y < 30; y++) {
                screen[x][y] = pL.getObjectColor(x, y);
            }
        }
    }
    gui.repaint();
}

public Color getScreen(int x, int y) {
    return screen[x][y];
}

public int getScreenSizeX() {
    return screenSizeX;
}

public int getScreenSizeY() {
    return screenSizeY;
}

}

I censored my package xD

Thank you for your replies, sorry I am not familiar with game programming and programming beginner!

EDIT: My Error Log:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at com.zeptosi.snogg.GUI.paintComponent(GUI.java:31) ...

Snogg
  • 9
  • 4

0 Answers0