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.
Screen shot 2011-12-02 at 11.51.40 AM.jpg
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
/** * @(#)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.