• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Applets

 
Greenhorn
Posts: 9
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
I like tacos! And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic