Results 1 to 5 of 5
  1. #1
    sole is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

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

    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;  
                }  
            }  
        }  
    }
    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?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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 11:33 AM.

  3. #3
    sole is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default 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 !

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Problem with applets!!
    By mohsib in forum Java Applets
    Replies: 3
    Last Post: 09-12-2011, 03:10 PM
  2. Help with embed problem with applets
    By Beginner in forum New To Java
    Replies: 0
    Last Post: 10-22-2010, 11:31 AM
  3. Linux java applets: printing problem
    By dixan in forum Java Applets
    Replies: 0
    Last Post: 01-28-2010, 03:52 PM
  4. problem with applets?
    By srikanthnambu in forum Java Applets
    Replies: 2
    Last Post: 05-22-2009, 07:08 AM
  5. problem with applets..
    By sireesha in forum New To Java
    Replies: 3
    Last Post: 05-21-2009, 04:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •