Tuesday, 17 September 2013

Mouse Event

HTML Online Editor Sample
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package  javaapplication24;
 
/**
 *
 * @author gaurav
 */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class JavaApplication24 extends Applet implements MouseListener,KeyListener{
 
    /**
     * @param args the command line arguments
     */
    String msg;
    char c;
    int i,j,k,m=0;
    public void init()
    {addMouseListener(this);
    addKeyListener(this);
    }
    public void mouseClicked(MouseEvent e){
        msg="kakati";
    i=e.getX();
    j=e.getY();
    repaint();
    k=1;
    
    } 
    public void mouseEntered(MouseEvent e){
        msg="kakati";
    i=e.getX();
    j=e.getY();
    repaint();
    k=1;
    
    } public void mouseExited(MouseEvent e){
        msg="kakati";
    i=e.getX();
    j=e.getY();
    repaint();
    k=1;
    
    } public void mousePressed(MouseEvent e){
        msg="kakati";
    i=e.getX();
    j=e.getY();
    repaint();
    k=1;
    
    } public void mouseReleased(MouseEvent e){
        msg="kakati";
    i=e.getX();
    j=e.getY();
    repaint();
    k=1;
    
    }
    
    public void keyPressed(KeyEvent e){
    showStatus("key pressed");}
    public void keyTyped(KeyEvent e){
    c=e.getKeyChar();m=1;
    System.out.println(c);
    }
   public void keyReleased(KeyEvent e){showStatus("key released");}
    
    public void paint(Graphics g)
    {g.setColor(Color.red);
        if(k==1){g.drawString(msg, i, j);
        }
            }
    
    
    
    
    
    
    
  
}

TicTacToe

HTML Online Editor Sample

 

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tictactoe;
 
/**
 *
 * @author gaurav
 */
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
 
public class Tictactoe extends Applet implements ActionListener{
 
 Button [][]square;
 Button start,pause,reset;
 int s=0,a=0,b=0,l=1,g=0,h=0,x=0,y=0,z=0,w=0,u=0,v=0,n=0,o=0;
    @Override
public  void init()
{
    setLayout(null);
    start=new Button("START");
    start.reshape(305+25, 475,40 ,40);
    pause=new Button("Pause");
    pause.reshape(355+25,475,40,40);
    reset=new Button("RESET");
    reset.reshape(405+25,475,40,40);
    add(start);start.setBackground(Color.yellow);
    pause.setBackground(Color.GREEN);
    reset.setBackground(Color.white);reset.addActionListener(this);
    add(pause);
    add(reset);start.addActionListener(this);
    square=new Button[3][3];
    int k=1;
    for(int i=0;i<3;i++)
    {for (int j=0;j<3;j++){
    square[i][j]=new Button();
    
   square[i][j].reshape(325+50*j,300+50*i,50,50);add(square[i][j]);k++;   
   square[i][j].setBackground(Color.gray);
    square[i][j].addActionListener(this);
    
    }
}}
public void actionPerformed(ActionEvent ae)
{if(ae.getSource()==reset){
    for(int i=0;i<3;i++)
{for(int j=0;j<3;j++){square[i][j].setLabel("  ");n=1;repaint();
s=0;a=0;b=0;l=1;g=0;h=0;x=0;y=0;z=0;w=0;u=0;v=0;n=0;
    }}}
else if(ae.getSource()==start)
{ a=1; l=0;repaint();}
 
if(a==1)
 
{for(int j=0;j<3;j++)
{for(int k=0;k<3;k++){
    if(ae.getSource()==square[j][k]){
        if(b%2==0){if(square[j][k].getLabel()=="O"){JOptionPane.showMessageDialog(null, "wrong move");b--;}
        else
            square[j][k].setLabel("X");
        repaint();}
        else if(b%2==1){if(square[j][k].getLabel()=="X"){JOptionPane.showMessageDialog(null, "wrong move");b--;}
        else
            square[j][k].setLabel("O");
        repaint();}b++;check();
    }
}
 
 
 
 
}
    }if(b==9&&o==0){JOptionPane.showMessageDialog(null, "GAME IS DROW press RESET to paly again");return;}
    }
 
 
 
 
public void check(){
    g=0;h=0;x=0;y=0;z=0;u=0;v=0;w=0;o=0;
    for(int i=0;i<3;i++)
    {g=0;h=0;
        for(int j=0;j<3;j++)
    {if(square[i][j].getLabel()=="X")g++;
    else if(square[i][j].getLabel()=="O")
    h++;
        }if(g==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER ONE IS WINNER press RESET to play again");o=1;return;}
        else if(h==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER TWO IS WINNER press RESET to play again");o=1;return;}
        
    } for(int j=0;j<3;j++)
    {x=0;y=0;
        for(int i=0;i<3;i++)
    {if(square[i][j].getLabel()=="X"){x++;}
    else if(square[i][j].getLabel()=="O"){
    y++;}
        }if(x==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER ONE IS WINNER press RESET to play again");o=1;return;}
        else if(y==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER TWO IS WINNER");o=1;return;}
        
    }for(int j=0;j<3;j++)
    {
        for(int i=0;i<3;i++)
    {if(square[i][j].getLabel()=="X"&&i==j){z++;}
    else if(square[i][j].getLabel()=="O"&&i==j){
    w++;}
        }if(z==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER ONE IS WINNER press RESET to play again");o=1;return;}
        else if(w==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER TWO IS WINNER press RESET to play again");o=1;return;}
        
    } for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
    {if(square[i][j].getLabel()=="X"&&i+j==2){u++;}
    else if(square[i][j].getLabel()=="O"&&i+j==2)
    {v++;}
        }if(u==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER ONE IS WINNER press RESET to play again");o=1;return;}
        else if(v==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER TWO IS WINNER press RESET to play again");o=1;return;}
        
    }
 
 
 
 
 
 
 
 
}
 
 
public void paint(Graphics g){
    setBackground(Color.CYAN);
    g.setColor(Color.red);
    g.fillRect(200,200,400,400);
 
 
 
 
if(l==0&&n==0){
if(b%2==0&&b<9)
{g.setColor(Color.green);g.fillRect(325,225,150,40);g.setColor(Color.black);
    g.drawString("PLAYER ONE MOVE",325+25,250);}
else if(b%2==1&&b<9){g.setColor(Color.white);g.fillRect(325,225,150,40);g.setColor(Color.blue);
    g.drawString("PLAYER TWO MOVE", 325+25, 250);}
    
    
}else if(n==1){g.drawString(" ", 325+25,250);}
}}
   
    

Maze Game

HTML Online Editor Sample
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication19;
 
/**
 *
 * @author gaurav
 */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class JavaApplication19 extends Applet implements ActionListener,Runnable{
int counter=0;int s=0;int a=0,b=0;int l=0;
    Button [][]square;
    int [][]visited;
    int [][]visit;
    Button start,end,reset,set;
    Thread t[];
    public void init()
    {setBackground(Color.PINK);
    t=new Thread[5];
    for(int k=0;k<5;k++){t[k]=new Thread(this);}
    setLayout(null);
    visited=new int[10][10];
    visit=new int[10][10];
    for(int i=0;i<10;i++){for(int j=0;j<10;j++)
    {visit[i][j]=1;visited[i][j]=0;}
        }
    reset=new Button("RESET");
    add(reset);
    reset.reshape(350,570,100,30);
    reset.setBackground(Color.blue);
    reset.addActionListener(this); set=new Button("SET");
    add(set);
    set.reshape(350,20,100,30);
    set.setBackground(Color.red);
    set.addActionListener(this);
    start=new Button("START");
    add(start);
    start.setBackground(Color.green);
    start.reshape(100,50,50,50);
    start.addActionListener(this);
    end=new Button("END");
    add(end);
    end.reshape(650,500,50,50);
    end.setBackground(Color.red);
    end.addActionListener(this);
    square=new Button[10][10];
    for(int i=0;i<10;i++)
    {for(int j=0;j<10;j++)
    {square[i][j]=new Button();
    square[i][j].setBackground(Color.white);
    add(square[i][j]);
    square[i][j].reshape(150+(i*50),50+(j*50),50,50);square[i][j].addActionListener(this);
    
    }
        }
    
   
   
    
    } public void run()
    {try{square[0][0].setBackground(Color.yellow);int i=0;int j=0;
    visit[0][0]=0;visited[0][0]=1;Thread.sleep(300);do{if(i==0){ if(visited[i+1][j]==0){square[i+1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i+1][j]=1;visit[i+1][j]=0;
            Thread.sleep(300);i++;}else if(visited[i][j+1]==0){square[i][j+1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j+1]=1;visit[i][j+1]=0;
            Thread.sleep(300);j++;}else if(visited[i][j-1]==0&&j>0){square[i][j-1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j-1]=1;visit[i][j-1]=0;
            Thread.sleep(300);j--;}}
    else if(i==9){ if(j<9&&visited[i][j+1]==0){square[i][j+1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j+1]=1;visit[i][j+1]=0;
            Thread.sleep(300);j++;}
    else if(visited[i][j-1]==0&&j>0){square[i][j-1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j-1]=1;visit[i][j-1]=0;
            Thread.sleep(300);j--;}
    else if(i>0&&visited[i-1][j]==0){square[i-1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i-1][j]=1;visit[i-1][j]=0;
            Thread.sleep(300);i--;}}
   
    else if(j==0){if(visited[i+1][j]==0){square[i+1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i+1][j]=1;visit[i+1][j]=0;
            Thread.sleep(300);i++;}
     else if(j<9&&visited[i][j+1]==0){square[i][j+1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j+1]=1;visit[i][j+1]=0;
            Thread.sleep(300);j++;}
     else if(i>0&&visited[i-1][j]==0){square[i-1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i-1][j]=1;visit[i-1][j]=0;
            Thread.sleep(300);i--;}
    }
    else if(j==9){if(visited[i+1][j]==0){square[i+1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i+1][j]=1;visit[i+1][j]=0;
            Thread.sleep(300);i++;}
     else if(visited[i][j-1]==0&&j>0){square[i][j-1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j-1]=1;visit[i][j-1]=0;
            Thread.sleep(300);j--;}
    
     else if(i>0&&visited[i-1][j]==0){square[i-1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i-1][j]=1;visit[i-1][j]=0;
            Thread.sleep(300);i--;}
    }
    else if(visited[i+1][j]==0){square[i+1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i+1][j]=1;visit[i+1][j]=0;
            Thread.sleep(300);i++;}
     
     else if(j<9&&visited[i][j+1]==0){square[i][j+1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j+1]=1;visit[i][j+1]=0;
            Thread.sleep(300);j++;}
     else if(visited[i][j-1]==0&&j>0){square[i][j-1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j-1]=1;visit[i][j-1]=0;
            Thread.sleep(300);j--;}
     else if(i>0&&visited[i-1][j]==0){square[i-1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i-1][j]=1;visit[i-1][j]=0;
            Thread.sleep(300);i--;}
     
     //back tracking
  /*  else if(visited[0][j+1]==1&&visited[0][j-1]==1&&visited[1][j]==1){
         System.out.print("ram");
         if(visit[0][j+1]==0)
     { square[0][j+1].setBackground(Color.yellow);square[0][j].setBackground(Color.white);visited[0][j+1]=1;visit[0][j]=0;
            Thread.sleep(300);j++;} 
             else if(visit[0][j-1]==0&&j>0)
     { square[0][j-1].setBackground(Color.yellow);square[0][j-1].setBackground(Color.white);visited[0][j-1]=1;visit[0][j]=0;
            Thread.sleep(300);j--;} 
             else if(visit[1][j]==0)
     { square[1][j].setBackground(Color.yellow);square[0][j].setBackground(Color.white);visited[1][j]=1;visit[0][j]=0;
            Thread.sleep(300);i++;}
     }
     else if(visited[9][j+1]==1&&visited[9][j-1]==1&&visited[8][j]==1){
         System.out.print("ram");
         if(visit[9][j+1]==0)
     { square[9][j+1].setBackground(Color.yellow);square[9][j].setBackground(Color.white);visited[9][j+1]=1;visit[9][j]=0;
            Thread.sleep(300);j++;} 
             else if(visit[9][j-1]==0&&j>0)
     { square[9][j-1].setBackground(Color.yellow);square[9][j-1].setBackground(Color.white);visited[9][j-1]=1;visit[9][j]=0;
            Thread.sleep(300);j--;} 
             else if(visit[i-1][j]==0)
     { square[9-1][j].setBackground(Color.yellow);square[9-1][j].setBackground(Color.white);visited[9-1][j]=1;visit[9][j]=0;
            Thread.sleep(300);i--;}
     }*/
     else if(visited[i+1][j]==1&&visited[i][j+1]==1&&visited[i][j-1]==1&&visited[i-1][j]==1&&i<9&&i>0&&j>0&&j<9){if(i<9&&visit[i+1][j]==0){
            
                square[i+1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i+1][j]=1;visit[i][j]=1;
            Thread.sleep(300);i++;}
     else if(visit[i][j+1]==0&&j<9){
            
                square[i][j+1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j+1]=1;visit[i][j]=1;
            Thread.sleep(300);j++;}
            
             
     else if(visit[i][j-1]==0&&j>0){
            
                square[i][j-1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j-1]=1;visit[i][j]=1;
            Thread.sleep(300);j--;}
             else if(visit[i-1][j]==0&&i>0){
            
                square[i-1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i-1][j]=1;visit[i][j]=1;
            Thread.sleep(300);i--;}} 
    if(i==9&&j==9){JOptionPane.showMessageDialog(null, "GAME IS OVER TO PLAY AGAIN PRESS RESET BUTTON");break;}
             
 }while(i<=10&&j<=10);}
    
     catch(InterruptedException e){System.out.println("gaurav");}
    }
    
    
    
    
    
    public void actionPerformed(ActionEvent ae)
    {if(ae.getSource()==set)
    {
    s=0;l=0;
    JOptionPane.showMessageDialog(null, "DESIGN THE TRACK");
    
    }
        
        
    else if(ae.getSource()==reset){s=0;
    
    
         for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++){
                square[i][j].setBackground(Color.white);visit[i][j]=1;visited[i][j]=0;
                
                
            }
    
    
    
    
    }
         l++;start.setBackground(Color.green);end.setBackground(Color.red);
            } else if(ae.getSource()==end){
 end.setBackground(Color.yellow);JOptionPane.showMessageDialog(null, "GAME IS OVER TO PLAY AGAIN PRESS RESET BUTTON");t[l].stop();t[l].notify();}
       else if(ae.getSource()==start){
        start.setBackground(Color.yellow);
        s=1;t[l].start();
        
        
        
        
        
        
        return;}
        
        
        else if(s<1){
    
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++){
              
                if(ae.getSource()==square[i][j])
                    
                {if(square[i][j].getBackground()==Color.white) {square[i][j].setBackground(Color.black);visited[i][j]=1;visit[i][j]=1;}
                else 
                { square[i][j].setBackground(Color.white);visited[i][j]=0;visit[i][j]=1;
                }
                
                }}
                
                }}
    
  /*  else if(s==1){ 
        
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++){
              
                if(ae.getSource()==square[i][j])
                    
                {if(square[i][j].getBackground()==Color.white)
                    square[i][j].setBackground(Color.yellow);
                    else
                    square[i][j].setBackground(Color.white);
                }
                
                }}}*/
    
 
            
 
}
    public void paint(Graphics g)
    {g.drawRect(100,50,600,600);
     
    
    
    }
 
 
 
 
}
        

Addition Of Numbers

HTML Online Editor Sample

 

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication17;
 
/**
 *
 * @author gaurav
 */import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class JavaApplication17 extends Applet implements ActionListener {
 
   Button sum,reset;
   Label input1,input2,sum1;
   TextField t1,t2,t3;
   public void init()
   {
   setLayout(null);
   Label input1=new Label("value1");
   Label input2=new Label("value2");
   Label sum1=new Label("sum");
   input1.setBackground(Color.ORANGE); input2.setBackground(Color.orange); sum1.setBackground(Color.orange);
   t1=new TextField();
   t2=new TextField();
   add(t1);
   add(t2); t1.setBackground(Color.LIGHT_GRAY); t2.setBackground(Color.LIGHT_GRAY);
   t1.reshape(200,100,50,20);
   t2.reshape(200,200,50,20);
   add(input1);
   add(input2);add(sum1);
   input1.reshape(100, 100, 50,20);
   input2.reshape(100, 200, 50,20);
   sum1.reshape(100,400,50,10);t3=new TextField();add(t3);t3.reshape(200,400,50,20); t3.setBackground(Color.MAGENTA);
   sum=new Button("sum");add(sum);
   sum.reshape(100,300,40,40);
   reset=new Button("reset");
   add(reset); sum.setBackground(Color.gray); reset.setBackground(Color.gray);
   reset.reshape(200,300,40,40);
   sum.addActionListener(this);
   reset.addActionListener(this);
   
   
   }
   public void actionPerformed(ActionEvent ae)
   
   {
   if(ae.getSource()==sum){
       
       
       String a=t1.getText();
   String b=t2.getText();
   int x=Integer.parseInt(b);int y=Integer.parseInt(a);
   int c=x+y;
   System.out.println(c);
   t3.setText(String.valueOf(c));
   
   
   }
   
   else if(ae.getSource()==reset)
   {
   t3.setText(" ");t1.setText(" ")
   ;t2.setText(" ");
   
   
   
   }
   
   
   
   }
   
   
   
   
}

8 Queen Problem

HTML Online Editor Sample

 

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
import java.applet.Applet;
import java.awt.event.*;
        import java.awt.*;
 
/**
 *
 * @author gaurav
 */
public class eightqueen extends Applet implements Runnable,ActionListener {
 
    /**
     * Initialization method that will be called after the applet is loaded into
     * the browser.
     */
    int a=0,b=0,c=0,d=0;
    Button [][]chess;
    int [][]check;
    Thread t;
    Button start;
    public void init() {
        setLayout(null);
        t=new Thread(this);
        start=new Button("START");
        add(start);
        start.reshape(250,525,100,30);
        start.setBackground(Color.red);
        start.addActionListener(this);
        check=new int[8][8];
        for(int i=0;i<8;i++)
        {for(int j=0;j<8;j++){
            check[i][j]=0;
        
        
        }}
        chess=new Button[8][8];
        for(int i=0;i<8;i++){
            for(int j=0;j<8;j++){chess[i][j]=new Button();
            add(chess[i][j]);
            if((i+j)%2==0){
            chess[i][j].setBackground(Color.black);}
            else
            {chess[i][j].setBackground(Color.white);}
            chess[i][j].reshape(100+(50*j),100+(50*i),50,50);
            chess[i][j].addActionListener(this);
                
        }}
        queen();
        // TODO start asynchronous download of heavy resources
    }
    // TODO overwrite start(), stop() and destroy() methods
    public void run()
    {try
    {
        do{chess[6][4].setLabel("Q");
    chess[6][4].setBackground(Color.green);
    chess[1][3].setLabel("Q");
    chess[1][3].setBackground(Color.green);
    chess[3][0].setLabel("Q");
    chess[3][0].setBackground(Color.green);
    chess[5][1].setLabel("Q");
    chess[5][1].setBackground(Color.green);
    chess[7][2].setLabel("Q");
    chess[7][2].setBackground(Color.green);
    chess[4][7].setLabel("Q");
    chess[4][7].setBackground(Color.green);
    chess[2][6].setLabel("Q");
    chess[2][6].setBackground(Color.green);
    chess[0][5].setLabel("Q");
    chess[0][5].setBackground(Color.green);
    Thread.sleep(5000);
    clear();
    chess[4][6].setLabel("Q");
    chess[4][6].setBackground(Color.green);
    chess[3][1].setLabel("Q");
    chess[3][1].setBackground(Color.green);
    chess[0][3].setLabel("Q");
    chess[0][3].setBackground(Color.green);
    chess[1][5].setLabel("Q");
    chess[1][5].setBackground(Color.green);
    chess[2][7].setLabel("Q");
    chess[2][7].setBackground(Color.green);
    chess[7][4].setLabel("Q");
    chess[7][4].setBackground(Color.green);
    chess[6][2].setLabel("Q");
    chess[6][2].setBackground(Color.green);
    chess[5][0].setLabel("Q");
    chess[5][0].setBackground(Color.green);
    Thread.sleep(5000);
    clear();
    chess[1][4].setLabel("Q");
    chess[1][4].setBackground(Color.green);
    chess[4][0].setLabel("Q");
    chess[4][0].setBackground(Color.green);
    chess[2][1].setLabel("Q");
    
    chess[2][1].setBackground(Color.green);
    chess[0][2].setLabel("Q");
    chess[0][2].setBackground(Color.green);
    chess[6][3].setLabel("Q");
    chess[6][3].setBackground(Color.green);
    chess[3][7].setLabel("Q");
    chess[3][7].setBackground(Color.green);
    chess[5][6].setLabel("Q");
    chess[5][6].setBackground(Color.green);
    chess[7][5].setLabel("Q");
    chess[7][5].setBackground(Color.green);
     Thread.sleep(5000);
    clear();
    
    
    chess[4][1].setLabel("Q");
    chess[4][1].setBackground(Color.green);
    chess[0][4].setLabel("Q");
    chess[0][4].setBackground(Color.green);
    chess[1][2].setLabel("Q");
    
    chess[1][2].setBackground(Color.green);
    chess[2][0].setLabel("Q");
    chess[2][0].setBackground(Color.green);
    chess[3][6].setLabel("Q");
    chess[3][6].setBackground(Color.green);
    chess[7][3].setLabel("Q");
    chess[7][3].setBackground(Color.green);
    chess[6][5].setLabel("Q");
    chess[6][5].setBackground(Color.green);
    chess[5][7].setLabel("Q");
    chess[5][7].setBackground(Color.green);
     Thread.sleep(5000);
    clear();
   /* chess[4][6].setLabel("Q");
    chess[4][6].setBackground(Color.green);
    chess[4][6].setLabel("Q");
    chess[4][6].setBackground(Color.green);
    chess[4][6].setLabel("Q");
    chess[4][6].setBackground(Color.green);
    chess[4][6].setLabel("Q");
    chess[4][6].setBackground(Color.green);
    */
    }while(true);}
    
    
    
    
    
    
    catch(InterruptedException e){System.out.print("gaurav");
    }
    
    
    
    
    
    }
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==start)
            t.start();
       
    
    }
    public void queen(){}
    public void clear()
    {for(int i=0;i<8;i++){for(int j=0;j<8;j++){if((i+j)%2==1){chess[i][j].setBackground(Color.white);chess[i][j].setLabel(" ");}
    else
    {chess[i][j].setBackground(Color.black);chess[i][j].setLabel(" ");}
            }}}
    
}

Pendulum Clock

HTML Online Editor Sample

 

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication16;
 
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
 
/**
 *
 * @author gaurav
 */
public class timmer extends Applet implements Runnable,ActionListener {
 
    /**
     * Initialization method that will be called after the applet is loaded into
     * the browser.
     */
    int i=200;int j;
    int counter=0;
    double q;
    boolean b=true,a=true;
    Thread t;
    Button star,stop,resume,reset;
    public void init() {
        star=new Button("START");
        
        stop=new Button("stop");add(stop);
        stop.addActionListener(this);
        resume=new Button("resume");add(resume);
        resume.addActionListener(this);
        reset=new Button("reset");add(reset);
        reset.addActionListener(this);
        add(star);
        star.addActionListener(this);
        t=new Thread(this);
        
        // TODO start asynchronous download of heavy resources
    }public void run(){
        try
        {
            for(i=200;i<=300;i=i+10){
            repaint();
            counter++;
            
        Thread.sleep(300);}
            
            do{
            for(i=300;i>=100;i=i-10){
            repaint();
        Thread.sleep(300);
            counter++;}
        for(i=100;i<=300;i=i+10){
            repaint();
        Thread.sleep(300);}
        
         counter++;
        }while(b);}
        catch(InterruptedException e){System.out.println("gaurav");}
    
    
    
    
    
    }
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==star){
            if(a!=false)
            t.start();
            else
            {t.resume();t.notify();}
        }
        else if(ae.getSource()==stop)
        {t.suspend();
        
        double p=(double)counter/11;
        q=p*3.92;
        JOptionPane.showMessageDialog(null,String.valueOf(q)+"sec");
        }
        else if(ae.getSource()==resume){t.resume();
        t.notify();}
        else if(ae.getSource()==reset){counter=0;i=200;j=300;t.suspend();
        a=false;
       }
    }
    // TODO overwrite start(), stop() and destroy() methods
    public void paint(Graphics g)
    {g.setColor(Color.ORANGE);
   // g.drawString(String.valueOf(q)+"sec",50,20);
        g.drawLine(100, 100, 300, 100);
        g.drawRect(75,100, 250,300);if(i<=300&&i>250){g.setColor(Color.blue);
    setBackground(Color.GRAY);
    int k=i-200;
    double l=Math.pow(k,2);
    double m=40000-l;
       double n=Math.sqrt(m);
          int  j=(int)n+100;
    
    g.drawLine(200,100,i,j);
    g.setColor(Color.red);
    
    g.fillOval(i-10,j-10,20,20);
    }else if(i<=250&&i>200)
    {g.setColor(Color.white);
    setBackground(Color.GRAY);
    int k=i-200;
    double l=Math.pow(k,2);
    double m=40000-l;
       double n=Math.sqrt(m);
          int  j=(int)n+100;
    
    g.drawLine(200,100,i,j);
    g.setColor(Color.black);
    
    g.fillOval(i-10,j-10,20,20);
    }
    else if(i>150&&i<=200){g.setColor(Color.yellow);
    setBackground(Color.white);
    int k=i-200;
    double l=Math.pow(k,2);
    double m=40000-l;
       double n=Math.sqrt(m);
          int  j=(int)n+100;
    
    g.drawLine(200,100,i,j);
    g.setColor(Color.blue);
    
    g.fillOval(i-10,j-10,20,20);
    }
    else{g.setColor(Color.red);
    setBackground(Color.black);
    int k=i-200;
    double l=Math.pow(k,2);
    double m=40000-l;
       double n=Math.sqrt(m);
          int  j=(int)n+100;
    
    g.drawLine(200,100,i,j);
    g.setColor(Color.yellow);
    
    g.fillOval(i-10,j-10,20,20);
    }
    }
}
 

Game Of Life

HTML Online Editor Sample

 

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameoflife;
 
/**
 *
 * @author gaurav
 */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Gameoflife extends Applet implements ActionListener,Runnable{
    Button [][]square;
    Thread []t;
    int i=0,j=0,k=0,l,s=1,p=0,q;
    Button set,start,stop;
    int [][]status;
    int [][]coun;
    int y=0;
    public void init(){setLayout(null);
    status=new int[40][40];
    coun=new int[40][40];
    for(int i=0;i<40;i++){for(int j=0;j<40;j++){status[i][j]=0;}}
    for(int i=0;i<40;i++){for(int j=0;j<40;j++){coun[i][j]=0;}}
    set=new Button("set");
    add(set);
    set.addActionListener(this);
    set.setBackground(Color.red);
    set.reshape(300,640,100,20);
    start=new Button("START"); stop=new Button("stop");
    add(stop);
    stop.addActionListener(this);
    stop.setBackground(Color.yellow);
    stop.reshape(420,640,100,20);
    
    add(start);start.addActionListener(this);
    start.reshape(550,640,100,20);
    start.setBackground(Color.green);
    square=new Button[40][40];
    for(int i=0;i<40;i++){
    for(int j=0;j<40;j++)
    {square[i][j]=new Button();
    add(square[i][j]);square[i][j].setBackground(Color.white);
    square[i][j].reshape(200+(15*j), 20+(15*i), 15, 15);
    square[i][j].addActionListener(this);
    
    }}
    t=new Thread[100];
   for(int x=0;x<100;x++) 
   t[x]=new Thread(this) ;
    
    
    }
    public void run()
    
    {try{do{
        Thread.sleep(300);
    for(int i=1;i<39;i++){for(int j=1;j<39;j++)
    
    {coun[i][j]=0;
    if(status[i+1][j]==1)
        coun[i][j]++;
    if(status[i][j+1]==1)
        coun[i][j]++;
    if(status[i-1][j]==1)
        coun[i][j]++;
    if(status[i][j-1]==1)
        coun[i][j]++;
    if(status[i+1][j+1]==1)
        coun[i][j]++;
    if(status[i-1][j+1]==1)
        coun[i][j]++;
    if(status[i-1][j-1]==1)
        coun[i][j]++;
    if(status[i+1][j-1]==1)
        coun[i][j]++;}} 
    if(status[1][0]==1)coun[0][0]++;
    if(status[1][1]==1)coun[0][0]++;
    if(status[0][1]==1)coun[0][0]++;
    if(status[1][39]==1)coun[0][39]++;
    if(status[1][38]==1)coun[0][39]++;
    if(status[0][38]==1)coun[0][39]++;
    if(status[38][0]==1)coun[39][0]++;
    if(status[38][1]==1)coun[39][0]++;
    if(status[39][1]==1)coun[39][0]++;
    if(status[38][38]==1)coun[39][39]++;
    if(status[38][39]==1)coun[39][39]++;
    if(status[39][38]==1)coun[39][39]++;
    for(int a=1;a<39;a++)
    {if(status[0][a+1]==1)coun[0][a]++;
    if(status[0][a-1]==1)coun[0][a]++;
    if(status[1][a]==1)coun[0][a]++;
    if(status[1][a-1]==1)coun[0][a]++;
    if(status[1][a+1]==1)coun[0][a]++;
    
    
    }
     for(int a=1;a<39;a++)
    {if(status[39][a+1]==1)coun[39][a]++;
    if(status[39][a-1]==1)coun[39][a]++;
    if(status[38][a]==1)coun[39][a]++;
    if(status[38][a-1]==1)coun[39][a]++;
    if(status[38][a+1]==1)coun[39][a]++;
    
    
    }
     for(int a=1;a<39;a++)
    {if(status[a+1][0]==1)coun[a][0]++;
    if(status[a-1][0]==1)coun[a][0]++;
    if(status[a+1][1]==1)coun[a][0]++;
    if(status[a][1]==1)coun[a][0]++;
    if(status[a+1][1]==1)coun[a][0]++;
    
    
    }
      for(int a=1;a<39;a++)
    {if(status[a+1][39]==1)coun[a][39]++;
    if(status[a-1][39]==1)coun[a][39]++;
    if(status[a+1][38]==1)coun[a][39]++;
    if(status[a][38]==1)coun[a][39]++;
    if(status[a-1][38]==1)coun[a][39]++;
    
    
    }
    
    for(int i=1;i<39;i++){for(int j=1;j<39;j++){
    if(coun[i][j]<2&&status[i][j]==1){
        square[i][j].setBackground(Color.white);status[i][j]=0;}
    else if(coun[i][j]>=2&&coun[i][j]<=3&&status[i][j]==1){}
    else if(coun[i][j]>3&&status[i][j]==1){status[i][j]=0;square[i][j].setBackground(Color.white);}
    else if(coun[i][j]==3&&status[i][j]==0){status[i][j]=1;square[i][j].setBackground(Color.black);}
    
    
    
    }}
    
    
    
  //  Thread.sleep(300);
    } while(true);
    }
    catch(InterruptedException e)
    {System.out.println("gaurav");
    }
    
    
    }
    public void actionPerformed(ActionEvent ae){
    if(ae.getSource()==set)
    {y++;
    s=1;p=0;
    repaint();for(int i=0;i<40;i++){
    for(int j=0;j<40;j++){
    square[i][j].setBackground(Color.white);
    coun[i][j]=0;
     status[i][j]=0;}}}
    for(int i=0;i<40;i++){
    for(int j=0;j<40;j++){if(ae.getSource()==square[i][j]){square[i][j].setBackground(Color.black);
    status[i][j]=1;
    }}}
    
    if(ae.getSource()==start)
    {s=0;p=0;repaint();
        t[y].start();
        
    
    }
    if(ae.getSource()==stop){
        p=1;s=0;repaint();
    t[y].stop();
    t.notify();
    
    }
    
    }
    public void paint(Graphics g)
    {
   
    if(s==1){ g.setColor(Color.CYAN);
        g.fillRect(350, 0, 300, 30);
         g.setColor(Color.blue);
    g.drawString("SET THE DESIGN", 400,15);}
    if(p==1){ g.setColor(Color.CYAN);g.fillRect(350, 0, 300, 30);
     g.setColor(Color.blue);
    g.drawString("TO START AGAIN PRESS SET BUTTON", 375, 15);}
    
    
    }
   
}