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 8657865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:37:38+00:00 2026-06-12T15:37:38+00:00

The following is the condensed code of my applet game: import java.applet.Applet; import java.awt.event.*;

  • 0

The following is the condensed code of my applet game:

import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;

public class Game extends Applet implements KeyListener, Runnable {
    Button options = new Button("Options");
    Thread thread = new Thread(this);

    public void init() {
        addKeyListener(this);
        thread.start();
    }

    public void paint(Graphics graphics) {
        // draw stuff
    }

    public void run() {
        try {
            while (true) {
                thread.sleep(40);
                repaint();
            }
        } catch (InterruptedException exception) {}
    }

    public void keyPressed(KeyEvent keyEvent) {
        switch (keyEvent.getKeyCode()) {
        case KeyEvent.VK_ESCAPE:
            // pause game
            add(options);
        }
    }

    public void keyReleased(KeyEvent keyEvent) {}
    public void keyTyped(KeyEvent keyEvent) {}
}

My game runs as it’s expected to. However when the user presses Esc I want to pause the game and display an options button.

The problem is that when I press Esc it pauses the game as expected. However it doesn’t display the button on the screen. I’ve tried to search for a solution to no avail. What exactly is happening?

  • 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-12T15:37:39+00:00Added an answer on June 12, 2026 at 3:37 pm

    Works perfectly fine for me…

    public class TestApplet02 extends Applet implements KeyListener, Runnable {
    
        Button options = new Button("Options");
        Thread thread = new Thread(this);
        int y = 0;
    
        public void init() {
            thread.start();
        }
    
        @Override
        public void start() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    setLayout(new BorderLayout());
                    addKeyListener(TestApplet02.this);
                }
            });
        }
    
        public void paint(Graphics graphics) {
            super.paint(graphics);
            Graphics2D g2d = (Graphics2D) graphics;
            y++;
            if (y > getHeight()) {
                y = 0;
            }
            g2d.drawLine(0, y, getWidth(), y);
        }
    
        public void run() {
            try {
                while (true) {
                    thread.sleep(40);
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            repaint();
                        }
                    });
                }
            } catch (InterruptedException exception) {
            }
        }
    
        public void keyPressed(KeyEvent keyEvent) {
            switch (keyEvent.getKeyCode()) {
                case KeyEvent.VK_ESCAPE:
                    // pause game
                    add(options);
                    invalidate();
                    revalidate();
            }
        }
    
        public void keyReleased(KeyEvent keyEvent) {
        }
    
        public void keyTyped(KeyEvent keyEvent) {
        }
    }
    

    From the link TrashGod provided…

    In an applet, the GUI-creation task must be launched from the init
    method using invokeAndWait; otherwise, init may return before the GUI
    is created, which may cause problems for a web browser launching an
    applet. In any other kind of program, scheduling the GUI-creation task
    is usually the last thing the initial thread does, so it doesn’t
    matter whether it uses invokeLater or invokeAndWait.

    UPDATED

    The major issues I had were:

    In you’re escape key handler, if direction is 0, the pause option will never activate…

    case KeyEvent.VK_ESCAPE:
        direction = -direction;
    
        if (direction < 0) {
            add(options);
        } else {
            remove(options);
        }
    

    The other thing I had to was call revalidate…

    invalidate();
    revalidate();
    repaint();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Consider the following condensed code: /* Compile: gcc -pthread -m32 -ansi x.c */ #include
I have the following (condensed) silverlight xaml for a view / viewmodel: <UserControl x:Class=MyView>
I've managed to condense my confusion to the following code: template<typename T> class BTI
We have a lot of class code which has some boilerplate like the following:
The following code (condensed from a larger program) does not compile with clang or
I'd like to condence the following code into fewer calls to .replace() . It
I've got a fairly complex web app which I've condensed down to the following
Following code produces a nested array as a result for keys containing three items:
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to

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.