-2

I found this source code from http://zetcode.com/tutorials/javagamestutorial/puzzle/ And I wanted to play around with it. It's a picture puzzle game, however when I run the program, it doesn't show up scrambled and I was wondering how will I go about this to get it scrambled? I'm still new to programming and I just wanted to play around with this to learn a bit. Thank you!

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Puzzle extends JFrame implements ActionListener {

private JPanel centerPanel;
private JButton button;
private JLabel label;
private Image source;
private Image image;
int[][] pos;
int width, height;

public Puzzle() {

    pos = new int[][] {
                        {0, 1, 2}, 
                        {3, 4, 5}, 
                        {6, 7, 8}, 
                        {9, 10, 11}
                    };


    centerPanel = new JPanel();
    centerPanel.setLayout(new GridLayout(4, 4, 0, 0));

    ImageIcon sid = new ImageIcon(Puzzle.class.getResource("icesid.jpg"));
    source = sid.getImage();

    width = sid.getIconWidth();
    height = sid.getIconHeight();


    add(Box.createRigidArea(new Dimension(0, 5)), BorderLayout.NORTH);    
    add(centerPanel, BorderLayout.CENTER);


    for ( int i = 0; i < 4; i++) {
        for ( int j = 0; j < 3; j++) {
            if ( j == 2 && i == 3) {
                label = new JLabel("");
                centerPanel.add(label);
            } else {
                button = new JButton();
                button.addActionListener(this);
                centerPanel.add(button);
                image = createImage(new FilteredImageSource(source.getSource(),
                    new CropImageFilter(j*width/3, i*height/4, 
                        (width/3)+1, height/4)));
                button.setIcon(new ImageIcon(image));
            }
        }
    }

    setSize(325, 275);
    setTitle("Puzzle");
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);
}


public static void main(String[] args) {

    new Puzzle();

}

public void actionPerformed(ActionEvent e) {
    JButton button = (JButton) e.getSource();
    Dimension size = button.getSize();

    int labelX = label.getX();
    int labelY = label.getY();
    int buttonX = button.getX();
    int buttonY = button.getY();
    int buttonPosX = buttonX / size.width;
    int buttonPosY = buttonY / size.height;
    int buttonIndex = pos[buttonPosY][buttonPosX];



    if (labelX == buttonX && (labelY - buttonY) == size.height ) {

         int labelIndex = buttonIndex + 3;

         centerPanel.remove(buttonIndex);
         centerPanel.add(label, buttonIndex);
         centerPanel.add(button,labelIndex);
         centerPanel.validate();
    }

    if (labelX == buttonX && (labelY - buttonY) == -size.height ) {

         int labelIndex = buttonIndex - 3;
         centerPanel.remove(labelIndex);
         centerPanel.add(button,labelIndex);
         centerPanel.add(label, buttonIndex);
         centerPanel.validate();
    }

    if (labelY == buttonY && (labelX - buttonX) == size.width ) {

         int labelIndex = buttonIndex + 1;

         centerPanel.remove(buttonIndex);
         centerPanel.add(label, buttonIndex);
         centerPanel.add(button,labelIndex);
         centerPanel.validate();
    }

    if (labelY == buttonY && (labelX - buttonX) == -size.width ) {

         int labelIndex = buttonIndex - 1;

         centerPanel.remove(buttonIndex);
         centerPanel.add(label, labelIndex);
         centerPanel.add(button,labelIndex);
         centerPanel.validate();
    }
}
}
bummi
  • 27,123
  • 14
  • 62
  • 101
James
  • 11
  • 5

1 Answers1

2
  1. Create each Icon and add the Icon to an ArrayList.
  2. Then you can use the Collections.shuffle(...) method
  3. Then iterate through the shuffled ArrayList and create your buttons add add the Icons to each button.

Edit:

Simple example showing the concept:

import java.awt.*;
import java.util.*;
import javax.swing.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        setLayout( new GridLayout(3, 4) );

        ArrayList<Integer> numbers = new ArrayList<Integer>();

        for (int i = 0; i < 12; i++)
            numbers.add( new Integer(i) );

        Collections.shuffle(numbers);

        for (int i = 0; i < 12; i++)
            add( new JLabel( "" + numbers.get(i) ) );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.setLocationByPlatform( true );
        frame.setSize(300, 300);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Hello, thank you for the advice. Can you give me an example on how to code this? – James May 06 '15 at 05:06
  • @James, You already have most of the code. The only thing new is the Collections.shuffle(...) and that is a single statement. Instead of having one loop you need to create two loops. You also need an ArrayList to temporarily hold the 12 Icons. The first loop creates the Icon from the Image. The second loop creates the button using the Icon. Give it a try and ask a specific question if you have a problem. You don't learn if you don't try. – camickr May 06 '15 at 05:28
  • private ArrayList ImgIcon = new ArrayList(); pieces = new ArrayList(); for (int i=0; i<12; i++) { Ths is what I've added so far, am I in the right track? – James May 06 '15 at 06:01
  • 1) Yes, that is how you create a loop. 2) No, that is not how you create the ArrayList. I suggested you need the `ArrayList` to contain `Icons`, not Integers. Post your updated code in the queston, not in the comment if you need more help. We can't read code in the comment section because it is not formatted. – camickr May 06 '15 at 14:53
  • Hi, I was wondering if you can do the code for me? I've been trying all day and still no progress. I'm still fairly new to Java so I'm not sure what I'm really doing. I'm just playing around right now and would like to see the pictures scrambled so I can work from there :) Thank you very much. – James May 08 '15 at 11:59
  • 1
    I have no idea what you are doing or what you find confusing about my suggestion?. You have not posted any code showing what you have tried. When you have a problem understanding a concept create a [SSCCE](http://sscce.org/) to simplify the concept and forget about the real program. I posted a simple `SSCCE` demonstrating the concept I descriped. It contains 2 loops and a shuffle statement. Your code will be basically the same except your ArrayLIst will contain Icons instead of Integers. – camickr May 08 '15 at 15:08