MrFluffy
MrFluffy

Reputation: 3

Java program in netbeans won't run properly but works perfectly fine when debugging

Good evening, I've been programing a snake game the last few days using netbeans. Everything was perfectly fine until today when I decided to put in a JMenuBar so the user can set difficulty etc. This also worked fine but when I put the listeners for the MenuItems the program would only show an empty frame when running the project(F6). But when I click debug project(ctrl + f5) the game starts normaly. The run button also works when I remove the listeners. Any idea why this is that way? Main class:

    package test1;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.logging.Level;
    import java.util.logging.Logger;



    public class Test1 extends JPanel implements KeyListener, Runnable{

    private JFrame frame;
    private Schlange snake;
    private Apfel apfel;
    private boolean up;
    private boolean left;
    private boolean right;
    private boolean down;
    private int bounds;
    private boolean beschleunigen;
    private int level;
    private JMenu menu;
    private JMenu sub1;
    private JMenu sub2;
    private JMenuItem restart;
    private JMenuItem schw1, schw2, schw3, schw4, schw5;
    private JMenuBar menuBar;
    private boolean running;
    public Test1(){
    super();       
    init();
    }

    public static void main(String[] args) {

        Test1 t1 = new Test1();
    }

    private void init() {
        bounds = 300;
        //Frame
        frame = new JFrame();
        frame.setSize(bounds,bounds);
        frame.setTitle("Snake by Fluffy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100,100);
        frame.setVisible(true);
        frame.setFocusable(true);
        frame.setResizable(false);
        frame.addKeyListener(this);

        //Spiel
        snake = new Schlange(5, this);
        apfel = new Apfel(frame);
        up = false;
        left = false;
        right = false;
        down = false;
        apfel.getRnd(snake);
        level = 160;
        beschleunigen = false;
        running = true;

        //Menue
        sub1 = new JMenu("Schwierigkeit"); 
        sub2 = new JMenu("Spiel");
        schw1 = new JMenuItem("Leicht");
        schw2 = new JMenuItem("Mittel");
        schw3 = new JMenuItem("Schwer");
        schw4 = new JMenuItem("Irre");
        schw5 = new JMenuItem("Beschleunigung");
        restart = new JMenuItem("Neustart");



        //Menue Listener
        schw1.addActionListener(new ActionListener() {
            @Override           
            public void actionPerformed(ActionEvent e) {
                level = 200;
            }
        });

        schw2.addActionListener(new ActionListener() {
            @Override           
            public void actionPerformed(ActionEvent e) {
                level = 160;
            }
        });

        schw3.addActionListener(new ActionListener() {
            @Override           
            public void actionPerformed(ActionEvent e) {
                level = 120;
            }
        });

        schw4.addActionListener(new ActionListener() {
            @Override           
            public void actionPerformed(ActionEvent e) {
                level = 80;
            }
        });

        schw5.addActionListener(new ActionListener() {
            @Override           
            public void actionPerformed(ActionEvent e) {
                beschleunigen = true;
            }
        });

        restart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                restart();
            }
        });



        sub1.add(schw1);
        sub1.add(schw2);
        sub1.add(schw3);
        sub1.add(schw4);
        sub1.add(schw5);    
        sub2.add(restart);
        menuBar = new JMenuBar();
        menu = new JMenu("Optionen");
        menu.add(sub1);        
        //menu.add(restart);
        menuBar.add(sub2);
        menuBar.add(menu);      

        //Ende
        frame.setJMenuBar(menuBar);
        this.setBackground(Color.green);
        frame.add(this);       
        run();      
    }

    @Override
    public void paintComponent(Graphics gr) {
        super.paintComponent(gr);   
        apfel.paintComponent(gr);
        if(snake.collision()) {
            gr.drawString("GAME OVER", 200, 200);
            running = false;
        }
        snake.paintComponent(gr);

    }

    @Override
    public void keyTyped(KeyEvent e) {        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case 37:
                if(right!=true){
                left = true;
                up = false;
                down = false;
                right = false;
                }
                break;                
            case 38:
                if(down!=true){
                up = true;
                down = false;
                right = false;
                left = false;
                }
                break;               
            case 39:
                if(left!=true){
                right = true;
                left = false;
                up = false;
                down = false;
                }
                break;
            case 40:
                if(up!=true){
                down = true;
                up = false;
                right = false;
                left = false;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {        
        while (running) {
            if(up) snake.move("up");
            if(down) snake.move("down");
            if(left) snake.move("left");
            if(right) snake.move("right");
            repaint();
            if(snake.collisionApfel(apfel.getX(), apfel.getY())) {
                apfel.getRnd(snake);
                if(level > 20 && beschleunigen) level -= 10;
            }
            try {
                Thread.sleep(level);
            } catch (InterruptedException ex) {
                Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public Apfel getApfel(){
        return apfel;
    }

    public void restart(){
        snake = null;
        apfel = null;      
        snake = new Schlange(5, this);
        apfel = new Apfel(frame);
        repaint();
        up = false;
        left = false;
        right = false;
        down = false;
        apfel.getRnd(snake);
        level = 160;
        beschleunigen = false;
        running = true;
    }


}

If needed I can also post the other classes but they shouldn't be the problem... Some of the code is in german which I hope is no problem either :D

Upvotes: 0

Views: 128

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Take frame.setVisible(true); and place it at the end of the init method. You have a race condition between the frame been realized on the screen and the EDT

Upvotes: 2

Related Questions