Results 1 to 4 of 4
  1. #1
    Peter20 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default Help to make a game ???

    Hi,
    I am new in java programming and I would like to make an applet so as to insert into a webpage.

    The applet I am trying to make is a game called slided squares.
    There are 8 squares (with numbers 1,2,3 ... 8 ) and an empty.
    If I click on a square near empty square, it is moved.

    Also there is a move counter.

    when I find all the squares with arithmetic order the program appears a congratulations dialog message.

    Here is the game :
    http://leepoint.net/notes-java/examp...idepuzzle.html



    and my code until now is :
    Java Code:
    import javax.swing.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.reflect.*; 
    
    public class SApplet extends Applet implements ActionListener {
      TextField input,output;
      Label label1,label2, label3, label4, label5, label6;
      JButton[] buttons = new JButton[9];
      JLabel lbl;
      int num, sum = 0,i;
      int[] test = {2,1,3,8,7,5,6,4,0};
      int metrhths = 0;
      Label metrhthsl;
      
      public void init(){
          GridLayout layout = new GridLayout(4,3);
          setLayout(layout);
          Label keno = new Label("");
                Label keno2 = new Label("");
           metrhthsl = new Label("moves: "+Integer.toString(metrhths));
          
          for(i=0;i<9;i++){
              buttons[i]=new JButton(Integer.toString(test[i]));
              add(buttons[i]);
              buttons[i].addActionListener(this);
            }
             add(keno);
            add(metrhthsl);
            add(keno2);
      }
      public int getArrayIndex(int[] myArray, int myObject)
     {
       int ArraySize = Array.getLength(myArray);
       for (int i = 0; i < ArraySize; i++)
          {
           if (myArray[i] == myObject)
            {
             return(i);
            }
          }
      return(-1);
     }
    
         public void actionPerformed(ActionEvent ae){
             metrhths +=1;
             metrhthsl.setText("moves: "+Integer.toString(metrhths));
            //add(metrhthsl);
             Object source = ae.getSource();
             String label = ae.getActionCommand();
             int labelint = Integer.parseInt(label);
             int index=getArrayIndex(test, labelint);
             if((buttons[index+1].getText()).equals("0")){
    
      //  if(source==buttons[1]){
            
        int[] test2 = {1,1,3,8,7,5,6,4,0};
         
          for(i=0;i<9;i++){
              buttons[i].setText(Integer.toString(test2[i]));
    
            }
        }
      }
      
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    You're using both AWT components as well as Swing components; don't do that: AWT components are 'heavyweight' (the OS draws them) while Swing components are 'light weight' (Java draws them). Combining them without any precautions almost never works. My advice would be to use the Swing components (the stuff starting with a capital J).

    kind regards,

    Jos

  3. #3
    Peter20 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks for your advice !

    But I don't know how to make the listeners

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by Peter20 View Post
    Thanks for your advice !

    But I don't know how to make the listeners
    you can simply add a mouse Listener to your components

    I recommend to use loop like a

    Java Code:
    JButton b;
    JPanel p=new JPanel(new GridLayout(3,3));
    for (int i=0 i<9; i++)
    {
    b=new JButton(a_text);
    b.addMouseListener(this);
    b.setName(String.valueOf(i));
    p.add(b);
    }
    
    ...and listens for
    
    public void mouseClicked(MouseEvent e)
    {
     if("0".equals(e.getComponent().getName()))
    {...}
    }
    or something like that :) but I guess you got the idea
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. Replies: 6
    Last Post: 12-15-2009, 03:33 AM
  3. Help to make memory game :=)
    By arian88 in forum AWT / Swing
    Replies: 7
    Last Post: 10-15-2009, 07:23 AM
  4. Replies: 3
    Last Post: 04-20-2009, 10:53 PM
  5. how to make mastermind game
    By javabeginer in forum New To Java
    Replies: 10
    Last Post: 04-14-2009, 03:11 AM

Posting Permissions

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