Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8744565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:51:38+00:00 2026-06-13T11:51:38+00:00

I am writing a game for a CS class. There is a GUI MyDisplay

  • 0

I am writing a game for a CS class.

There is a GUI MyDisplay which is an extended KeyListener with a JFrame with a packed JPanel[][] for an instance variable.

MyGame has a private enum called GameState which is either MENU, PLAY, QUIT, or PAUSE. MyGame has the static void main method which has the following construct:

MyGame is as follows:

public class MyGame
{
    /* Private instance fields and constructor left out.  
    Constructor initializes a MyDisplay and state = MENU */

    public static void main(String[] args)
    {
        MyGame g = new MyGame();
        while(true)
        {
            gstate = g.getState();
            if(gstate == GameState.PLAY)
                g.play();
            else if(gstate == GameState.PAUSE)
                g.pause();
            else if(gstate == GameState.MENU)
                g.menu();
        }
    }

    /* implementation of MyGame methods */
}

Where play, pause, and menu invoke various calls to MyDisplay to draw new stuff. The only way the state can change is if the user presses enter. Then, MyDisplay calls a MyGame method called enterPressed which changes the state accordingly.

When I compile this, the game does not behave normally. When I press enter (and print out the state to check), I notice, as it should, that MENU changes to PLAY. But even though the state is in PLAY, g.play() is not being evaluated (I tested whether or not the console printed out a test phrase in an added line of code in play()).

But what really confuses me is that when I enter any call to System.out, such as System.out.print(""); in the while loop of main, everything works fine, and g.play() gets called as it should.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:51:38+00:00Added an answer on June 13, 2026 at 11:51 am

    You are hitting a thread starvation case. Because your initial state is MENU the loop consumes all the cpu, and the UI doesn’t have the chance to consume your key press event.
    Try with a small sleep in your while, like: sleep(10)

    Here you will find more informations and a lot of alternatives, even a Timer is much more CPU friendly, but there are better ways, depends on your game logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a game which uses opengles. I have created my renderer class
I'm writing a game which uses a border layout with a JPanel using BorderLayout.CENTER.
In a game that I am writing, I use a 2D vector class which
I am writing a small Android game using a SurfaceView class. The outline of
I'm writing a console-environment Hangman game for my introductory programming class. The player chooses
I am writing a game in python in which I must periodically pull a
I'm writing a game in C using the Gtk libraries, in which the player
I am working on a game and I am writing an Entity base class.
I'm writing an image class for a game I'm working on and some images
I am writing simple game on flex. In the game there are 2 objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.