Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
</applet>*/
import java.awt.*;
import java.applet.*;
import java.lang.String.*;
int n=0;
String label[];
int value[];
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");
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:
91
92
Bouncing ball
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
</applet>*/
class Ball
int x,y,radius,dx,dy;
Color BallColor;
this.x=x;
this.y=y;
this.radius=radius;
this.dx=dx;
this.dy=dy;
BallColor=bColor;
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);
t.start();
g.setColor(redBall.BallColor);
g.setColor(blackBall.BallColor);
while(true)
try{
displacementOperation(redBall);
displacementOperation(blackBall);
Thread.sleep(20);
repaint();
catch(InterruptedException e){}
}
}
clock
import java.applet.*;
import java.awt.*;
import java.util.*;
Thread t,t1;
t=new Thread(this);
t.start();
t1=Thread.currentThread();
while(t1==t)
repaint();
try
t1.sleep(1000);
}
}
String hour=String.valueOf(cal.get(Calendar.HOUR));
String minute=String.valueOf(cal.get(Calendar.MINUTE));
String second=String.valueOf(cal.get(Calendar.SECOND));
}
flag
import java.applet.*;
import java.awt.*;
import java.lang.*;
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.*;
int x[]={80,200,300};
int y[]={120,40,120};
int n=x.length;
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.*;
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.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>*/
implements ActionListener
String msg="";
int v1,v2,result;
TextField t1;
Button add,sub,mul,div,clear,mod,EQ;
char OP;
setBackground(k);
t1=new TextField(10);
setLayout(gl);
for(int i=0;i<10;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);
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.*;
int value1=10;
int value2=20;
int sum=value1+value2;
g.drawString(s,100,100);
int Mul=value1*value2;
g.drawString(m,100,120);