Showing posts with label applet. Show all posts
Showing posts with label applet. Show all posts

Saturday, March 15, 2014

moving ball in java



import java.applet.*;
import java.awt.*;
import java.util.Random;

public class Bouncing_ball extends Applet implements Runnable {


int x1=100,xrate=1;
int y1=200,yrate=3;
int radius1=20;
Random re;
Thread th;
public void start(){
setBackground(Color.red);
setSize(400,400);
re=new Random();
th=new Thread(this);
th.start();
}
int t=200;
public void run(){
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while( true){
if(x1<=0|| x1>=360){

xrate=-xrate;
if(yrate<0)
yrate=-re.nextInt(4);
else
yrate=re.nextInt(4);
}

if(y1<=0|| y1>=360){

yrate=-yrate;
if(xrate<0)
xrate=-re.nextInt(4);
else
xrate=re.nextInt(4);
}

x1+=xrate;
y1+=yrate;
repaint();
try{

Thread.sleep(20);
}
catch(InterruptedException ex){

}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}
}
public void paint(Graphics g){
g.setColor(Color.green);
g.fillOval(x1,y1,radius1*2,radius1*2);

}

}

Spiral motion code in java

import java.applet.*;
import java.awt.*;

public class Sprial extends Applet{
public void paint(Graphics g){
int x=0,y=10,xrate=240,yrate=210;
setBackground(Color.green);
g.setColor(Color.black);
for(int i=1;i<=15;i++){
int m=x;
int n=y;
if(i%4==1){
x+=xrate;
xrate-=30;
}
else if(i%4==2){
y+=yrate;
yrate-=30;
}
else if(i%4==3){
x-=xrate;
xrate-=30;
}
else
{
y-=yrate;
yrate-=30;
}
g.drawLine(m, n, x, y);
}
}
}

Tuesday, March 11, 2014

cannon bal using appletl in java



import java.applet.Applet;
import java.awt.*;
import java.awt.color.*;
import javax.swing.*;
import javax.swing.event.*;

public class projectile extends Applet implements Runnable{

double x=0,y=0,u=110,t=0,m=40;
Thread th;
/* public void update (Graphics g)
{
paint(g);
}*/
public void paint(Graphics g)
{
setBackground(Color.yellow);
g.setColor(Color.BLUE);
g.fillOval((int)x, (int)y, 50, 50);
g.fillRect(0,500,1300,10);
}
public void init(){
setSize(1500,600);
}
public void start()
{
th=new Thread(this);
th.start();
}
public void run() {
double T=0;
while(t<=T)
{
m= Math.toRadians(m);
T=2*u*(Math.sin(m))/9.8;
x=100+u*(Math.cos(m))*t;
if(t<T/2)
y=500-(u*t*(Math.sin(m))-(0.5)*9.8*t*t);
else if(t>T/2)
y=500-(u*t*(Math.sin(m))-(0.5)*9.8*t*t);
else
y=y;
repaint();
m=Math.toDegrees(m);t=t+0.01;
try
{
Thread.sleep(4);
}
catch(InterruptedException ex)
{
}


}
}
}

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) {}

}

usefull links and tips

www.oracle.com
www.eclipse.com
www.filehippo.com


  • for c++ and c language DEV CPP compiler is good and you can download free 
  • for Assembly Language 8086 download Emulator 8086 or DOS Box
  • Eclipse and NetBean software for java

inverted pendulum using applet in java



import java.applet.Applet;
import java.awt.*;
import java.awt.color.*;
import javax.swing.*;
import javax.swing.event.*;
public class pendulum extends Applet implements Runnable {
double x=500,y=500,m=225;
Thread th;
public void init(){
setSize(500,500);
}
public void paint(Graphics g)
{
setBackground(Color.blue);
g.setColor(Color.red);
g.fillOval((int)x, (int)y, 50, 50);
g.drawLine(130,150,(int)x+25, (int)y+25);

}
public void start()
{
th=new Thread(this);
th.start();
}
public void run()
{
int turn=0;
while(true)
{

if(turn==0)
{
int count=0;
while(count<=90)
{
count++;
m=Math.toRadians(m);
x=100+100*Math.cos(m);
y=100+100*Math.sin(m);

repaint();
m=Math.toDegrees(m);m++;
try
{
Thread.sleep(10);
}
catch(InterruptedException ex)
{
}
}
turn=1;
}
else
{
int count=0;
while(count<=90)
{
m=Math.toRadians(m);
x=100+100*Math.cos(m);
y=100+100*Math.sin(m);
count++;
repaint();
m=Math.toDegrees(m);m--;
try
{
Thread.sleep(10);
}
catch(InterruptedException ex)
{
}
}


turn=0;
}
}
}

}

Saturday, March 8, 2014

sorting methods in java

import java.util.Scanner;


public class sorting {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


System.out.println("enter the elements ");
Scanner n=new Scanner(System.in);
int k = 1;
int[] a=new int[10];
int m=5;
a[0]=45;
a[1]=67;
a[2]=9;
a[3]=23;
a[4]=1;

System.out.println(" enter your choice of sorting\n1.bubble sort\n2.selection sort\n3.insertion sort ");
Scanner k=new scanner(System.in);
switch(k)
{

case 1: System.out.println(" sorted using bubble sort");

for(int i=0;i<m;i++)
{
for(int j=0;j<m-i-1;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<m;i++)
System.out.println(a[i]+" ");break;
case 2:
System.out.println(" sorted using selection sort ");

for(int i=0;i<m;i++)
{
int loc = 0;
int temp=a[i];
int f=0;
for(int j=i+1;j<m;j++)
{
if(temp>a[i])
{
f=1;
loc=j;

}
}
if(f==1)
{
a[loc]=a[i];
a[i]=temp;
}
}
}
}
}

HISTOGRAM using applet in JAVA




import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
public class histogram extends Applet implements ActionListener{


 Button increase, decrease;             // These are two buttons
TextField enter;
String str;
private int uvwl=0,lvwl=0,ucnt=0,lcnt=0,other=0;

 public void init ()
  {
 setSize(1000,700);
  setLayout(new FlowLayout());
  increase = new Button("GO");
  increase.addActionListener(this);
    add(increase);
    enter=new TextField(11);
 
    add(enter);
    decrease = new Button("QUIT");
    decrease.addActionListener(this);
    add(decrease);
  }

 private static int CountUpperCasevwl(String str)
   {
    int count=0;
    for(int i=0;i<str.length();i++)
    {
    char ch=str.charAt(i);
    if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
    count++;
   
    }
    return count;
   }
 private static int CountlowerCasevwl(String str)
   {
    int count=0;
    for(int i=0;i<str.length();i++)
    {
    char ch=str.charAt(i);
    if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
    count++;
   
    }
    return count;
   }
 private static int CountUpperCasecnt(String str)
   {
    int count=0;
    for(int i=0;i<str.length();i++)
    {
    char ch=str.charAt(i);
    if(ch>='A'&&ch<='Z'&&ch!='A'&&ch!='E'&&ch!='I'&&ch!='O'&&ch!='U')
    count++;
   
    }
    return count;
   }
 private static int CountlowerCasecnt(String str)
   {
    int count=0;
    for(int i=0;i<str.length();i++)
    {
    char ch=str.charAt(i);
    if(ch>='a'&&ch<='z'&&ch!='a'&&ch!='e'&&ch!='i'&&ch!='o'&&ch!='u')
    count++;
   
    }
    return count;
   }


 
public void actionPerformed(ActionEvent a) {
// TODO Auto-generated method stub
if(a.getSource()==increase){
str=enter.getText();
uvwl=CountUpperCasevwl(str);
lvwl=CountUpperCasecnt(str);
ucnt=CountlowerCasevwl(str);
lcnt=CountlowerCasecnt(str);
             other=str.length()-uvwl-lvwl-ucnt-lcnt;
}
else if(a.getSource()==decrease)
{
System.exit(3);
}
repaint();

}
public void paint(Graphics g)
{

setBackground(Color.gray);
g.drawRect(300, 100, 500, 500);
g.setColor(Color.blue);
g.drawString("UVWL", 320, 590);
g.drawString("LVWL", 380, 590);
g.drawString("UCNT", 440, 590);
g.drawString("LCNT", 500, 590);
g.drawString("OTHER", 560, 590);
for(int i=0;i<10;i++)
{

String str = Integer.toString(i);
g.drawString(str,305,590-(20*i));
}


g.setColor(Color.red);
g.fillRect(320, 580-( 20*uvwl),30 , 20*uvwl);
g.fillRect(380, 580-(20*lvwl), 30, 20*lvwl);
g.fillRect(440, 580-(20*ucnt), 30, 20*ucnt);
g.fillRect(500, 580-(20*lcnt), 30, 20*lcnt);
g.fillRect(560, 580-(20*other), 30, 20*other);
}

}


