I have a problem that I'm not able to understand, none the less solve.
I am making a clone of Asteroids (I call it Meteors, to be clever). Currently I have a class entitled MeteorGame
which is the chief class that does the GUI drawing. MeteorGame extends JFrame to make the window, and it has another class that it uses to interpret keyboard events. Everything works great but I need to scale this up. Basically, what I have created is one "level" of the game, and now I need a level manager. I call this class Meteors
and I want IT to handle creating the JFrame, rather than MeteorGame.
So to this effect, what I am trying to create is a system where Meteors class is basically a shell that creates a window and instantiates "levels" of MeteorGame in sequence. I have converted my MeteorGame class from JFrame to JPanel, so that my JFrame Meteors class adds JPanel components to itself as the user levels up.
I want each MeteorGame to draw itself and interpret keyboard event entirely autonomously, with the JFrame Meteors class simply working to queue up levels.
Many problems.
Is using the paintComponent() method the only way to draw to a JPanel? The way my old class works is it uses a constant
while
loop (managed for frame rate) to continuously call the update() and draw() methods that do all the work. So 40 times per second the method draws itself onto the JFrame.With my updates converting draw() to paintComponent() the frame is drawn only once, and then disappears. I need to find a way to keep redrawing the JPanel continuously. (to eliminate flicker I draw the panel by writing to an image and then drawing the image)
Is there a better way to do all of this? I'm moving from a background in Objective-C iOS development where I am much more familiar with the view hierarchy. I'm sure that what I'm doing is not the most ideal situation by any means.
Also, when I create a JButton in the JFrame class and try to draw it using the following code, nothing happens. What am I doing wrong?
JButton button = new JButton("Close");
button.setLocation(300, 300);
add(button);