1 Attachment(s)
How can I create a jigsaw puzzle with Array multi-dimensional?
Hello everyone, I am a beginner in Java
Basically, I am working on my programming assignment and it needs me to create a jigsaw puzzle.(Not for play)
Here is the requirement.
Attachment 878
I have to create three buttons to control the nine images in this programming.
These buttons are Shuffle button, Reset button and Swap button. Actually, I have no idea how to compile the codes for the "Swap button". I don't know what's wrong with my codes.
Here is my code
Code :
/**
* @(#)MultiDimArray.java
*
* MultiDimArray Applet application 2011/2/11
*
* Read in 9 images and store in a 3x3 array.
* Use a nested for loop to display them.
*
* Add a button to mix it up
* Add a button to put it back in order
* Add a button to swap two random images
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*; //for ActionListener button
import java.util.Random;
public class testing extends Applet implements ActionListener {
Image[][] pic; //picture images
Image[][] rightPic; //picture images in the correct order
Rectangle[][] tile;
Image previous[][];
int tileWidth;
int tileHeight;
TextArea test;
Button shuffleButton;
Button resetButton;
Button swapButton;
String buttonPressed;
public void init() {
//---------------------------Set up the shuffle button---------------------------//
shuffleButton=new Button("Shuffle Images");
setLayout(null);
shuffleButton.setBounds(350,10,120,50);
shuffleButton.addActionListener(this);
add(shuffleButton);
//---------------------------Set up the reset button---------------------------//
resetButton=new Button("Reset Images");
setLayout(null);
resetButton.setBounds(350,60,120,50);
resetButton.addActionListener(this);
add(resetButton);
//---------------------------Set up the swap button---------------------------//
swapButton=new Button("Swap Button");
setLayout(null);
swapButton.setBounds(350,110,120,50);
swapButton.addActionListener(this);
add(swapButton);
//---------------------------Load the pictures in correct order---------------------------//
pic=new Image[3][3];
pic[0][0]=getImage(getCodeBase(),"A1.jpg");
pic[0][1]=getImage(getCodeBase(),"A2.jpg");
pic[0][2]=getImage(getCodeBase(),"A3.jpg");
pic[1][0]=getImage(getCodeBase(),"A4.jpg");
pic[1][1]=getImage(getCodeBase(),"A5.jpg");
pic[1][2]=getImage(getCodeBase(),"A6.jpg");
pic[2][0]=getImage(getCodeBase(),"A7.jpg");
pic[2][1]=getImage(getCodeBase(),"A8.jpg");
pic[2][2]=getImage(getCodeBase(),"A9.jpg");
/*
* Store the right picture positions by
* copying the pic to rightPic array if images
*/
rightPic=new Image[3][3];
for (int a=0;a<3;a++) {
for (int b=0;b<3;b++) {
rightPic[a][b]=pic[a][b];
}
}
//---------------------------Set the tile size and locations---------------------------//
tileWidth=100;
tileHeight=100;
tile=new Rectangle[3][3];
for (int a=0;a<3;a++) {
for (int b=0;b<3;b++) {
tile[a][b]=new Rectangle();
tile[a][b].width=tileWidth;
tile[a][b].height=tileHeight;
tile[a][b].x=b*100;
tile[a][b].y=a*100;
}
}
requestFocus();
}
//--------------------------- Draw the array of images at the stored locations---------------------------//
public void paint(Graphics g) {
/* Draw the puzzle */
for (int a=0;a<3;a++) {
for (int b=0;b<3;b++) {
g.drawImage(pic[a][b],tile[a][b].x,tile[a][b].y,tile[a][b].width,tile[a][b].height,this);
}
}
}
//---------------------------Figure out which button was pushed---------------------------//
public void actionPerformed(ActionEvent e) {
// Using the random number function
Random randomNum1 = new Random();
Random randomNum2 = new Random();
Random randomNum3 = new Random();
Random randomNum4 = new Random();
//**********************Shuffle Images pressed**********************//
buttonPressed=e.getActionCommand();
// Mix up positions in the array
if (buttonPressed=="Shuffle Images") {
for( int a = 0 ; a < 3 ; a++){
for( int b = 0 ; b < 3 ; b++){
int x1 = randomNum1.nextInt(3);
int y1 = randomNum2.nextInt(3);
previous = new Image[3][3];
previous[a][b]=pic[a][b];
pic[a][b]=pic[y1][x1];
pic[y1][x1]=previous[a][b];
}
}
}
//**********************Reset Images pressed**********************//
else if (buttonPressed=="Reset Images") {
// Put the positions back into the right order
for (int a=0;a<3;a++) {
for (int b=0;b<3;b++) {
pic[a][b]=rightPic[a][b];
}
}
}
//**********************Swap Images pressed**********************//
else if (buttonPressed=="Swap Images") {
int tempX, tempY;
int x1 = randomNum1.nextInt(3);
int y1 = randomNum2.nextInt(3);
int x2 = randomNum3.nextInt(3);
int y2 = randomNum4.nextInt(3);
tempX = tile[y1][x1].x;
tile[y1][x1].x = tile[y2][x2].x;
tile[y2][x2].x = tempX;
tempY = tile[y1][x1].y;
tile[y1][x1].y = tile[y2][x2].y;
tile[y2][x2].y = tempY;
tile[y1][x1].x = x1 * 100;
tile[y1][x1].y = y1 * 100;
tile[y2][x2].x = x2 * 100;
tile[y2][x2].y = y2 * 100;
}
//---------------------------Redraw the puzzle---------------------------//
repaint();
}
}
When I press the "Swap button", it is nothing happened.
Can someone help me fix the code?
Thanks a lot.
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
Quote:
When I press the "Swap button", it is nothing happened.
To make sure about that add a println statement to the listener method and print out the ActionEvent object: e.
When you press the button, you can see if it prints anything.
You should use the equals() method when comparing Strings, not the == operator:
buttonPressed=="Shuffle Images"
What code have you written to handle the event when the Swap button is pressed?
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
Java script
Yes, i know. Only the swap button doesn't work.
My shuffle button and reset button are working.
else if (buttonPressed=="Swap Images")
This is original code which is given by my teacher.
I want to know, Did I type any wrong code for the "Swap button"? and how come it is wrong?
Thanks a lot.
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
Did you add the println statement as I suggested?
What was printed out when you executed the code after you added the println?
Code :
if(buttonPressed=="Swap Images")
That is the wrong way to compare Strings. You should use the equals() method
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
I used the equals() method already, but it still doesn't work.
I think the code of "Swap button" is wrong.
Code :
//**********************Swap Images Pressed**********************//
else if (buttonPressed.equals("Swap Images")) {
int tempx1, tempy1;
int tempx2, tempy2;
int x1 = randomNum1.nextInt(3);
int y1 = randomNum2.nextInt(3);
int x2 = randomNum3.nextInt(3);
int y2 = randomNum4.nextInt(3);
tempx1 = tile[y1][x1].x;
tile[y1][x1].x = tile[y2][x2].x;
tile[y2][x2].x = tempx1;
tempy1 = tile[y1][x1].y;
tile[y1][x1].y = tile[y2][x2].y;
tile[y2][x2].y = tempy1;
tempx2 = tile[y1][x1].x;
tile[y1][x1].x = tile[y2][x2].x;
tile[y2][x2].x = tempx2;
tempy2 = tile[y1][x1].y;
tile[y1][x1].y = tile[y2][x2].y;
tile[y2][x2].y = tempy2;
tile[y1][x1].x = x1 * 100;
tile[y1][x1].y = y1 * 100;
tile[y2][x2].x = x2 * 100;
tile[y2][x2].y = y2 * 100;
}
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
Did you add the println statement as I suggested in posts #2 and #4?
What was printed out when you executed the code after you added the println?
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
YES, i did.
When i press the "Swap button", It is still nothing happened.
It should swap two images randomly.
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
As I asked in posts #2, #4 and #6
What was printed out when you executed the code after you added the println?
Please post the print outs that you get from the printlns. There is valuable information in them that you need to understand.
3 Attachment(s)
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
When I press the "Swap button", It is still nothing happened.
It should swap two images randomly.
Attachment 882
The "Shuffle Button" and "Reset Button" are working.
"Shuffle Button"
Attachment 883
"Reset Button"
Attachment 884
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
Please read this again:
What was printed out when you executed the code after you added the println?
Please post the print outs that you get from the printlns. There is valuable information in them that you need to understand.
I am trying to help you debug your code and figure out why it does not do what you want. If you don't want any help, I'll leave.
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
Where is the output from the println asked for here:
To make sure about that add a println statement to the listener method and print out the value of the ActionEvent object: e.
Press all three buttons.
Copy and paste the output here.
1 Attachment(s)
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
Another way to get the console output is to use the appletviewer to execute the applet. It has a console that you can copy the output from.
Here is what is copied into the system clipboard:
Java Plug-in 1.6.0_29
Using JRE version 1.6.0_29-b11 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Norm
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
SearchApplet - 3 Aug 2005
Searching E:/, file selection: *.html for null
init() exiting
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
Here's a sample of the console from when I use AppletViewer with an applet that uses println to output debugging data:
Quote:
Running: D:\Java\jdk1.6.0_02\bin\appletviewer.exe NumberGame2.java
What is 10 + 5?
Face: Player Answer= 0 Answer= 0
Smile Face: Player Answer Ok
Face: Player Answer= 0 Answer= 0
Smile Face: Player Answer Ok
player input=15
If Statement:
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
Two out of three buttons work for you. What is different between the two that work and the one that does NOT work?
Try swapping the code that is executed for one of the buttons that works with the one that does not work and see what happens.
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
This is the console when I executed my applet.
Java Plug-in 1.6.0_29
Using JRE version 1.6.0_29-b11-402-11M3527 Java HotSpot(TM) 64-Bit Server VM
User home directory = /Users/ivanchan
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
Re: How can I create a jigsaw puzzle with Array multi-dimensional?
Where is the output from the println I asked you in post #2 to put in the actionPerformed method?
After you add that println, execute the code and press all three buttons. You should get one line of output for each button press.