Reputation: 1
I have a Applet game, and it consists of a Applet class for the game, and a regular drawpanel
class for the graphics. I wanted to use a third class to have a main menu, that had several JButton
s to direct to other class I want to make, but more importantly to open the Applet
game class. Does the Menu
Class have to be an Applet
? I wouldn't think it would, but if not, how could I use a line of code similar to
Pacman g = new Pacman();
To open a Applet class?
Thank You!
(I don't have many years of programming under my belt, as I am just a tenth grade student in AP Computer Science)
Upvotes: 0
Views: 122
Reputation: 208944
"Does the Menu Class have to be an Applet?"
No, just use a CardLayout
. Put another JPanel
class in the Applet
class. So you have your DrawPanel
and MenuPanel
. See How to Use CardLayout
public class MyApplet extends JApplet {
DrawPanel drawPanel = new DrawPanel();
MenuPanel menuPanel = new MenuPanel();
class DrawPanel extends JPanel {
}
class MenuPanel extends JPanel {
}
}
See the running example from How do I Switch JPanels inside a JFrame for a simple implementation of CardLayout
. The basic concept is to layer the JPanel
on top of one another. And you can use a button click to show a different panel. So you would start with with MenuPanel
, then a button on that panel can cause the CardLayout
to show the DrawPanel
Side Note
Have a look at Why CS teachers should stop teaching Java applets and suggest it as a good read for your teacher :)
Use Java naming convention. Clas names begin with capital letters using camel casing, e.g. DrawPanel
and not drawpanel
Upvotes: 2