Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: How can I create a jigsaw puzzle with Array multi-dimensional?

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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.
    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.
    Last edited by ivan8; December 2nd, 2011 at 02:13 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: How can I create a jigsaw puzzle with Array multi-dimensional?

    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?

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

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

    if(buttonPressed=="Swap Images")
    That is the wrong way to compare Strings. You should use the equals() method

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    //**********************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;
     
     
        }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

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

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

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

  9. #9
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Screen Shot 2011-12-04 at 2.04.54 PM.jpg

    The "Shuffle Button" and "Reset Button" are working.

    "Shuffle Button"
    Screen Shot 2011-12-04 at 2.07.17 PM.jpg

    "Reset Button"
    Screen Shot 2011-12-04 at 2.07.45 PM.jpg

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

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

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

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

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default 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
    Attached Images Attached Images
    Last edited by Norm; December 4th, 2011 at 05:52 PM.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

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

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

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

  15. #15
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,609
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

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

Similar Threads

  1. Replies: 3
    Last Post: October 26th, 2011, 03:37 PM
  2. 2 dimensional array coding
    By Bighairjersey in forum Java Theory & Questions
    Replies: 8
    Last Post: July 22nd, 2011, 02:30 PM
  3. HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 3rd, 2011, 03:44 PM
  4. Help on 2D Multi Table Array
    By clevel211 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 1st, 2011, 03:28 AM
  5. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 12th, 2010, 11:44 PM