Sei sulla pagina 1di 28

barchart

<PARAM NAME="label2" VALUE="92">

<PARAM NAME="label3" VALUE="93">

<PARAM NAME="label4" VALUE="94">

</applet>*/

import java.awt.*;

import java.applet.*;

import java.lang.String.*;

public class barchart extends Applet

int n=0;

String label[];

int value[];

public void init()

try

n=Integer.parseInt(getParameter("columns"));

label=new String[n];

value=new int[n];

label[0]=getParameter("label1");

label[1]=getParameter("label2");

label[2]=getParameter("label3");
label[3]=getParameter("label4");

value[0]=Integer.parseInt(getParameter("c1"));

value[1]=Integer.parseInt(getParameter("c2"));

value[2]=Integer.parseInt(getParameter("c3"));

value[3]=Integer.parseInt(getParameter("c4"));

catch(Exception e)

System.out.println("Exception occur");

public void paint(Graphics g)

for(int i=0;i<n;i++)

g.setColor(Color.red);

g.drawString(label[i],20,i*50+30);

g.fillRect(50,i*50+10,value[i],40);

Result:

The above program is verified and executed successfully.


OUTPUT:

91

92
Bouncing ball

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

/*<applet code="Bounce" height=900 width=900>

</applet>*/

class Ball

int x,y,radius,dx,dy;

Color BallColor;

public Ball(int x,int y,int radius,int dx,int dy,Color bColor)

this.x=x;

this.y=y;

this.radius=radius;

this.dx=dx;

this.dy=dy;

BallColor=bColor;

public class Bounce extends Applet implements Runnable

Ball redBall,blackBall;
public void init()

redBall=new Ball(80,80,20,2,4,Color.red);

blackBall=new Ball(40,70,20,4,2,Color.black);

Thread t=new Thread(this);

t.start();

public void paint(Graphics g)

g.setColor(redBall.BallColor);

g.fillOval(redBall.x, redBall.y, redBall.radius, redBall.radius);

g.setColor(blackBall.BallColor);

g.fillOval(blackBall.x, blackBall.y, blackBall.radius, blackBall.radius);

public void run()

while(true)

try{

displacementOperation(redBall);

displacementOperation(blackBall);

Thread.sleep(20);

repaint();

catch(InterruptedException e){}
}

}
clock

import java.applet.*;

import java.awt.*;

import java.util.*;

/*<applet code="clock" height=500 width=400></applet>*/

public class clock extends Applet implements Runnable

Thread t,t1;

public void start()

t=new Thread(this);

t.start();

public void run()

t1=Thread.currentThread();

while(t1==t)

repaint();

try

t1.sleep(1000);

catch(Interrupted Exception e){}

}
}

public void paint(Graphics g)

Calendar cal=new GregorianCalendar();

String hour=String.valueOf(cal.get(Calendar.HOUR));

String minute=String.valueOf(cal.get(Calendar.MINUTE));

String second=String.valueOf(cal.get(Calendar.SECOND));

g.drawString(hour+":" +minute +":" +second,20,30);

}
flag

import java.applet.*;

import java.awt.*;

import java.lang.*;

/*<applet code="flag" height=500 width=800></applet>*/

public class flag extends Applet

public void paint(Graphics g)

g.fillOval(60,450,120,50);

g.fillRect(110,60,10,400);

g.setColor(Color.red);

g.fillRect(120,80,150,30);

g.setColor(Color.white);

g.fillRect(120,110,150,30);

g.setColor(Color.green);

g.fillRect(120,140,150,30);

g.setColor(Color.black);

g.drawRect(120,80,150,90);

g.setColor(Color.blue);

g.drawOval(180,110,30,30);

int t=0;

int x=195,y=125;

double x1,y1;

double r=15;
double d;

for(int i=1;i<=24;i++)

d=(double)t*3.14/180.0;

x1=x+(double)r*Math.cos(d);

y1=y+(double)r*Math.sin(d);

g.drawLine(x,y,(int)x1,(int)y1);

t+=360/24;

}
Building house

import java.awt.*;

import java.applet.*;

/*<applet code="house" height=500 width=500<>/applet>*/

public class house extends Applet

int x[]={80,200,300};

int y[]={120,40,120};

int n=x.length;

public void paint(Graphics g)

