Search code examples
javaappletstateplayback

Java Game Applet freezes on first frame


I have been working on a basic Applet (not JApplet, I don't know why my instructor told me to use AWT...). It's a simple game that has a ball bouncing across the screen, collecting dots. I recently added a menu, and it seems to be all working ok, but when I press "play" the game starts, and freezes on the first frame. I will paste a skeleton of the code below, cutting out what I assume to be irrelevant to the question. Please forgive my Syntax and perhaps ignorance as I have only been programming for a week or two.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.event.MouseEvent;

public class StartingPoint extends Applet implements Runnable
{
    private Graphics doubleG;
    Ball b;
    boolean gameOver = false;
    Menu menu = new Menu();
    private enum STATE
    {
        MENU,
        GAME,
    };

    public static STATE State =STATE.MENU;
    //  more irrelevant variables below. will comment "blah blah" when cut has been made.


    @Override
    public void init() 
    {        
        setSize(800,600);
        //  blah blah
    }


    @Override
    public void start() 
    {
        b = new Ball();
        //  blah blah
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() 
    {
        if(State == STATE.GAME)
        {
            while(true)
            {
                b.update(this);
                // blah blah
                repaint();
                try 
                {
                    Thread.sleep(17);
                } 
                catch (InterruptedException e) 
                {
                    e.printStackTrace();
                }
            }
        }
        else if(State == STATE.MENU)
        {
            while(true)
            {
                repaint();
                try 
                {
                    Thread.sleep(17);
                } 
                catch (InterruptedException e) 
                {
                    e.printStackTrace();
                }
            }
        }
    }

    public void update(Graphics g) 
    {
        if(i == null)
        {
            i = createImage(this.getSize().width, this.getSize().height);
            doubleG = i.getGraphics();
        }
        doubleG.setColor(getBackground());
        doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
        doubleG.setColor(getForeground());
        paint(doubleG);

        g.drawImage(i, 0, 0, this);
    }

    public void paint (Graphics g)
    {
        if(State == STATE.GAME)
        {
            b.paint(g);
            //blah blah
        }
        else if(State ==STATE.MENU)
        {
            menu.render(g);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) 
    {
        if(mouseIn)
        { //mouseIn is a boolean that checks if Try Again is clicked on lose
            if(b.getScore() == -1)
            {
                b = null;
                b = new Ball();
                b.setScore(0);
            }
            if(State == STATE.MENU)
            {
                if(menu.isPlayb() == true)
                { //playb=boolean to check if "play" is clicked on menu.
                    State = STATE.GAME;
                    menu.setPlayb(false);
                }   

            }

        }
    }
}

It's still quite long sorry, I tried to cut as much as possible. Could someone please give me a hand here? It would be much appreciated.

Here is some more info about the way the program behaves:

If the initial state is set to game, the applet runs just as it did without a menu.

There is no error given when the game pauses after the play button is pushed.

I have a guess that the problem lies in that after the play button sets the state to STATE.GAME, the beginning of the code sets it back to menu again. No idea how accurate of a guess this is though, as I have tried many ways of avoiding writing STATE.MENU at the beginning of the code, to no different result. (I tried booleans, and If/else).

Please help if you can! Thanks so much.

Also, if you need any more info, I will provide :)


Solution

  • Thanks for trying to help everyone! After all the effort of attempting to solve the problem, the answer was simple. I just needed to run my start method again when the play button is pushed....

    Thanks :)