This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Problem with Applets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with Applets" Watch "Problem with Applets" New topic
Author

Problem with Applets

Vikas Solegaonkar
Greenhorn

Joined: Oct 17, 2011
Posts: 8

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.

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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38463
    
  23
Did you know you can start an applet like this?
campbell@xxxxxj:~/java> gedit Puzzle.java&
[1] 12301
campbell@xxxxxj:~/java> javac Puzzle.java
campbell@xxxxxj:~/java> appletviewer Puzzle.java
clicked 10
. . .
It’s in Herbert Schildt’s book, page 309.

Why are you using only AWT components, rather than Swing? Why are you using break; in that loop? Will that mean you only call drawButtons() once, before you have iterated the collection of buttons? Why don’t you call drawButtons() after the end of the loop?

I have not checked your algorithm. You ought to write it on a sheet of paper (or a word processor = WP) in words, if you have any difficulty with it. If you use a WP, you can post it here and ask for help.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Try resizing the applet after shuffling Components in ArrayList.. It updates the Applet.

Not sure why repaint() did not affect the Applet - might be it's not the way of doing it.
Vikas Solegaonkar
Greenhorn

Joined: Oct 17, 2011
Posts: 8

First, as pointed out by Campbell Ritchie, here is the algorithm (I thought it was too trivial so did not want to waste space on it)
This is what the code does:
1. On Init, populate the ArrayList<Component> Numbers with one blank label and 15 buttons representing the 15 digits. The ArrayList is shuffled and then displayed in the GridLayout
2. On any button press.. check if that button has the blankLabel as a neighbor. If it is, just swap the position.

I found a solution to this problem.. posting it here to complete the thread..

The problem is that the components remembered their location. That is, even after call to



The location of the components did not reset. They were added in a different order every time, but since they remembered their location, they got placed in the same old place.
When I explicitly reset their location using setLocation(), the problem got solved.

But the question

Thank you all for your help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38463
    
  23
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with Applets