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?
Works perfectly fine for me…
From the link TrashGod provided…
UPDATED
The major issues I had were:
In you’re escape key handler, if direction is 0, the pause option will never activate…
The other thing I had to was call
revalidate…