1

I'm pretty new to Java (C# programmer) and I have here a problem that I dont understand. Sorry for the crappy english. =)

I'm working on a simple Snake game, I have different classes, enums etc.. My problem is, if I call the JFrame with the game from the Menu class, it doesnt do anything. I can debug through the code but it doesnt draw anything. If I start the Game class direct from the Main method, there is no problem.

I think that its a simple error and im just to stupid to see it.

Here the code:

Mouse Event:

else if (mouseX > 75 && mouseX < 175 && mouseY > 260
                && mouseY < 285) {
            startGame();

startGame method in the Menu class:

public void startGame(){
    this.setVisible(false);
    game = new Game();
}

Game Constructor:

public Game() {
    super("Snake V 0.1");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(940, 620);
    setLocation(200, 200);
    addKeyListener(keyListener);
    setVisible(true);
    //this.createBufferStrategy(2);

    zeichnung.addFigur(new Field(0, 0));
    snake = new Snake(40, 300, 1);
    zeichnung.addFigur(snake);
    zeichnung.addFigur(new Diamond());

    this.getContentPane().add(jpanel);

    this.repaint();

    this.Intervall();
}

Intervall:

public void Intervall() {
    while (true) {
        try {
            snake.direction = keyListener.getDirection();
            zeichnung.checkCollision(snake);
            snake.move();
            this.repaint();
            Thread.sleep(150);
        } catch (InterruptedException e) {
            // TODO: handle exception
        }
    }
}

As i said before. If I start the Game class direct from Main method (simple with Game game = new Game();) there is no problem.

I hope I dont forgot any important information.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Alias
  • 13
  • 3
  • are you forget to show the Jframe (game.show()) in main class?? – Alya'a Gamal Jan 05 '13 at 22:47
  • Its not necessary. From main Method it works without "game.show()": public static void main(String[] args) { Menu menu = new Menu(); Game game = new Game(); } This works perfect – Alias Jan 05 '13 at 23:06
  • so the problem is that you want to call Game class from Menu Class , alright ?? and you call Menu class in Main class then call Game class in Menu class , did you? – Alya'a Gamal Jan 05 '13 at 23:26
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jan 05 '13 at 23:31
  • Never use `Thread.sleep(..)` on EDT. See [here](http://stackoverflow.com/questions/14074329/using-sleep-for-a-single-thread/14074427#14074427) for more. also dont use `KeyListener` rather Swing `KeyBinding`s – David Kroukamp Jan 06 '13 at 08:43

1 Answers1

1

It shouldn't work at all in any Swing GUI.

Thread.sleep() kills repainting and listening.

see final post here for example of problem

http://www.coderanch.com/t/600642/GUI/java/update-getGraphics

use a Swing Timer instead

Michael Dunn
  • 818
  • 5
  • 3