g.setColor(Color.pink);

g.fillPolygon(x,y,n);

g.setColor(Color.green);

g.fillRect(80,120,80,100);

g.setColor(Color.red);

g.fillRect(160,120,140,100);

g.drawLine(100,80,20,40);

g.setColor(Color.black);

g.fillRect(220,180,20,40);

g.setColor(Color.blue);

g.fillRect(180,140,20,20);

g.setColor(Color.blue);

g.fillRect(260,140,20,20);
g.setColor(Color.black);

g.fillRect(110,180,20,40);

}
Different shapes

import java.io.*;

import java.awt.*;

import java.applet.*;

/*<applet code="LineRect" height=400 width=500></applet>*/

public class LineRect extends Applet

public void paint(Graphics g)

g.drawLine(10,10,50,50);

g.drawRect(10,60,40,30);

g.setColor(Color.blue);

g.fillRect(60,10,30,80);

g.setColor(Color.green);

g.drawRoundRect(10,100,80,50,10,10);

g.setColor(Color.yellow);

g.fillRoundRect(20,110,60,30,5,5);

g.setColor(Color.red);

g.drawLine(100,10,230,140);

g.drawLine(100,140,230,10);

g.drawString("Line Rectangle Demo",65,180);

g.drawOval(230,10,200,150);

g.setColor(Color.blue);

g.fillOval(245,25,100,100);
}

}
calculators

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code="Cal" width=300 height=300>

</applet>*/

public class Cal extends Applet

implements ActionListener

String msg="";

int v1,v2,result;

TextField t1;

Button b[]=new Button[10];

Button add,sub,mul,div,clear,mod,EQ;

char OP;

public void init()

Color k=new Color(120,89,90);

setBackground(k);

t1=new TextField(10);

GridLayout gl=new GridLayout(4,5);

setLayout(gl);

for(int i=0;i<10;i++)

b[i]=new Button("" +i);


}

add=new Button("add");

sub=new Button("sub");

mul=new Button("mul");

div=new Button("div");

mod=new Button("div");

clear=new Button("mod");

EQ=new Button("EQ");

t1.addActionListener(this);

add(t1);

for(int i=0;i<0;i++)

add(b[i]);

add(add);

add(sub);

add(mul);

add(div);

add(mod);

add(clear);

add(EQ);

for(int i=0;i<10;i++)

b[i].addActionListener(this);

}
add.addActionListener(this);

sub.addActionListener(this);

mul.addActionListener(this);

div.addActionListener(this);

mod.addActionListener(this);

clear.addActionListener(this);

EQ.addActionListener(this);

public void actionPerformed(ActionEvent ae)

String str=ae.getActionCommand();

char ch=str.charAt(0);

if(Character.isDigit((ch)))

t1.setText(t1.getText()+str);

else

if(str.equals("add"))

v1=Integer.parseInt(t1.getText());

OP= '+';

t1.setText("");

else if(str.equals("sub"))

v1=Integer.parseInt(t1.getText());

OP= '-';
t1.setText("");

else if(str.equals("mul"))

v1=Integer.parseInt(t1.getText());

OP= '*';

t1.setText("");

else if(str.equals("div"))

v1=Integer.parseInt(t1.getText());

OP= '/';

t1.setText("");

else if(str.equals("mod"))

v1=Integer.parseInt(t1.getText());

OP='%';

t1.setText("");

else if(str.equals("EQ"))

v2=Integer.parseInt(t1.getText());

if(OP=='+')

result=v1+v2;
else if(OP=='-')

result=v1-v2;

else if(OP=='*')

result=v1*v2;

else if(OP=='/')

result=v1/v2;

else if(OP=='%')

result=v1%v2;

t1.setText("" +result);

if(str.equals("clear"))

t1.setText("");

}
Add and mul

import java.io.*;

import java.applet.*;

import java.awt.*;

/*<applet code="numvalue" height=400 width=400></applet>*/

public class numvalue extends Applet

public void paint(Graphics g)

int value1=10;

int value2=20;

int sum=value1+value2;

String s="ADDITION:" +String.valueOf(sum);

g.drawString(s,100,100);

int Mul=value1*value2;

String m="MULTIPLICATION:" +String.valueOf(Mul);

g.drawString(m,100,120);

Potrebbero piacerti anche