Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In a AWT/Swing (or about any GUI) program (which includes applets), you have to decouple the program logic (i.e. your moving between the rooms) from the painting.</p> <p>This means, your <code>keyPress</code> and similar event-driven methods should (maybe indirectly by calling other methods) change some internal state of the program (i.e. in which room I'm now), then call <code>repaint()</code>, and be finished.</p> <p>Your <code>paint</code> method should look at the state, paint the applet according to this state, and then be finished.</p> <p>What you are doing is recursively invoking some method from itself without a recursion-end condition, and this causes the StackOverflowError (which also is the eponym for this web site) you observed.</p> <hr> <p>So, here is an example applet reacting on user input:</p> <pre><code>class ExampleApplet extends JApplet { /** our game state. */ private int colorCode = 0; /** * This method will be called by the applet environment on loading the applet. */ public void init() { EventQueue.invokeLater(new Runnable() { public void run() { JApplet app = ExampleApplet.this; app.setContentPane(new Painter()); app.addKeyListener(new Listener()); }}); } /** * An object of this class is registered as a key listener for our applet. * It changes the game state whenever a key is typed, and then invokes repaint(). */ class Listener extends KeyAdapter { public void keyTyped(KeyEvent e) { colorCode ++; repaint(); } } /** This class is the contentPane of the applet, and responsible for the painting. */ class Painter extends JPanel { private Color chooseColor() { switch(colorCode % 11) { case 0: return Color.WHITE; case 1: return Color.BLUE; case 2: return Color.YELLOW; case 3: return Color.RED; case 4: return Color.CYAN; case 5: return Color.LIGHT_GRAY; case 6: return Color.MAGENTA; case 7: return Color.GREEN; case 8: return Color.ORANGE; case 9: return Color.PINK; case 10: return Color.BLACK; } throw new IllegalStateException("colorCode is negative: " + colorCode); } /** * Paints the component dependent on the current state of the game. */ public void paintComponent(Graphics g) { g.setColor(chooseColor()); g.fillRectangle(0,0, getHeight(), getWidth()); } } } </code></pre> <p>Of course, in your game you would not want to paint a rectangle in some color, but you would call the right painting method for the room you are currently in. Your internal state would include the room, and it would be changed to one of the other rooms when the right key is pressed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload