Showing posts with label mouse in maze. Show all posts
Showing posts with label mouse in maze. Show all posts

Tuesday, March 11, 2014

mouse in maze using java

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.*;


public class maze extends Applet implements MouseListener{
    int x=1,y=0,m=0,n1=20;
    int x1,y1;
    Button[][] B=new Button[n1][n1];
    int[][] n=new int[n1][n1];
    Button go;
    int intX=0,intY=0;
    Thread th;
     public void start()
        {
            th=new Thread();
            th.start();
        }
    public void init()
    {
      
         for(int i=0;i<n1;i++)
            for(int j=0;j<n1;j++)
            {
                n[i][j]=1;
                B[i][j]=new Button();
                B[i][j].addMouseListener(this);
                add(B[i][j]);
              
            }
         go=new Button("go");
        go.addMouseListener(this);
        add(go);
         setSize(1000,600);
           setLayout(new GridLayout(n1+1,n1));
        
    }
    public void mousePressed(MouseEvent me) {}
    public void mouseClicked(MouseEvent me) {
        if(m==0)
        for(int i=0;i<n1;i++)
        {
            for(int j=0;j<n1;j++)
            {
                if(me.getSource()==B[i][j])
                {
                    n[i][j]=0;
                    B[i][j].setBackground(Color.red);
                    B[i][j].setEnabled(true);
                }
            }
        }
        if(me.getSource()==go)
            m=1;
      
        for(int i=0;i<n1;i++)
        {
            for(int j=0;j<n1;j++)
            {
                if(me.getSource()==B[i][j]&&n[i][j]!=0)
                {
                    if(((i==x+1)&&(j==y))||((i==x-1)&&(j==y))||((i==x)&&(j==y+1))||(i==x)&&(j==y-1))
                    {
                        x1=x;
                        y1=y;
                    x=i;
                    y=j;
                    B[x1][y1].setBackground(Color.black);
                    B[x1][y1].setEnabled(true);
                    }
                }
            }
        }
        B[x][y].setBackground(Color.green);
        B[x][y].setEnabled(true);
      
        B[0][19].setBackground(Color.cyan);
        B[0][19].setEnabled(true);
        n[x][y]=0;
      
        if(x==0&&y==19)
        {
          
            for(int i=0;i<20;i++)
            {
                for(int j=0;j<20;j++)
                {
                    B[i][j].setBackground(Color.yellow);
                    B[i][j].setEnabled(true);
                }
            }
            for(int i=8;i<12;i++)
            {
                B[i][3].setBackground(Color.red);
                B[i][3].setEnabled(true);
                B[i][7].setBackground(Color.red);
                B[i][7].setEnabled(true);
                B[i][9].setBackground(Color.red);
                B[i][9].setEnabled(true);
              
                B[i][11].setBackground(Color.red);
                B[i][11].setEnabled(true);
              
                B[i][14].setBackground(Color.red);
                B[i][14].setEnabled(true);
            }
            B[6][9].setBackground(Color.red);
            B[6][9].setEnabled(true);
            for(int j=4;j<7;j++)
            {
              
            B[11][j].setBackground(Color.red);
            B[11][j].setEnabled(true);
            }
            B[10][5].setBackground(Color.red);
            B[10][5].setEnabled(true);
            B[9][12].setBackground(Color.red);
            B[9][12].setEnabled(true);
            B[10][13].setBackground(Color.red);
            B[10][13].setEnabled(true);
        }
      
        }
  
    public void mouseEntered(MouseEvent arg0) {}
    public void mouseExited(MouseEvent arg0) {}
    public void mouseReleased(MouseEvent arg0) {}

}

Contributors

Translate