bu kodu değiştirip butonlar üzerindeki rakamları resimlerle değiştirmek istiyorum..bi el atın hafızlar=))
import javax.swing.JFrame.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Puzzle extends JFrame implements ActionListener {
//Buttons for each row and column
Button btn[][];
//Displayed numbers on the buttons
String s[]={"","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
//Row array
int row[]=new int[17];
//Location of empty button
int emptyRow;
int emptyColumn;
//Global variables to move the pieces
boolean toUp=false;
boolean toRight=false;
boolean toLeft=false;
boolean toDown=false;
boolean isTrue=false;
String title;
//Constructor definition
public Puzzle(String title) {
//Set title name
super(title);
//Set GridLayout
setLayout(new GridLayout(4,4));
//Initialize the variables count and count1 to be zer0
int count=0;
int count1=0;
//The loop controller
boolean goOn=true;
boolean isTrue=false;
int number;
//Random number generator
do {
number=(int)Math.round(Math.random() * 15);
row[count]=number;
for (int i=0;i<count;i++){
if (row==row[count])
count--;
}
count++;
//To continue if count is not equal to 16
if (count==16)
goOn=false;
}
while(goOn);
//Two dimensional btn array
btn=new Button[4][4];
int k=0;
//The loop to find locations of emptyRow and emptyColumn
for(int m=0;m<4;m++)
for(int l=0;l<4;l++){
int a=row;
//If it's empty button
//Set the properties of the button
if (a==0){
emptyRow=m;
emptyColumn=l;
btn[m]=new Button(s);
btn[m].setEnabled(false);
btn[m].setVisible(false);
btn[m].addActionListener(this);
add(btn[m]);
}
//If it's not empty button
//Set the properties of each button
else{
btn[m]=new Button(s);
add(btn[m]);
btn[m].addActionListener(this);
}
k++;
}
//Invoke calculate method
calculate(emptyRow,emptyColumn);
}
//calculate method
//The movements which the user can do
public void calculate(int a,int b){
if (a-1>=0)
toUp=true;
if (a+1<=3)
toDown=true;
if (b+1<=3)
toRight=true;
if (b-1>=0)
toLeft=true;
}
//Refresh variables about movements
public void clean(){
toUp=false;
toDown=false;
toRight=false;
toLeft=false;
}
//Handle ActionEvent
public void actionPerformed(ActionEvent e){
//When the user moves the button to up
if (toUp){
if (e.getSource()==btn[emptyRow-1][emptyColumn]){
String l=btn[emptyRow-1][emptyColumn].getLabel();
btn[emptyRow][emptyColumn].setLabel(l);
btn[emptyRow][emptyColumn].setVisible(true);
btn[emptyRow][emptyColumn].setEnabled(true);
btn[emptyRow-1][emptyColumn].setVisible(false);
emptyRow--;
clean();
calculate(emptyRow,emptyColumn);
isTrue=true;
}
}
//When the user moves the button to down
if (toDown){
if (!isTrue){
if (e.getSource()==btn[emptyRow+1][emptyColumn]){
String l=btn[emptyRow+1][emptyColumn].getLabel();
btn[emptyRow][emptyColumn].setLabel(l);
btn[emptyRow][emptyColumn].setVisible(true);
btn[emptyRow][emptyColumn].setEnabled(true);
btn[emptyRow+1][emptyColumn].setVisible(false);
emptyRow++;
clean();
calculate(emptyRow,emptyColumn);
isTrue=true;
}
}
}
//When the user moves the button to left
if (toLeft){
if (!isTrue){
if (e.getSource()==btn[emptyRow][emptyColumn-1]){
String l=btn[emptyRow][emptyColumn-1].getLabel();
btn[emptyRow][emptyColumn].setLabel(l);
btn[emptyRow][emptyColumn].setVisible(true);
btn[emptyRow][emptyColumn].setEnabled(true);
btn[emptyRow][emptyColumn-1].setVisible(false);
emptyColumn--;
clean();
calculate(emptyRow,emptyColumn);
isTrue=true;
}
}
}
//When the user moves the button to down
if (toRight){
if (!isTrue){
if (e.getSource()==btn[emptyRow][emptyColumn+1]){
String l=btn[emptyRow][emptyColumn+1].getLabel();
btn[emptyRow][emptyColumn].setLabel(l);
btn[emptyRow][emptyColumn].setVisible(true);
btn[emptyRow][emptyColumn].setEnabled(true);
btn[emptyRow][emptyColumn+1].setVisible(false);
emptyColumn++;
clean();
calculate(emptyRow,emptyColumn);
isTrue=true;
}
}
}
//Refresh the variable
isTrue=false;
//Set properties of empty button
btn[emptyRow][emptyColumn].setEnabled(false);
//Invoke the check method
check();
}
//check method
//Check the game finished or not
public void check(){
boolean k=false;
int a=1;
int count=0;
for(int m=0;m<4;m++)
for(int l=0;l<4;l++){
if (a==16)
break;
if (btn[m].getLabel().equals(""+a))
count++;
a++;
}
if (count==15)
System.out.println ("You win");
}
//main method
public static void main(String[] args){
//Create an instance of Puzzle constructor
Puzzle puzzle=new Puzzle("Puzzle Game");
//Set the properties of puzzle
puzzle.setSize(1000,1000);
puzzle.show();
puzzle.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}