File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Questions regarding to using applets and JComponents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Questions regarding to using applets and JComponents" Watch "Questions regarding to using applets and JComponents" New topic
Author

Questions regarding to using applets and JComponents

Reventon Si
Greenhorn

Joined: Jan 06, 2012
Posts: 4
Hello, before I go on, please kindly acknowledge that this is my fourth month of Java programming and that there is still a lot of codes and terms which I am not familiar of. So please kindly avoid complicated codes if it is possible. If not, please do try to explain your coding

I am trying to turn my Tic-Tac-Toe game from a standalone java program (Not an application; the kind that is written in one class and various methods) to an applet (either AWT or Swing is fine; though I would prefer Swing as I can add an icon to JButton, wheras a Button cannot) For the past months, I had only been working with standalone programs, so I do not really understand the fundaments of applets... In a standalone, I understand that the progam can be broken down into methods so it is more efficient and easier to debug, but in an applet, how can one break the program down into methods? 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? It would be kindly appreciated if you can explain to me on these questions.

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.

I searched around the net and found that for a JFrame (an application) the code JPanel.removeAll() should remove the component from the container, then all I have to do is repaint() and revalidate(). But this method is not working for me in a JApplet. And I was not able to add other JComponents after setContentPane(main);

Also, I don't understand the significance of setContentPane(main), but without this line of code, nothing would show up at all. I also tried running this applet without adding my components into the main JPanel and it showed up fine, though I read from this Using Top-Level Containers that GUI Components must be stored in a container for them to show up? So where would my components be stored if I just did add(JComponent)? Directly into the JApplet?

Below is what I came up with so far:



I am sorry if my question is too confusing and broad, I will try to clear things up if you have no idea what I am talking about.

Regardless, your help and advice is much appreciated! Thank you in advance!
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

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.

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?

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.

Reventon Szt wrote:
So where would my components be stored if I just did add(JComponent)? Directly into the JApplet?

If you just did an add(), I believe that is a shortcut to getContentPane().add().

What isn't working? Is your ActionListener triggered and the screen just doesn't want to repaint?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Greenhorn

Joined: Jan 06, 2012
Posts: 4

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."


Thanks so much for replying Jason! I was beginning to lose hope on creating this applet.
Anyways, you are absolutely correct indeed. By standalone, I do mean programs with no GUI and only command line for input. And yes my Tic-Tac-Toe game only has one class.


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.

For the applet though, I am thinking on making a game menu as the first screen which will let the user choose if they would like to play 1 Player or 2 Players, then after the click, it would bring them to the next screen which will let them choose game mode, after choosing game mode, an array of buttons would be shown which would be my game board. I guess its the transition between game menu after clicking the button is what I need help on. By removing all components on screen upon click is essentially what I am trying to achieve.


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 did read a lot of tutorials online for the past few days, most were too complicated with terms and codes that I had no idea what was going on. As you know, I had only been working with very procedural programs.


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.


I see. So when I set my main panel set the content pane, would that prevent me from removing components (such as JButtons, JLabels, etc) from the main panel?

What isn't working? Is your ActionListener triggered and the screen just doesn't want to repaint?

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?

If there is anything unclear, please tell me and I would gladly clarify them. Thanks so much once again!
Reventon Si
Greenhorn

Joined: Jan 06, 2012
Posts: 4
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.


Thanks for reply Paul!

I tried doing two panels, but when I use setVisible(false); on the main Panel, it does not "hide" and I was able to make the other panel show.

Would you kindly show me a way to do that?

Thanks once again!
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

Take a look at CardLayout. That might do what you need. Each "game screen" would be its own panel, and you could switch between them based on button presses.
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.
Reventon Si
Greenhorn

Joined: Jan 06, 2012
Posts: 4
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.


I looked up your link and indeed it seems like it is what I was looking for. But for some reason, the main JPanel just would not allow me to switch to my other panel. Just out of curiousity, does setContentPaine(...) permanately sets the contents on screen, thus prevents me from refreshing/removing it?

Anyhow, I manage to achieve doing a menu by using a JFrame in my applet, JFrame seems to be easier to work with for me. However I am now faced with another problem. Upon user click on a JButton, I have an actionListener which would change a value of a variable that is responsible for creating a 2 dimensional JButton array (Tic-Tac-Toe grid). (The idea is to wait for the user input then draw the grid, as I intend to allow the user to choose the grid size)

So I would like the user input (via JButton click) be taken first before the applet draws the array of JButton. The problem is, if I initialize that variable with a value, the applet would just go on and draw the grid for me before waiting for the user input. I tried putting the part of my code where it draws the grid inside the actionListener then call the repaint() method, but the same occurred and the applet refuse to "refresh" itself.

Just to clarify, by drawing, I do mean "adding the JButton components into the JPanel main, then setting the JPanel main as the Content Pane".

Jason, do you know of any ways to tackle this problem?
Sorry I know I am asking a lot, but I really do appreciate your kindness and helpfulness into assisting others with their problems. I hereby sincerely thank you!
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

Using a JFrame within your applet doesn't seem necessary to me. Both the JFrame and the JApplet are top level containers, and you only ever need one top level container for your application. I don't know if you've gone through this tutorial yet, but it might be a good place to start. Step back a bit from your current game and try following along with the examples given in this trail. There should be a way to get all of your panels in there, perhaps by setting the JApplet's content pane to have CardLayout and then adding your various panels to that.

I'm not sure I entirely understand your problem with the buttons. You say the buttons are being drawn in the grid before waiting for user input... how are you getting user input? When you mentioned buttons, I assumed that each of the nine grid squares is itself a button, blank to begin with, that the user can click on. When the user clicks the button, the button updates to show the user's symbol (X or O), and maybe deactivates so it can't be clicked again. If that's the case, then I think you can just create nine buttons in a 3 x 3 GridLayout with an empty name, and set their ActionListener to call setText() with the player's symbol, followed by setEnabled(false). As an alternative, you could not use buttons at all and create your own JComponent, defining your own paintComponent() method to do the rendering, as well as your own field for tracking which player "owns" that space.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Questions regarding to using applets and JComponents