Friday, March 7, 2014

Projectile motion implementation using applet java

A ball moving in projectile motion.means x and y cordinates should change according the projectilemotion rules.



import java.applet.Applet;
import java.awt.*;
import java.awt.color.*;
import javax.swing.*;
import javax.swing.event.*;

public class projectile extends Applet implements Runnable{

double x=0,y=0,u=110,t=0,m=40;
Thread th;
public void paint(Graphics g)
{
setBackground(Color.yellow);
g.setColor(Color.BLUE);
g.fillOval((int)x, (int)y, 50, 50);
g.fillRect(0,500,1300,10);
}
public void init(){
setSize(1500,600);
}
public void start()
{
th=new Thread(this);
th.start();
}
public void run() {
double T=0;
while(t<=T)
{
m= Math.toRadians(m);
T=2*u*(Math.sin(m))/9.8;
x=100+u*(Math.cos(m))*t;
if(t<T/2)
y=500-(u*t*(Math.sin(m))-(0.5)*9.8*t*t);
else if(t>T/2)
y=500-(u*t*(Math.sin(m))-(0.5)*9.8*t*t);
else
y=y;
repaint();
m=Math.toDegrees(m);t=t+0.01;
try
{
Thread.sleep(10);
}
catch(InterruptedException ex)
{
}


}
}
}

CONWAY's GAME OF LIFE USING BUTTON in JAVA

                                GAME OF LIFE is a very common problem in which you have to create a grid of button ( so called cells) . Some of these cells are alive and others are dead.
there are three rules for a cell which decides the cell will be alive in next step or dead.

1. If a perticular cell is alive and surrounded by 3 or more than 3 it will die due to over crouding . If cell is surrounded by less than 2 alive then it will die due to lonleyness.

2.If the perticular cell is dead then it will alive in next step if it is surrounded by exactly 2 alive cells.
 
  1st step                                                      nest step
alive(surrounded by 3 or more than 3)           dead
alive (surrounded by less then 2)                   dead
dead(surrounded by exactly 2)                      alive



take a button array
  Button[][] grid=new Button[10][10];    //create a view
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++){
n[i][j]=0;
grid[i][j]=new Button();
add(grid[i][j]);
grid[i][j].addActionListener(this);
}
}

setLayout(new GridLayout(11,10));

take a 2-D array which stores 1 OR 0. 1 means alive and 0 means dead. array 'n'.

now repaete the function a number of time. like 5 times. in the  public void actionPerformed(ActionEvent a) function.


public void actionPerformed(ActionEvent a)
{

for(int i=1;i<9;i++)
{

for(int j=1;j<9;j++)
{
if(a.getSource()==grid[i][j])
{
n[i][j]=1;
grid[i][j].setBackground(Color.red);
grid[i][j].setEnabled(true);
}
}
}


go.setBackground(Color.black);
go.setEnabled(true);
int h=0;
while(h<=20)
{ h++;
int count=0;
int[][] loc=new int[20][3];
int life[]=new int[40];

for(int k=1;k<9;k++)
{
for(int l=1;l<9;l++)
{

int ctr=0;
int ut1=3,ut2=3,x=0,y=0;


x=k-1;
y=l-1;

for(int c=x;c<ut1+x;c++)
{
for(int b=y;b<ut2+y;b++)
{
if(n[c][b]==1)
{ ctr++;

}
}
}
if(n[k][l]==1)
ctr--;

if(n[k][l]==1)
{

if(ctr<2||ctr>3)
{
loc[count][0]=k;
loc[count][1]=l;
life[count++]=0;
}
if(ctr==2)
{
loc[count][0]=k;
loc[count][1]=l;
life[count++]=1;
}

}
else
{

if(ctr==3)
{
loc[count][0]=k;
loc[count][1]=l;
life[count++]=1;
}

}
}
    }
for(int i1=0;i1<count;i1++)
{
if(life[i1]==0)
{
n[loc[i1][0]][loc[i1][1]]=0;

grid[loc[i1][0]][loc[i1][1]].setBackground(Color.white);
grid[loc[i1][0]][loc[i1][1]].setEnabled(true);
}

else if(life[i1]==1)
{
n[loc[i1][0]][loc[i1][1]]=1;
grid[loc[i1][0]][loc[i1][1]].setBackground(Color.red);
grid[loc[i1][0]][loc[i1][1]].setEnabled(true);
}
}

try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
}
}


}

TOWER OF HANOI USING APPLET in JAVA

                                                    TOWER OF HANOI  is very common program all over the world it is taught by the institutes as a very basic program. there are 'n' number of plates which are placed over a pig(stick) such a way smaller over the bigger one. We have to move the all disks to third pig (receiver pig ) using the temperary pig (mid pig) from the first pig (container pig) .
.There are two rules to do.
1.smaller disk over bigger only
2.only one disk moves at a time

There are three steps to do this problem.
     1.store the movement of disks using the below function called hanoi in a 2-D array. count is a counter.

'n' is the storing array.
    
     public  void hanoi(int m,char a,char b,char c)
{

if(m>0)
{
hanoi(m-1,a,c,b);
n[count][0]=a;
n[count][1]=c;
count++;
hanoi(m-1,b,a,c);
}
}
     
     now we got the position of all the disks in each step. 
     
     2.create the stable structure using applet . Stable structures are pig and the base
     call the paint function to paint all this pigs(sticks) and plates.
    to do this we take three 1-D arrays like  a[20],b[20],c[20] . initial we put n,n-1,n-2,.....1 numbers       in 'a' array.
    then we move these numbers using the storing array 'n' by the below function.
   
  public void run() {

for(int i=0;i<count;i++)
{
repaint();

if(n[i][0]=='a'&&n[i][1]=='b')
{
a[1][p2++]=a[0][p1];
p1--;
}
else if(n[i][0]=='a'&&n[i][1]=='c')
{

a[2][p3++]=a[0][p1];
p1--;
}
else if(n[i][0]=='b'&&n[i][1]=='c')
{

p2--;
a[2][p3++]=a[1][p2];

}
else if(n[i][0]=='b'&&n[i][1]=='a')
{
p1++;
p2--;
a[0][p1]=a[1][p2];

}
else if(n[i][0]=='c'&&n[i][1]=='a')
{
p3--;p1++;
a[0][p1]=a[2][p3];

}
else if(n[i][0]=='c'&&n[i][1]=='b')
{
p3--;
a[1][p2++]=a[2][p3];

}
try
{
Thread.sleep(100);
}
catch(InterruptedException ex)
{
}

}
}  
        
        3. Create a paint function such a way that pigs and base should remain as it is and disk should be created acording the number in perticular array.
ex. initially all the disks are at pig-1 so all disks should appear at 1st position. all the numbers are in 'a' array.


 public  void paint(Graphics g)
{
ctr++;
setBackground(Color.yellow);
g.setColor(Color.green);
g.fillRect(380, 500, 800,20);
for(int i=0;i<3;i++)
g.fillRect(500+245*i, 100, 10, 400);
g.setColor(Color.blue);
Font myFont = new Font("Serif", Font.BOLD, 22);
g.setFont( myFont);
g.drawString("time"+ctr, 380, 20);
 
  g.setColor(Color.black);
  for(int j=0;j<=p1;j++)
  g.fillRect(500-(a[0][j]*10), 500-21*(j+1), 20*a[0][j], 20);
  
  for(int j=0;j<p2;j++)
  g.fillRect(750-(a[1][j]*10), 500-21*(j+1), 20*a[1][j], 20);
  
  for(int j=0;j<p3;j++)
  g.fillRect(1000-(a[2][j]*10), 500-21*(j+1), 20*a[2][j], 20);
  

   }



Contributors

Translate