wood burning stoves
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with Applets" Watch "Problem with Applets" New topic

Problem with Applets

Vikas Solegaonkar

Joined: Oct 17, 2011
Posts: 9

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

Joined: Oct 13, 2005
Posts: 45360
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

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

Joined: Oct 17, 2011
Posts: 9

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

Joined: Oct 13, 2005
Posts: 45360
Well done
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem with Applets
jQuery in Action, 3rd edition