Results 1 to 5 of 5
Thread: Problem with Applets
- 10-29-2011, 09:30 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
Problem with Applets
Hi I am trying to learn java applets.. trying to create a square puzzle in an applet. Here is the code I have written so far..
Essentially, I have created a grid layout of 4 / 4. Added 15 buttons and a blank label into this grid. On clicking a button, check if the button has blankLabel as one of the neighbors - if so, swap them.Java Code:public class Puzzle extends Applet implements ActionListener { static final int size = 4; ArrayList<Component> Numbers = new ArrayList<Component>(size*size); Label blankLabel = new Label(); public void init() { Numbers.add(blankLabel); for(int i = 1 ; i<size*size; i++) { Button B = new Button(i + ""); B.addActionListener(this); Numbers.add(B); } Collections.shuffle(Numbers); DrawButtons(); } private void DrawButtons() { removeAll(); setLayout(new GridLayout(size,size)); for(int i = 0; i < Numbers.size(); i++) { add(Numbers.get(i)); } } @Override public void actionPerformed(ActionEvent e) { Component S = (Component) e.getSource(); int i = Numbers.indexOf(S); int[] check = new int[4]; check[0] = (i%size > 0) ? i-1 : -1; check[1] = (i%size < size-1) ? i + 1 : -1; check[2] = (i > size-1) ? (i-size) : -1; check[3] = (i < size*(size-1)) ? (i+size) : -1; System.out.println("clicked " + i); System.out.println("Checking for " + check[0] + " " + check[1] + " " + check[2] + " " + check[3] + " " ); for(int c : check) { if((c>0) && Numbers.get(c) == blankLabel){ System.out.println("Trying to swap position " + c + " and " + i); Numbers.set(c, S); Numbers.set(i, blankLabel); DrawButtons(); repaint(); break; } } } }
This works to the extent that the grid is displayed, on clicking, it identifies the correct button and tries to swap them correctly. The elements in the Numbers array are always correct. But, the display is never updated. It continues to show the initial layout.
Anything I am missing out here? Can someone please help me with this?
- 10-29-2011, 10:31 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Problem with Applets
Try changing the size of the applet and you should see the buttons swap as intended. (This assumes that you are using appletviewer or similar that lets you change the applet's size. In any case if you click to swap a pair and then click on another button adjacent to the blank you will see from the output that the swap is not attempted: it is as if the applet knows the real state of the games but is just not drawing properly).
The problem lies in the method that removes all the buttons from their container and them adds them again. In particular the add() method: "add(Numbers.get(i));".
Applet is a subclass of Container (a thing that contains components), and Container has a method validate() whose javadoc states:
"Validates this container and all of its subcomponents.
"The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed."
Long story, but the bottom line is that you should add "validate();" at the end of the method that removes the buttons and adds them again.
-----
Please use standard Java naming conventions: methods and variables start with a lower case letter. So, numbers and drawButtons(). And make the instance variables private unless you have some real need for them not to be. These things are just habits, but good habits.
Also consider using the more modern gui classes: JApplet, JButton, etc.Last edited by pbrockway2; 10-29-2011 at 10:33 AM.
- 10-29-2011, 02:04 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
Re: Problem with Applets
Thanks pbrockway2, for pointing out the errors in the my naming conventions.. Will be more careful in future.. Good to learn good habits early in life!
I used awt rather than swing components as per Herbert Schwildt's book... to gain a good understanding of awt .
Call to validate did resolve the issue! Thanks for your help with this !
-
Re: Problem with Applets
Use Swing, not AWT. I'm fearful that your book is hopelessly out of date.
- 10-30-2011, 01:48 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Similar Threads
-
Problem with applets!!
By mohsib in forum Java AppletsReplies: 3Last Post: 09-12-2011, 02:10 PM -
Help with embed problem with applets
By Beginner in forum New To JavaReplies: 0Last Post: 10-22-2010, 10:31 AM -
Linux java applets: printing problem
By dixan in forum Java AppletsReplies: 0Last Post: 01-28-2010, 02:52 PM -
problem with applets?
By srikanthnambu in forum Java AppletsReplies: 2Last Post: 05-22-2009, 06:08 AM -
problem with applets..
By sireesha in forum New To JavaReplies: 3Last Post: 05-21-2009, 03:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks