🔒 Closed [HELP]Slide Puzzle

Status
Not open for further replies.

CoffeeBean-

Enthusiast
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!
 
Dear CoffeeBean-,

Since 2 years have passed since the last reply in this thread, I am locking it to prevent necroposting. Feel free to start a new thread or contact any forum staff if you want this to be reopened.

Thread closed.
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 1K
    Views
  • 2
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,030
Guests online
1,133
Total visitors
2,163

Forum statistics

Threads
2,272,806
Posts
28,945,415
Members
1,236,418
Latest member
yesitsme
Back
Top