Results 1 to 3 of 3
  1. #1
    haya27 is offline Member
    Join Date
    Dec 2014
    Posts
    1
    Rep Power
    0

    Default Java Puzzle Game - Complete beginner please help!

    Okay so I found this code online, and I wanted to play around with it. It's simply a puzzle slide

    The code:

    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();
    }
    }
    }

    So I know how to change the picture and all, but my question is when the picture appears it is already solved, how can I have the picture already shuffled. And another question would be, I want to make this game look good like is that possible like to put a background and make a setup - like for when I'm playing the game. Third question is, is there a way I could have a menu with like 4 pictures and the user picks which ever one they want to solve. I'm really sorry for all these stupid questions but I am a beginner.
    Thank you in advance!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Java Puzzle Game - Complete beginner please help!

    I will help with the first question.

    Create a list (see ArrayList) to hold the pieces instead of adding them to the panel. Make it a list of type JComponent.
    Then after the pieces have been created, shuffle the list (see the Collections class). Then add the components
    to the panel.

    I should warn you that this method may result in a puzzle that can't be solved. The only other way I know to guarantee a solvable
    puzzle is to randomly generate moves over and over again to "shuffle" the puzzle. So have the program pretend it is the player.

    Also, the program distorts the pieces based on the panel size, so make the following change:

    change
    Java Code:
    setSize(325,275)
    to
    Java Code:
    setPreferredSize(new Dimension(width, height));
    pack();
    Regards,
    Jim
    Last edited by jim829; 12-14-2014 at 09:18 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java Puzzle Game - Complete beginner please help!

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. where to start - complete beginner
    By scott7saunders in forum New To Java
    Replies: 5
    Last Post: 02-28-2014, 01:13 PM
  2. Complete Beginner
    By rjreynolds in forum New To Java
    Replies: 4
    Last Post: 01-12-2012, 04:33 AM
  3. Java Complete Beginner Tutorials
    By KrisInfinity in forum Reviews / Advertising
    Replies: 3
    Last Post: 12-15-2011, 07:38 PM
  4. Replies: 1
    Last Post: 06-02-2011, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •