1. Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as:

    All that and more! Registration is quick, simple and absolutely free. Join our community today!

Java [HELP]Slide Puzzle

Discussion in 'Coding, Programming' started by CoffeeBean-, Mar 18, 2015.

  1. Mga ka PHC patulong naman po dito sa program natin. Hindi po kasi sya nagsashuffle pag nirun ko game. Bali pag run mo complete na po siya agad. Gusto ko po sanang magpatulong.

    Ito po yung codes.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;

    public class myPuzzle 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 myPuzzle() {

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


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

    ImageIcon sid = new ImageIcon(myPuzzle.class.getResource("images/sti.png"));
    source = sid.getImage();

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


    add(centerPanel, BorderLayout.CENTER);


    for ( int i = 0; i < 3; i++) {
    for ( int j = 0; j < 3; j++) {
    if ( j == 2 && i == 2) {
    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/3,
    (width/3)+1, height/3)));
    button.setIcon(new ImageIcon(image));
    }
    }
    }

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


    public static void main(String[] args) {

    new myPuzzle();

    }

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


    SALAMAT PO MGA KA PHC!
     
  2. Bale d ba sya nakashuffle pag ni run, ayun ba ung problem???
     
Tags / Keywords: