Results 1 to 4 of 4
Thread: Help to make a game ???
- 05-09-2010, 11:03 AM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
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])); } } } }
- 05-09-2010, 11:48 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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
- 05-09-2010, 12:19 PM #3
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
Thanks for your advice !
But I don't know how to make the listeners
- 05-10-2010, 05:36 AM #4
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
you can simply add a mouse Listener to your components
I recommend to use loop like a
or something like that :) but I guess you got the ideaJava 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())) {...} }If my answer helped you. Please click my "REP" button and add a comment
Have a Good Java Coding :)
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
Trying to make a functioning Tetris game/ why does my board look weird?
By DeusExMachina90 in forum AWT / SwingReplies: 6Last Post: 12-15-2009, 02:33 AM -
Help to make memory game :=)
By arian88 in forum AWT / SwingReplies: 7Last Post: 10-15-2009, 06:23 AM -
How to make a game quit in BlueJ after entering a room
By alpdog14 in forum New To JavaReplies: 3Last Post: 04-20-2009, 09:53 PM -
how to make mastermind game
By javabeginer in forum New To JavaReplies: 10Last Post: 04-14-2009, 02:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks