import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Random;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Random;
class A3 extends JFrame {
public static void main(String[]args){
new A3();
}
new A3();
}
public A3()
{
{
String s;
JFrame F=new JFrame("A");
F.setDefaultLookAndFeelDecorated(true);
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel P = new JPanel();
P.setLayout(new GridLayout(4,4));
JButton button[]=new JButton[16];
F.setDefaultLookAndFeelDecorated(true);
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel P = new JPanel();
P.setLayout(new GridLayout(4,4));
JButton button[]=new JButton[16];
int num[] = new int[16];
Random ran = new Random();
Random ran = new Random();
for(int i = 0; i < 16; i++){
num[i] = i;
}
num[i] = i;
}
for(int i = 0; i < 16; i++){
int j,x;
j = ran.nextInt(16);
x = num[i];
num[i] = num[j];
num[j] = x;
}
int j,x;
j = ran.nextInt(16);
x = num[i];
num[i] = num[j];
num[j] = x;
}
for (int i = 0; i < 16; i++){
button[i] = new JButton();
button[i] = new JButton();
button[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1;
for(int i=0; i <= 15; i++)
{
if(e.getSource() == button[i]){
public void actionPerformed(ActionEvent e) {
String s1;
for(int i=0; i <= 15; i++)
{
if(e.getSource() == button[i]){
if(Integer.valueOf(button[i+1].getText())==0)
{
s1=button[i].getText();
button[i].setText(button[i+1].getText());
button[i+1].setText(s1);
{
s1=button[i].getText();
button[i].setText(button[i+1].getText());
button[i+1].setText(s1);
}
else if(Integer.valueOf(button[i-1].getText())==0)
{
s1=button[i].getText();
button[i].setText(button[i-1].getText());
button[i-1].setText(s1);
else if(Integer.valueOf(button[i-1].getText())==0)
{
s1=button[i].getText();
button[i].setText(button[i-1].getText());
button[i-1].setText(s1);
}
else if(Integer.valueOf(button[i+4].getText())==0)
{
s1=button[i].getText();
button[i].setText(button[i+4].getText());
button[i+4].setText(s1);
}
else if(Integer.valueOf(button[i-4].getText())==0 && i>3 && i<15)
{
s1=button[i].getText();
button[i].setText(button[i-4].getText());
button[i-4].setText(s1);
}
else if(Integer.valueOf(button[i+4].getText())==0)
{
s1=button[i].getText();
button[i].setText(button[i+4].getText());
button[i+4].setText(s1);
}
else if(Integer.valueOf(button[i-4].getText())==0 && i>3 && i<15)
{
s1=button[i].getText();
button[i].setText(button[i-4].getText());
button[i-4].setText(s1);
}
//System.out.println(i+button[i].getText());
}
}
}
});
button[i].setSize(50,50);
button[i].setText(Integer.toString(num[i]));
button[i].setText(Integer.toString(num[i]));
P.add(button[i]);
}
F.getContentPane().add(P,BorderLayout.CENTER);
F.setVisible(true);
}
}
F.getContentPane().add(P,BorderLayout.CENTER);
F.setVisible(true);
}
}