import java.applet.*;
import java.awt.*;
public class newbutton extends Applet
{
Button b[] = new Button[16];
HandleGameButton me;
HandleNewGame you;
HandleRestartGame we;
rcolor c = new rcolor(16);
Font f1, f2;
int blankPosition = 15;
Panel panel1,panel2;
int score = 0;
Label scoreLabel, setLabel;
Button newGameButton;
Button RestartGame;
String buttonText[];
public void init()
{
buttonText = new String[16];
panel1 = new Panel();
panel2 = new Panel();
newGameButton = new Button("New Game");
newGameButton.setBackground(Color.blue);
newGameButton.setForeground(Color.white)…
you = new HandleNewGame(this);
newGameButton.addActionListener(you);
panel2.add(newGameButton);
RestartGame = new Button("Restart Game");
RestartGame.setBackground(Color.orange);
RestartGame.setForeground(Color.black);
we = new HandleRestartGame(this);
RestartGame.addActionListener(we);
panel2.add(RestartGame);
f1 = new Font("Courier",Font.PLAIN, 70);
setFont(f1);
f2 = new Font("Courier",Font.PLAIN, 20);
panel2.setFont(f2);
c.fill_colors();
setLayout(new GridLayout(1,2,0,0) );
add(panel1);
add(panel2);
panel1.setLayout(new GridLayout(4,4,0,0) );
panel2.setLayout(new GridLayout(4,1,0,0) );
for (int index = 0; index < b.length; index++)
{
b[index] = new Button(String.valueOf(index+1));
b[index].setBackground(Color.red);
b[index].setForeground(Color.black);
me = new HandleGameButton(index,this);
b[index].addActionListener(me);
panel1.add(b[index]);
}
b[15].setBackground(Color.black);
panel2.setBackground(Color.green);
scoreLabel = new Label("Your Score is " + score);
scoreLabel.setBackground(Color.yellow);
panel2.add(scoreLabel);
mix();
saveGame();
score = 0;
scoreLabel.setText("Your Score is " + score);
}
public void saveGame()
{
for( int index = 0; index <= 15; index++)
{
buttonText[index] = b[index].getLabel();
}
}
public void pressButton(int buttonNumber)
{
if(nextToBlank( buttonNumber))
{
b[blankPosition].setBackground(Color.red…
b[buttonNumber].setBackground(Color.blac…
b[blankPosition].setLabel(b[buttonNumber… );
blankPosition = buttonNumber;
score++;
scoreLabel.setText("Your Score is " + score);
}
}
public boolean nextToBlank( int buttonNumber)
{
boolean answer = false;
if(buttonNumber-4 == blankPosition) answer = true;
else if(buttonNumber+4 == blankPosition) answer = true;
else if(buttonNumber+1 == blankPosition && buttonNumber/4 == blankPosition/4 ) answer = true;
else if(buttonNumber-1 == blankPosition && buttonNumber/4 == blankPosition/4 ) answer = true;
return(answer);
}
public int randInt( int limit )
{
return ( (int) (limit*Math.random() ) );
}
public void mix()
{
for(int index=1; index<=8000; index++)
{
int scrambleUp = randInt(16);
pressButton(scrambleUp);
}
}
}