Help with 2D animation and threads

Hi.

I am working on a simple arcade game in Java.

I know C++, and some Java, and I also have a basic understanding of Swing and AWT, and I would like some advice on how to use animation in swing.

As I understand it, I shouldn’t use the EDT for drawing, but should use an external thread and have it call repaint() every so often, but I have been having some problems with doing that.
The animation seems pretty smooth, but the JPanel that my game is contained in isn’t catching most of my mouseEvents, I believe this may be because I am somehow causing problems with the EDT.

Here is the pseudocode of my game:



public class MyGame extends JPanel implements Runnable {
   public MyGame {
      addMouseListener(new L);
   }

   run() {
      doInput();
      doAI();
      repaint();
      Thread.sleep(100);
   }

   public void paint(Graphics g) {
      super.paint();
      paintThings(g);
   }

   private class L extends MouseAdapter {
      public void mouseClicked(MouseEvent e) {
         //register input
      }
   }
}


I am sure this has already been covered, if it has, can someone please point me to a good tutorial on this kind of thing?

Well my first advice is if you are doing an arcade game, forget about Swing. And forget abput multiple threads. Neither is done in classic game programming and both will bring you grief.

For rendering you want to either go to active rendering and AWT BufferStrategies or go around AWT entirely and use one of the OpenGL APIs-- either JOGL or LWJGL.

As for the game structure, games are near real-time programs. What they are most similar to is embedded systems programming. Your game should have one lsrge loop where each time around the loop is processing for one frame. In psuedocode it looks something like this:

WHILE GAME RUNNING
Poll Input Devices.
Update Player(s)
Update AI
Calculate game results
Render the frame
END WHILE