Hi guys,
I have problem with displaying components in JPanel. After I run this code my window application freezes. I discovered that if I take method move from private class DrawingPanel and make it as a member of its parent class then this code work. But unfortunately I have to have this method inside DrawingPanel class. Thanks for any help

Code:

/*
* Game.java
*
* Created on 18 July 2009, 08:04
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/

myTarget.paintComponent(g);
myLauncher.paintComponent(g);
// TODO ray section to be uncommented
if (myRay != null)
{
myRay.paintComponent(g);
//check for collision here - if not in here, then no ray to check!
if (isContact())
{
System.out.println("hit it!-----------------------------------");
myRay = null;
myTarget = null;
//need to stop the game now else it crashes
System.exit(0);
}
}
move(g);//IF I COMMENT THIS LINE AND MAKE MOVE AS A MEMBER OF PARENT CLASS AND THEN IN MAIN I JUST CALL MOVE THIS WORKS
}

private final Color LAUNCHER_COLOUR = Color.BLACK; // colour
private final int LAUNCHER_HEIGHT, LAUNCHER_WIDTH; // width and height of the base of the launcher
private int x, y; // x and y position of the top left hand corner of the base of the launcher
private final int SIDE_MOVE; // amount of sideways movement

Welcome to the forum. One of your problems is that you have program logic in your paintComponent method, and this shouldn't be done as paint/paintComponent should concern itself with painting only. You should have a game engine that moves things, changes class variables, calls repaint, and then the paintCompnent uses those class variables to decide what to paint where.

Your next problem is that you're freezing the event dispatch thread or EDT, the single thread responsible for drawing the swing app and for interacting with the user, with a while (true) loop. Don't use while (true) (and don't use Thread.sleep(...) ) in the EDT. Instead, since this is a simple animation, use a Swing Timer.

I suggest you go to the Java tutorials and read up on use of Swing Timers. Also search this forum for their use with animations and you'll see many examples. Much luck!