Enjoy!!
Untitled.png 37.04K
89 downloadsBelow is the complete code
/**
*
* @author nyfer
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Checkers extends Applet implements MouseListener {
int i=0,j=0,xpos,ypos,empty_x,empty_y,row,col;
char[][] correct=new char[4][4];
char[][] wrong=new char[4][4];
Font f=new Font("Courier New ",Font.BOLD,20);
/** Initializes the applet Checkers */
public void init() {
wrong[0][0]='L';
wrong[0][1]='T';
wrong[0][2]='Y';
wrong[0][3]='M';
wrong[1][0]='R';
wrong[1][1]='I';
wrong[1][2]='E';
wrong[1][3]='O';
wrong[2][0]='A';
wrong[2][1]='P';
wrong[2][2]='U';
wrong[2][3]='N';
wrong[3][0]='D';
wrong[3][1]='R';
wrong[3][2]='A';
wrong[3][3]=' ';
addMouseListener(this);
}
public void paint(Graphics g)
{
int x=50,y=55;
int x1=25,y1=25;
g.drawString("Arrange the Letters into following sentence",20,270);
g.drawString("RATE" +" "+"YOUR" +" "+ "MIND" +" "+ "PAL",20,290);
g.setFont(f);
g.setColor(Color.red);
g.fillRect(10,10,230,230);
String s;
for(int i=0;i<4;i++)
{ g.setColor(Color.black);
for(int j=0;j<4;j++)
{
s=Character.toString(wrong[i][j]);
g.drawString(s,x,y);
x+=50;
g.drawRect(x1,y1,50,50);
x1+=50;
}
x=50;
y+=50;
y1+=50;
x1=25;
}
}
public void mouseClicked (MouseEvent mc)
{
int x=50,y=55;
int x1=25,y1=25;
xpos=mc.getX();
ypos=mc.getY();
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(xpos >x1 && xpos <x1+50 && ypos >y1 && ypos <y1+50 )
{
getempty();
row= getrow(ypos);
col= getcol(xpos);
try
{
if(row==0 && col==3)
{
if((wrong[row+1][col]==' ') || (wrong[row][col-1]==' '))
{
wrong[empty_x][empty_y]=wrong[row][col];
wrong[row][col]=' ';
repaint();
}
}
else if(row==3 && col==3)
{
if((wrong[row-1][col]==' ') || (wrong[row][col-1]==' '))
{
wrong[empty_x][empty_y]=wrong[row][col];
wrong[row][col]=' ';
repaint();
}
}
else if(row==0)
{
if((wrong[row+1][col]==' ')|| (wrong[row][col+1]==' ') || (wrong[row][col-1]==' ') )
{
wrong[empty_x][empty_y]=wrong[row][col];
wrong[row][col]=' ';
repaint();
}
}
else if(row==3)
{
if( (wrong[row][col+1]==' ') || (wrong[row-1][col]==' ') || (wrong[row][col-1]==' ') )
{
wrong[empty_x][empty_y]=wrong[row][col];
wrong[row][col]=' ';
repaint();
}
}
else if(col==0)
{
if((wrong[row+1][col]==' ')|| (wrong[row][col+1]==' ') || (wrong[row-1][col]==' '))
{
wrong[empty_x][empty_y]=wrong[row][col];
wrong[row][col]=' ';
repaint();
}
}
else if(col==3)
{
if((wrong[row+1][col]==' ')||(wrong[row-1][col]==' ')||(wrong[row][col-1]==' '))
{
wrong[empty_x][empty_y]=wrong[row][col];
wrong[row][col]=' ';
repaint();
}
}
else if(row==1|| row==2||col==1|| col==2)
{
if((wrong[row+1][col]==' ') || (wrong[row][col+1]==' ') || (wrong[row-1][col]==' ') || (wrong[row][col-1]==' '))
{
wrong[empty_x][empty_y]=wrong[row][col];
wrong[row][col]=' ';
repaint();
}
}
}
catch(Exception e)
{
//System.out.println("message:"+e);
}
}
x+=50;
x1+=50;
}
x=50;
y+=50;
y1+=50;
x1=25;
}
}
public int getrow(int ty)
{
if(ty<75 && ty>25) return 0;
else if(ty<125 && ty>75) return 1;
else if(ty<175 && ty>125) return 2;
else if(ty<225 && ty>175) return 3;
else return -1;
}
public int getcol(int tx)
{
if(tx<75 && tx>25) return 0;
else if(tx<125 && tx>75) return 1;
else if(tx<175 && tx>125) return 2;
else if(tx<225 && tx>175) return 3;
else return -1;
}
public void getempty()
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(wrong[i][j]==' ')
{
empty_x=i;
empty_y=j;
}
}
}
}
public void mousePressed (MouseEvent mc) {}
public void mouseReleased (MouseEvent mc) {}
public void mouseEntered (MouseEvent mc) {}
public void mouseExited (MouseEvent mc) {}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setLayout(new java.awt.BorderLayout());
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
Thank you
Attached Files
Edited by Nyfer, 13 October 2011 - 11:05 AM.


Sign In
Create Account


Back to top









