Reventon Szt wrote:
From what I know, the template of a simple applet consist of init(), start(), stop(), destroy(), but can I add in my own methods? If so, how do I call them? and which method would be responsible for controlling all the methods?
Reventon Szt wrote:
So where would my components be stored if I just did add(JComponent)? Directly into the JApplet?
Reventon Szt wrote:At the moment, I am trying to write the game's main menu which involves two JButton, a JLabel and a JPanel all added into a main JPanel which I had set it as the ContentPane.
When the user clicks one of the buttons, I would like to "clear out" the entire main JPanel, only keeping the background if possible, then adding other components into the same main JPanel.
Let's start with a question for you: when you say "standalone programs," I take it from the context that you mean programs that have no GUI and only take their input from the command line? In that case, then the simple answer to your first question -- "how can one break the program down into methods?" -- is: "the same way you would in a non-GUI program."
Out of curiosity, what was your Tic-Tac-Toe game like? I ask because my gut tells me it was probably very procedural in nature and contained only one class. I feel like making the leap from that to an applet would be a big one, because some of your confusion seems to be related to how objects will interact in your game.
Your methods would be invoked by the applet through the four methods you named. I haven't written an applet in a long time, so it's probably best to consult the tutorials on what those methods should do. I think start() is probably the one you care most about, and it would be responsible for setting up the game board and initializing the player scores to zero, or something like that. Don't quote me on that ;)
I'm not really clear on the rest of the post... I can say that setContentPane(main) is needed because every component must be in a container. main is the container you have added everything to, so you want to set that as the content pane (the panel that you see in the window) of your applet.
What isn't working? Is your ActionListener triggered and the screen just doesn't want to repaint?
Paul Clapham wrote:
Reventon Szt wrote:At the moment, I am trying to write the game's main menu which involves two JButton, a JLabel and a JPanel all added into a main JPanel which I had set it as the ContentPane.
When the user clicks one of the buttons, I would like to "clear out" the entire main JPanel, only keeping the background if possible, then adding other components into the same main JPanel.
Typical beginner idea, which beginners can't make it work. Removing and adding components is hard to do, not to mention that in real applications you don't see it being done. So try not to do that; if you want to change the displayed view from one panel to another, the usual way to do that is to set up two panels and to put them in a layout which allows you to make one or the other visible at the right time.
Reventon Si wrote:
The screen does not repaint after removing components from the main panel I suppose? As you can see in my code, I don't have a paint method yet, so I don't see the point of adding repaint(); or main.repaint(); in my code. But I did try that, and none of them worked. However, one thing I can assure is that the ActionListener is triggered properly as I had tested them. Regarding to repaint(); (sorry for asking so much :P), is the paint() method constantly running in an applet? even if I don't have it coded? And what does the parameter (Graphics g) mean? Is there an equivalent component in Swing?
You may not have implemented a paint() method anywhere, but the JComponents you are using have such a method. When you call repaint(), a request to repaint all the components in your applet is made and the repaint is scheduled on the Event Dispatch Thread (EDT). This thread is where all GUI code is executed. All of your components will be visited and their paint() methods will be called. The paint() method is not invoked constantly; it only gets called when scheduled. This can happen when you call repaint(), or if the window is "damaged" in any way (e.g. you minimize, maximize, move another window over top and of again, etc.)
The Graphics object is your context for drawing. It gives you all the tools you need to draw on the component that had its paint() method called. Graphics is used for legacy reasons; there's a newer Graphics2D object that you should prefer to use. Note that this only applies if you need to write your own paint() method. If you do find the need for it, you can simply cast the Graphics object as a Graphics2D object before using it. For more information on how to do your own drawing, search for "java 2d." There are many resources available.
Do not set lab on fire. Or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|