/*<applet code=wheelrot width=1000 height=600></applet>*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class wheelrot extends Applet implements ActionListener
{
TextField t1,t2,t3;
Button b;
public void init()
{
//setBackground(Color.red);
Label l1=new Label("xc(0to1000)");
Label l2=new Label("yc(0to600)");
Label l3=new Label("radius");
t1=new TextField(4);
t2=new TextField(4);
t3=new TextField(4);
b=new Button("start");
add(l1);add(t1);
add(l2);add(t2);
add(l3);add(t3);
add(b);
b.addActionListener(this);
}
public void paint(Graphics g)
{
Graphics g1=g;
int xc,yc,i,r,k;
double angle,pi,d;
pi=3.14;
angle=pi/180;
xc=Integer.parseInt(t1.getText());
yc=Integer.parseInt(t2.getText());
r=Integer.parseInt(t3.getText());
while(xc!=0)
{
for(double j=0;j<100000;j+=.005);
g.setColor(Color.black);
g.fillRect(1,1,1360,1000);
g.setColor(Color.green);
drwLine(1,yc+r,1360,yc+r,g1);
drwOval(xc,yc,r,g1);
drwLine((int)(xc+r*Math.sin(angle)),(int)(yc-r*Math.cos(angle)),(int)(xc-r*Math.sin(angle)),(int)(yc+r*Math.cos(angle)),g1);
drwLine((int)(xc-r*Math.cos(angle)),(int)(yc-r*Math.sin(angle)),(int)(xc+r*Math.cos(angle)),(int)(yc+r*Math.sin(angle)),g1);
//drwLine((int)(xc+r*Math.sin(angle+3.14/4)),(int)(yc-r*Math.cos(angle+3.14/4)),(int)(xc-r*Math.sin(angle+3.14/4)),(int)(yc+r*Math.cos(angle+3.14/4)),g1);
//drwLine((int)(xc-r*Math.cos(angle-3.14/4)),(int)(yc-r*Math.sin(angle-3.14/4)),(int)(xc+r*Math.cos(angle-3.14/4)),(int)(yc+r*Math.sin(angle-3.14/4)) ,g1);
xc+=10*pi*r/180;
angle=angle+10*pi/180;
if(xc>(1000-r))
xc=20;
}
}
public void actionPerformed(ActionEvent awt)
{
repaint();
}
public void drwLine(int c,int d,int e,int f,Graphics g2)
{
int x1=c;
int y1=d;
int x2=e;
int y2=f;
int step;
float xinc,yinc,x,y;
int dy=y2-y1;
int dx=x2-x1;
if(dx>dy)
step=dx;
else
step=dy;
xinc=(float)dx/step;
yinc=(float)dy/step;
x=x1;
y=y1;
g2.drawString(".",(int)x,(int)y);
for(int k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
g2.drawString(".",(int)x,(int)y);
}
}
public void drwOval( int a, int b , int r, Graphics g1)
{
int xc=a;
int yc=b;
int radious=r;
int x=0;
int y=radious;
float p=5/4 - r;
do
{
g1.drawString(".",Math.round(xc+x),Math.round(yc-y));
g1.drawString(".",Math.round(xc+y),Math.round(yc-x));
g1.drawString(".",Math.round(xc+y),Math.round(yc+x));
g1.drawString(".",Math.round(xc+x),Math.round(yc+y));
g1.drawString(".",Math.round(xc-x),Math.round( yc+y));
g1.drawString(".",Math.round(xc-y),Math.round(yc+x));
g1.drawString(".",Math.round(xc-y),Math.round(yc-x));
g1.drawString(".",Math.round(xc-x),Math.round( yc-y));
if(p<0)
{
x=x+1;
p=p+(2*x)+3;
}
else
{
x=x+1;
y=y-1;
p=p-2*y+2*x+5;
}
}
while(x<=y);
}
}
JAVA APPLET
Thursday, 16 January 2014
TRANSLATION OF STRAIGHT LINE
/*<applet code=translation width=1300 height=800></applet>*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class translation extends Applet implements ActionListener
{
Label l1=new Label("Enter the two end points of the line[x1,x2,y1,y2]: ");
Label l2=new Label("Enter translation factor [tx,ty]: ");
Label l3=new Label("Enter rotation angle and rotation factor[Q,rx,ry]: ");
Label l4=new Label("Enter shearing factor[sx,sy]:");
TextField t1=new TextField(3);
TextField t2=new TextField(3);
TextField t3=new TextField(3);
TextField t4=new TextField(3);
TextField t5=new TextField(3);
TextField t6=new TextField(3);
TextField t7=new TextField(3);
TextField t8=new TextField(3);
TextField t9=new TextField(3);
TextField t10=new TextField(3);
TextField t11=new TextField(3);
Button b1=new Button("Show");
Button b2=new Button("Show");
Button b3=new Button("Show");
Button b4=new Button("Show");
int x1,x2,y1,y2,tx,ty,Q,rx,ry,sx,sy,nx1,nx2,ny1,ny2,t=0;
public void init()
{
add(l1);
add(t1);
add(t2);
add(t3);
add(t4);
add(b1);
add(l2);
add(t5);
add(t6);
add(b2);
add(l3);
add(t7);
add(t8);
add(t9);
add(b3);
add(l4);
add(t10);
add(t11);
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void paint(Graphics g)
{
x1=Integer.parseInt(t1.getText());
x2=Integer.parseInt(t2.getText());
y1=Integer.parseInt(t3.getText());
y2=Integer.parseInt(t4.getText());
g.drawLine(x1,y1,x2,y2);
if(t==1)
g.drawLine(nx1,ny1,nx2,ny2);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
showStatus("Original");
repaint();
}
if(ae.getSource()==b2)
{
tx=Integer.parseInt(t5.getText());
ty=Integer.parseInt(t6.getText());
showStatus("After translation");
t=1;
nx1=x1+tx;
ny1=y1+ty;
nx2=x2+tx;
ny2=y2+ty;
repaint();
}
if(ae.getSource()==b3)
{
Q=Integer.parseInt(t7.getText());
rx=Integer.parseInt(t8.getText());
ry=Integer.parseInt(t9.getText());
showStatus("After rotation");
t=1;
double d=Math.toRadians(Q);
nx1=rx+(int)((x1-rx)*Math.cos(d))-(int)((y1-ry)*Math.sin(d));
nx2=rx+(int)((x2-rx)*Math.cos(d))-(int)((y2-ry)*Math.sin(d));
ny1=ry+(int)((x1-rx)*Math.sin(d))+(int)((y1-ry)*Math.cos(d));
ny2=ry+(int)((x2-rx)*Math.sin(d))+(int)((y2-ry)*Math.cos(d));
repaint();
}
if(ae.getSource()==b4)
{
sx=Integer.parseInt(t10.getText());
sy=Integer.parseInt(t11.getText());
showStatus("scaling");
t=1;
nx1=x1*sx;
nx2=x2*sx;
ny1=y1*sy;
ny2=y2*sy;
repaint();
}
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class translation extends Applet implements ActionListener
{
Label l1=new Label("Enter the two end points of the line[x1,x2,y1,y2]: ");
Label l2=new Label("Enter translation factor [tx,ty]: ");
Label l3=new Label("Enter rotation angle and rotation factor[Q,rx,ry]: ");
Label l4=new Label("Enter shearing factor[sx,sy]:");
TextField t1=new TextField(3);
TextField t2=new TextField(3);
TextField t3=new TextField(3);
TextField t4=new TextField(3);
TextField t5=new TextField(3);
TextField t6=new TextField(3);
TextField t7=new TextField(3);
TextField t8=new TextField(3);
TextField t9=new TextField(3);
TextField t10=new TextField(3);
TextField t11=new TextField(3);
Button b1=new Button("Show");
Button b2=new Button("Show");
Button b3=new Button("Show");
Button b4=new Button("Show");
int x1,x2,y1,y2,tx,ty,Q,rx,ry,sx,sy,nx1,nx2,ny1,ny2,t=0;
public void init()
{
add(l1);
add(t1);
add(t2);
add(t3);
add(t4);
add(b1);
add(l2);
add(t5);
add(t6);
add(b2);
add(l3);
add(t7);
add(t8);
add(t9);
add(b3);
add(l4);
add(t10);
add(t11);
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void paint(Graphics g)
{
x1=Integer.parseInt(t1.getText());
x2=Integer.parseInt(t2.getText());
y1=Integer.parseInt(t3.getText());
y2=Integer.parseInt(t4.getText());
g.drawLine(x1,y1,x2,y2);
if(t==1)
g.drawLine(nx1,ny1,nx2,ny2);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
showStatus("Original");
repaint();
}
if(ae.getSource()==b2)
{
tx=Integer.parseInt(t5.getText());
ty=Integer.parseInt(t6.getText());
showStatus("After translation");
t=1;
nx1=x1+tx;
ny1=y1+ty;
nx2=x2+tx;
ny2=y2+ty;
repaint();
}
if(ae.getSource()==b3)
{
Q=Integer.parseInt(t7.getText());
rx=Integer.parseInt(t8.getText());
ry=Integer.parseInt(t9.getText());
showStatus("After rotation");
t=1;
double d=Math.toRadians(Q);
nx1=rx+(int)((x1-rx)*Math.cos(d))-(int)((y1-ry)*Math.sin(d));
nx2=rx+(int)((x2-rx)*Math.cos(d))-(int)((y2-ry)*Math.sin(d));
ny1=ry+(int)((x1-rx)*Math.sin(d))+(int)((y1-ry)*Math.cos(d));
ny2=ry+(int)((x2-rx)*Math.sin(d))+(int)((y2-ry)*Math.cos(d));
repaint();
}
if(ae.getSource()==b4)
{
sx=Integer.parseInt(t10.getText());
sy=Integer.parseInt(t11.getText());
showStatus("scaling");
t=1;
nx1=x1*sx;
nx2=x2*sx;
ny1=y1*sy;
ny2=y2*sy;
repaint();
}
}
}
ANALOG CLOCK
/*<applet code=clock.class height=300 width=400></applet>*/
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.image.*;
import java.lang.*;
public class clock extends Applet implements ActionListener
{
int hour;
int min;
int second;
float h,m,s;
Label label1=new Label("hour");
Label label2=new Label("minute");
Label label3=new Label("second");
TextField text1=new TextField(3);
TextField text2=new TextField(3);
TextField text3=new TextField(3);
Button button=new Button("start");
public void init()
{
add(label1);
add(text1);
add(label2);
add(text2);
add(label3);
add(text3);
add(button);
button.addActionListener(this);
second=-90;
min=-90;
hour=-90;
}
public void paint(Graphics g)
{
g.setColor(Color.black);
int radius=91,j=0;
String num[]={"3","4","5","6","7","8","9","10","11","12","1","2"};
midcircle(202,147,103,g);
for(int i=0;i<360;i+=30)
g.drawString(num[j++],(int)(202+radius*Math.cos(Math.PI*i/180)),(int)(147+radius*Math.sin(Math.PI*i/180)));
g.setColor(Color.red);
ddaline(202,147,(int)(202+(radius-20)*Math.cos(Math.PI*(int)hour/180)),(int)(147+(radius-20)*Math.sin(Math.PI*(int)hour/180)),g);
g.setColor(Color.blue);
ddaline(202,147,(int)(202+(radius-10)*Math.cos(Math.PI*min/180)),(int)(147+(radius-10)*Math.sin(Math.PI*min/180)),g);
g.setColor(Color.green);
ddaline(202,147,(int)(202+radius*Math.cos(Math.PI*second/180)),(int)(147+radius*Math.sin(Math.PI*second/180)),g);
g.setColor(Color.red);
second++;
if(second>270)
{
second=-90;
min+=6;
hour+=(float)1/2;
if(hour>270)
hour=-90;
if(min>270)
min=-90;
}
for(double r=0;r<100000000;r++);
for(double r=0;r<100000000;r++);
for(double r=0;r<100000000;r++);
for(double r=0;r<100000000;r++);
for(double r=0;r<100000000;r++);
for(double r=0;r<20000000;r++);
for(double r=0;r<20000000;r++);
for(double r=0;r<20000000;r++);
for(double r=0;r<20000000;r++);
for(double r=0;r<20000000;r++);
for(double r=0;r<10000;r++);
for(double r=0;r<10000;r++);
for(double r=0;r<10000;r++);
for(double r=0;r<10000;r++);
for(double r=0;r<10000;r++);
repaint();
}
public void actionPerformed(ActionEvent ae)
{h=Integer.parseInt(text1.getText());
m=Integer.parseInt(text2.getText());
s=Integer.parseInt(text3.getText());
min=(int)(6*m-90);
hour=(int)((30*((m/60)+h))-90);
second=(int)(6*s-90);
if(ae.getSource()==button)
{
repaint();
}}
public void ddaline(int x1,int y1,int x2,int y2,Graphics g1)
{
int dx=x2-x1,dy=y2-y1,step;
float xinc,yinc,x,y;
if(Math.abs(dx)>Math.abs(dy))
step=Math.abs(dx);
else
step=Math.abs(dy);
xinc=(float)dx/step;
yinc=(float)dy/step;
x=x1; y=y1;
g1.drawString(".",(int)x,(int)y);
for(int i=1;i<step;i++)
{
x=x+xinc;
y=y+yinc;
g1.drawString(".",(int)x,(int)y);
}
}
public void midcircle(int xc,int yc,int r,Graphics g2)
{
int x2=0;
int y2=r;
int p=5/4-r;
while(x2<y2)
{
if(p<0)
{
x2++;
p=p+2*x2+2+1;
}
else
{
x2++;
y2--;
p=p+2*x2+2+1-2*y2+2;
}
g2.drawString(".",xc+x2,yc+y2);
g2.drawString(".",xc+y2,yc+x2);
g2.drawString(".",xc-y2,yc+x2);
g2.drawString(".",xc-x2,yc+y2);
g2.drawString(".",xc+x2,yc-y2);
g2.drawString(".",xc+y2,yc-x2);
g2.drawString(".",xc-y2,yc-x2);
g2.drawString(".",xc-x2,yc-y2);
}
}
}
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.image.*;
import java.lang.*;
public class clock extends Applet implements ActionListener
{
int hour;
int min;
int second;
float h,m,s;
Label label1=new Label("hour");
Label label2=new Label("minute");
Label label3=new Label("second");
TextField text1=new TextField(3);
TextField text2=new TextField(3);
TextField text3=new TextField(3);
Button button=new Button("start");
public void init()
{
add(label1);
add(text1);
add(label2);
add(text2);
add(label3);
add(text3);
add(button);
button.addActionListener(this);
second=-90;
min=-90;
hour=-90;
}
public void paint(Graphics g)
{
g.setColor(Color.black);
int radius=91,j=0;
String num[]={"3","4","5","6","7","8","9","10","11","12","1","2"};
midcircle(202,147,103,g);
for(int i=0;i<360;i+=30)
g.drawString(num[j++],(int)(202+radius*Math.cos(Math.PI*i/180)),(int)(147+radius*Math.sin(Math.PI*i/180)));
g.setColor(Color.red);
ddaline(202,147,(int)(202+(radius-20)*Math.cos(Math.PI*(int)hour/180)),(int)(147+(radius-20)*Math.sin(Math.PI*(int)hour/180)),g);
g.setColor(Color.blue);
ddaline(202,147,(int)(202+(radius-10)*Math.cos(Math.PI*min/180)),(int)(147+(radius-10)*Math.sin(Math.PI*min/180)),g);
g.setColor(Color.green);
ddaline(202,147,(int)(202+radius*Math.cos(Math.PI*second/180)),(int)(147+radius*Math.sin(Math.PI*second/180)),g);
g.setColor(Color.red);
second++;
if(second>270)
{
second=-90;
min+=6;
hour+=(float)1/2;
if(hour>270)
hour=-90;
if(min>270)
min=-90;
}
for(double r=0;r<100000000;r++);
for(double r=0;r<100000000;r++);
for(double r=0;r<100000000;r++);
for(double r=0;r<100000000;r++);
for(double r=0;r<100000000;r++);
for(double r=0;r<20000000;r++);
for(double r=0;r<20000000;r++);
for(double r=0;r<20000000;r++);
for(double r=0;r<20000000;r++);
for(double r=0;r<20000000;r++);
for(double r=0;r<10000;r++);
for(double r=0;r<10000;r++);
for(double r=0;r<10000;r++);
for(double r=0;r<10000;r++);
for(double r=0;r<10000;r++);
repaint();
}
public void actionPerformed(ActionEvent ae)
{h=Integer.parseInt(text1.getText());
m=Integer.parseInt(text2.getText());
s=Integer.parseInt(text3.getText());
min=(int)(6*m-90);
hour=(int)((30*((m/60)+h))-90);
second=(int)(6*s-90);
if(ae.getSource()==button)
{
repaint();
}}
public void ddaline(int x1,int y1,int x2,int y2,Graphics g1)
{
int dx=x2-x1,dy=y2-y1,step;
float xinc,yinc,x,y;
if(Math.abs(dx)>Math.abs(dy))
step=Math.abs(dx);
else
step=Math.abs(dy);
xinc=(float)dx/step;
yinc=(float)dy/step;
x=x1; y=y1;
g1.drawString(".",(int)x,(int)y);
for(int i=1;i<step;i++)
{
x=x+xinc;
y=y+yinc;
g1.drawString(".",(int)x,(int)y);
}
}
public void midcircle(int xc,int yc,int r,Graphics g2)
{
int x2=0;
int y2=r;
int p=5/4-r;
while(x2<y2)
{
if(p<0)
{
x2++;
p=p+2*x2+2+1;
}
else
{
x2++;
y2--;
p=p+2*x2+2+1-2*y2+2;
}
g2.drawString(".",xc+x2,yc+y2);
g2.drawString(".",xc+y2,yc+x2);
g2.drawString(".",xc-y2,yc+x2);
g2.drawString(".",xc-x2,yc+y2);
g2.drawString(".",xc+x2,yc-y2);
g2.drawString(".",xc+y2,yc-x2);
g2.drawString(".",xc-y2,yc-x2);
g2.drawString(".",xc-x2,yc-y2);
}
}
}
RISING OF SUN
/*<applet code=sun2 width=700 height=500></applet>*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class sun2 extends Applet
{
int rad,xc,yc,ang,y1;
public void init()
{
rad=0;
ang=25;
xc=420;
yc=420;
}
public void paint(Graphics g)
{
while(ang<=360)
{
g.setColor(Color.white);
g.fillRect(0,0,700,500);
ang++;
rad=(int)(ang/3);
y1=yc-ang;
g.setColor(Color.yellow);
g.fillOval(xc,y1,rad,rad);
g.setColor(Color.blue);
g.fillRect(0,400,700,400);
for(double i=0;i<10000000;i++);
}
while(ang>=25)
{
g.setColor(Color.white);
g.fillRect(0,0,700,500);
g.setColor(Color.yellow);
ang--;
rad=(int)(ang/3);
y1=yc-ang;
g.fillOval(xc,y1,rad,rad);
g.setColor(Color.blue);
g.fillRect(0,400,700,400);
for(double i=0;i<10000000;i++);
}
}}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class sun2 extends Applet
{
int rad,xc,yc,ang,y1;
public void init()
{
rad=0;
ang=25;
xc=420;
yc=420;
}
public void paint(Graphics g)
{
while(ang<=360)
{
g.setColor(Color.white);
g.fillRect(0,0,700,500);
ang++;
rad=(int)(ang/3);
y1=yc-ang;
g.setColor(Color.yellow);
g.fillOval(xc,y1,rad,rad);
g.setColor(Color.blue);
g.fillRect(0,400,700,400);
for(double i=0;i<10000000;i++);
}
while(ang>=25)
{
g.setColor(Color.white);
g.fillRect(0,0,700,500);
g.setColor(Color.yellow);
ang--;
rad=(int)(ang/3);
y1=yc-ang;
g.fillOval(xc,y1,rad,rad);
g.setColor(Color.blue);
g.fillRect(0,400,700,400);
for(double i=0;i<10000000;i++);
}
}}
Solar System
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.lang.*;
/*<applet code=solar.class width=800 height=700></applet>*/
public class solar extends Applet{
double q1,q2,q3,q4,q5,q6,q7,q8,q9;
int xc,yc;
Dimension d;
public void init(){
q1=q2=q3=q4=q5=q6=q7=q8=q9=0;
}
public void delay(){
for(double i=0;i<4000000;i+=0.1);
}
public void paint(Graphics g){
Graphics g1=g;
d=getSize();
xc=d.width/2;
yc=d.height/2;
g1.drawString("Sun",xc-2,yc-2);
while(true){
g1.setColor(Color.yellow);
g1.fillOval(xc-24,yc-24,48,48);
g1.setColor(Color.black);
g1.drawString("Sun",xc-5,yc+2);
g1.setColor(Color.gray);
drwcircle(xc,yc,40,g1);
drwcircle(xc,yc,60,g1);
drwcircle(xc,yc,100,g1);
drwcircle(xc,yc,120,g1);
drwcircle(xc,yc,160,g1);
drwcircle(xc,yc,200,g1);
drwcircle(xc,yc,220,g1);
drwcircle(xc,yc,250,g1);
drwcircle(xc,yc,275,g1);
g.fillOval((int)(xc+40*Math.cos(Math.PI*q1/180))-8,(int)(yc+40*Math.sin(Math.PI*q1/180))-8,16,16);
g.setColor(Color.green);
g.fillOval((int)(xc+60*Math.cos(Math.PI*q2/180))-10,(int)(yc+60*Math.sin(Math.PI*q2/180))-10,20,20);
g.setColor(Color.blue);
g.fillOval((int)(xc+100*Math.cos(Math.PI*q3/180))-10,(int)(yc+100*Math.sin(Math.PI*q3/180))-10,20,20);
g.setColor(Color.green);
g.fillOval((int)(xc+120*Math.cos(Math.PI*q4/180))-8,(int)(yc+120*Math.sin(Math.PI*q4/180))-8,16,16);
g.setColor(Color.cyan);
g.fillOval((int)(xc+160*Math.cos(Math.PI*q5/180))-16,(int)(yc+160*Math.sin(Math.PI*q5/180))-16,32,32);
g.setColor(Color.pink);
g.fillOval((int)(xc+200*Math.cos(Math.PI*q6/180))-15,(int)(yc+200*Math.sin(Math.PI*q6/180))-15,30,30);
g.setColor(Color.gray);
g.fillOval((int)(xc+220*Math.cos(Math.PI*q7/180))-12,(int)(yc+220*Math.sin(Math.PI*q7/180))-12,24,24);
g.setColor(Color.red);
g.fillOval((int)(xc+250*Math.cos(Math.PI*q8/180))-10,(int)(yc+250*Math.sin(Math.PI*q8/180))-10,20,20);
g.setColor(Color.yellow);
g.fillOval((int)(xc+275*Math.cos(Math.PI*q9/180))-7,(int)(yc+275*Math.sin(Math.PI*q9/180))-7,14,14);
delay();
g.setColor(Color.white);
g.fillOval((int)(xc+40*Math.cos(Math.PI*q1/180))-8,(int)(yc+40*Math.sin(Math.PI*q1/180))-8,16,16);
g.fillOval((int)(xc+60*Math.cos(Math.PI*q2/180))-10,(int)(yc+60*Math.sin(Math.PI*q2/180))-10,20,20);
g.fillOval((int)(xc+100*Math.cos(Math.PI*q3/180))-10,(int)(yc+100*Math.sin(Math.PI*q3/180))-10,20,20);
g.fillOval((int)(xc+120*Math.cos(Math.PI*q4/180))-8,(int)(yc+120*Math.sin(Math.PI*q4/180))-8,16,16);
g.fillOval((int)(xc+160*Math.cos(Math.PI*q5/180))-16,(int)(yc+160*Math.sin(Math.PI*q5/180))-16,32,32);
g.fillOval((int)(xc+200*Math.cos(Math.PI*q6/180))-15,(int)(yc+200*Math.sin(Math.PI*q6/180))-15,30,30);
g.fillOval((int)(xc+220*Math.cos(Math.PI*q7/180))-12,(int)(yc+220*Math.sin(Math.PI*q7/180))-12,24,24);
g.fillOval((int)(xc+250*Math.cos(Math.PI*q8/180))-10,(int)(yc+250*Math.sin(Math.PI*q8/180))-10,20,20);
g.fillOval((int)(xc+275*Math.cos(Math.PI*q9/180))-7,(int)(yc+275*Math.sin(Math.PI*q9/180))-7,14,14);
drwcircle(40,40,80,g1);
q1+=1.2;
q2+=1;
q3+=0.8;
q4+=0.7;
q5+=1;
q6+=0.9;
q7+=0.8;
q8+=0.7;
q9+=0.5;
}
}
public void drwcircle(int xc,int yc,int r,Graphics g2)
{
int x2=0;
int y2=r;
int p=5/4-r;
while(x2<y2)
{
if(p<0)
{
x2++;
p=p+2*x2+2+1;
}
else
{
x2++;
y2--;
p=p+2*x2+2+1-2*y2+2;
}
g2.drawString(".",xc+x2,yc+y2);
g2.drawString(".",xc+y2,yc+x2);
g2.drawString(".",xc-y2,yc+x2);
g2.drawString(".",xc-x2,yc+y2);
g2.drawString(".",xc+x2,yc-y2);
g2.drawString(".",xc+y2,yc-x2);
g2.drawString(".",xc-y2,yc-x2);
g2.drawString(".",xc-x2,yc-y2);
}
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.lang.*;
/*<applet code=solar.class width=800 height=700></applet>*/
public class solar extends Applet{
double q1,q2,q3,q4,q5,q6,q7,q8,q9;
int xc,yc;
Dimension d;
public void init(){
q1=q2=q3=q4=q5=q6=q7=q8=q9=0;
}
public void delay(){
for(double i=0;i<4000000;i+=0.1);
}
public void paint(Graphics g){
Graphics g1=g;
d=getSize();
xc=d.width/2;
yc=d.height/2;
g1.drawString("Sun",xc-2,yc-2);
while(true){
g1.setColor(Color.yellow);
g1.fillOval(xc-24,yc-24,48,48);
g1.setColor(Color.black);
g1.drawString("Sun",xc-5,yc+2);
g1.setColor(Color.gray);
drwcircle(xc,yc,40,g1);
drwcircle(xc,yc,60,g1);
drwcircle(xc,yc,100,g1);
drwcircle(xc,yc,120,g1);
drwcircle(xc,yc,160,g1);
drwcircle(xc,yc,200,g1);
drwcircle(xc,yc,220,g1);
drwcircle(xc,yc,250,g1);
drwcircle(xc,yc,275,g1);
g.fillOval((int)(xc+40*Math.cos(Math.PI*q1/180))-8,(int)(yc+40*Math.sin(Math.PI*q1/180))-8,16,16);
g.setColor(Color.green);
g.fillOval((int)(xc+60*Math.cos(Math.PI*q2/180))-10,(int)(yc+60*Math.sin(Math.PI*q2/180))-10,20,20);
g.setColor(Color.blue);
g.fillOval((int)(xc+100*Math.cos(Math.PI*q3/180))-10,(int)(yc+100*Math.sin(Math.PI*q3/180))-10,20,20);
g.setColor(Color.green);
g.fillOval((int)(xc+120*Math.cos(Math.PI*q4/180))-8,(int)(yc+120*Math.sin(Math.PI*q4/180))-8,16,16);
g.setColor(Color.cyan);
g.fillOval((int)(xc+160*Math.cos(Math.PI*q5/180))-16,(int)(yc+160*Math.sin(Math.PI*q5/180))-16,32,32);
g.setColor(Color.pink);
g.fillOval((int)(xc+200*Math.cos(Math.PI*q6/180))-15,(int)(yc+200*Math.sin(Math.PI*q6/180))-15,30,30);
g.setColor(Color.gray);
g.fillOval((int)(xc+220*Math.cos(Math.PI*q7/180))-12,(int)(yc+220*Math.sin(Math.PI*q7/180))-12,24,24);
g.setColor(Color.red);
g.fillOval((int)(xc+250*Math.cos(Math.PI*q8/180))-10,(int)(yc+250*Math.sin(Math.PI*q8/180))-10,20,20);
g.setColor(Color.yellow);
g.fillOval((int)(xc+275*Math.cos(Math.PI*q9/180))-7,(int)(yc+275*Math.sin(Math.PI*q9/180))-7,14,14);
delay();
g.setColor(Color.white);
g.fillOval((int)(xc+40*Math.cos(Math.PI*q1/180))-8,(int)(yc+40*Math.sin(Math.PI*q1/180))-8,16,16);
g.fillOval((int)(xc+60*Math.cos(Math.PI*q2/180))-10,(int)(yc+60*Math.sin(Math.PI*q2/180))-10,20,20);
g.fillOval((int)(xc+100*Math.cos(Math.PI*q3/180))-10,(int)(yc+100*Math.sin(Math.PI*q3/180))-10,20,20);
g.fillOval((int)(xc+120*Math.cos(Math.PI*q4/180))-8,(int)(yc+120*Math.sin(Math.PI*q4/180))-8,16,16);
g.fillOval((int)(xc+160*Math.cos(Math.PI*q5/180))-16,(int)(yc+160*Math.sin(Math.PI*q5/180))-16,32,32);
g.fillOval((int)(xc+200*Math.cos(Math.PI*q6/180))-15,(int)(yc+200*Math.sin(Math.PI*q6/180))-15,30,30);
g.fillOval((int)(xc+220*Math.cos(Math.PI*q7/180))-12,(int)(yc+220*Math.sin(Math.PI*q7/180))-12,24,24);
g.fillOval((int)(xc+250*Math.cos(Math.PI*q8/180))-10,(int)(yc+250*Math.sin(Math.PI*q8/180))-10,20,20);
g.fillOval((int)(xc+275*Math.cos(Math.PI*q9/180))-7,(int)(yc+275*Math.sin(Math.PI*q9/180))-7,14,14);
drwcircle(40,40,80,g1);
q1+=1.2;
q2+=1;
q3+=0.8;
q4+=0.7;
q5+=1;
q6+=0.9;
q7+=0.8;
q8+=0.7;
q9+=0.5;
}
}
public void drwcircle(int xc,int yc,int r,Graphics g2)
{
int x2=0;
int y2=r;
int p=5/4-r;
while(x2<y2)
{
if(p<0)
{
x2++;
p=p+2*x2+2+1;
}
else
{
x2++;
y2--;
p=p+2*x2+2+1-2*y2+2;
}
g2.drawString(".",xc+x2,yc+y2);
g2.drawString(".",xc+y2,yc+x2);
g2.drawString(".",xc-y2,yc+x2);
g2.drawString(".",xc-x2,yc+y2);
g2.drawString(".",xc+x2,yc-y2);
g2.drawString(".",xc+y2,yc-x2);
g2.drawString(".",xc-y2,yc-x2);
g2.drawString(".",xc-x2,yc-y2);
}
}
}
Tuesday, 3 September 2013
line transformation
/*<applet code=translation width=1300 height=800></applet>*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class translation extends Applet implements ActionListener
{
Label l1=new Label("Enter the two end points of the line[x1,x2,y1,y2]: ");
Label l2=new Label("Enter translation factor [tx,ty]: ");
Label l3=new Label("Enter rotation angle and rotation factor[Q,rx,ry]: ");
Label l4=new Label("Enter shearing factor[sx,sy]:");
TextField t1=new TextField(3);
TextField t2=new TextField(3);
TextField t3=new TextField(3);
TextField t4=new TextField(3);
TextField t5=new TextField(3);
TextField t6=new TextField(3);
TextField t7=new TextField(3);
TextField t8=new TextField(3);
TextField t9=new TextField(3);
TextField t10=new TextField(3);
TextField t11=new TextField(3);
Button b1=new Button("Show");
Button b2=new Button("Show");
Button b3=new Button("Show");
Button b4=new Button("Show");
int x1,x2,y1,y2,tx,ty,Q,rx,ry,sx,sy,nx1,nx2,ny1,ny2,t=0;
public void init()
{
add(l1);
add(t1);
add(t2);
add(t3);
add(t4);
add(b1);
add(l2);
add(t5);
add(t6);
add(b2);
add(l3);
add(t7);
add(t8);
add(t9);
add(b3);
add(l4);
add(t10);
add(t11);
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void paint(Graphics g)
{
x1=Integer.parseInt(t1.getText());
x2=Integer.parseInt(t2.getText());
y1=Integer.parseInt(t3.getText());
y2=Integer.parseInt(t4.getText());
g.drawLine(x1,y1,x2,y2);
if(t==1)
g.drawLine(nx1,ny1,nx2,ny2);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
showStatus("Original");
repaint();
}
if(ae.getSource()==b2)
{
tx=Integer.parseInt(t5.getText());
ty=Integer.parseInt(t6.getText());
showStatus("After translation");
t=1;
nx1=x1+tx;
ny1=y1+ty;
nx2=x2+tx;
ny2=y2+ty;
repaint();
}
if(ae.getSource()==b3)
{
Q=Integer.parseInt(t7.getText());
rx=Integer.parseInt(t8.getText());
ry=Integer.parseInt(t9.getText());
showStatus("After rotation");
t=1;
double d=Math.toRadians(Q);
nx1=rx+(int)((x1-rx)*Math.cos(d))-(int)((y1-ry)*Math.sin(d));
nx2=rx+(int)((x2-rx)*Math.cos(d))-(int)((y2-ry)*Math.sin(d));
ny1=ry+(int)((x1-rx)*Math.sin(d))+(int)((y1-ry)*Math.cos(d));
ny2=ry+(int)((x2-rx)*Math.sin(d))+(int)((y2-ry)*Math.cos(d));
repaint();
}
if(ae.getSource()==b4)
{
sx=Integer.parseInt(t10.getText());
sy=Integer.parseInt(t11.getText());
showStatus("scaling");
t=1;
nx1=x1*sx;
nx2=x2*sx;
ny1=y1*sy;
ny2=y2*sy;
repaint();
}
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class translation extends Applet implements ActionListener
{
Label l1=new Label("Enter the two end points of the line[x1,x2,y1,y2]: ");
Label l2=new Label("Enter translation factor [tx,ty]: ");
Label l3=new Label("Enter rotation angle and rotation factor[Q,rx,ry]: ");
Label l4=new Label("Enter shearing factor[sx,sy]:");
TextField t1=new TextField(3);
TextField t2=new TextField(3);
TextField t3=new TextField(3);
TextField t4=new TextField(3);
TextField t5=new TextField(3);
TextField t6=new TextField(3);
TextField t7=new TextField(3);
TextField t8=new TextField(3);
TextField t9=new TextField(3);
TextField t10=new TextField(3);
TextField t11=new TextField(3);
Button b1=new Button("Show");
Button b2=new Button("Show");
Button b3=new Button("Show");
Button b4=new Button("Show");
int x1,x2,y1,y2,tx,ty,Q,rx,ry,sx,sy,nx1,nx2,ny1,ny2,t=0;
public void init()
{
add(l1);
add(t1);
add(t2);
add(t3);
add(t4);
add(b1);
add(l2);
add(t5);
add(t6);
add(b2);
add(l3);
add(t7);
add(t8);
add(t9);
add(b3);
add(l4);
add(t10);
add(t11);
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void paint(Graphics g)
{
x1=Integer.parseInt(t1.getText());
x2=Integer.parseInt(t2.getText());
y1=Integer.parseInt(t3.getText());
y2=Integer.parseInt(t4.getText());
g.drawLine(x1,y1,x2,y2);
if(t==1)
g.drawLine(nx1,ny1,nx2,ny2);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
showStatus("Original");
repaint();
}
if(ae.getSource()==b2)
{
tx=Integer.parseInt(t5.getText());
ty=Integer.parseInt(t6.getText());
showStatus("After translation");
t=1;
nx1=x1+tx;
ny1=y1+ty;
nx2=x2+tx;
ny2=y2+ty;
repaint();
}
if(ae.getSource()==b3)
{
Q=Integer.parseInt(t7.getText());
rx=Integer.parseInt(t8.getText());
ry=Integer.parseInt(t9.getText());
showStatus("After rotation");
t=1;
double d=Math.toRadians(Q);
nx1=rx+(int)((x1-rx)*Math.cos(d))-(int)((y1-ry)*Math.sin(d));
nx2=rx+(int)((x2-rx)*Math.cos(d))-(int)((y2-ry)*Math.sin(d));
ny1=ry+(int)((x1-rx)*Math.sin(d))+(int)((y1-ry)*Math.cos(d));
ny2=ry+(int)((x2-rx)*Math.sin(d))+(int)((y2-ry)*Math.cos(d));
repaint();
}
if(ae.getSource()==b4)
{
sx=Integer.parseInt(t10.getText());
sy=Integer.parseInt(t11.getText());
showStatus("scaling");
t=1;
nx1=x1*sx;
nx2=x2*sx;
ny1=y1*sy;
ny2=y2*sy;
repaint();
}
}
}
line clipping
/*<applet code=lc height=1000 width=1000>
</applet>
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/* <applet code=lineclip height=600 width=600>
</applet>*/
public class lineclip extends Applet implements MouseListener, MouseMotionListener, ActionListener
{
Button bClip=new Button("Clip");
Graphics key;
float x,y,xa,ya,xb,yb,dx,dy,m,p,xEnd;
public void init()
{
add(bClip);
bClip.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mouseClicked(MouseEvent me){}
public void mouseMoved(MouseEvent me){}
public void mouseDragged(MouseEvent me){}
public void mousePressed(MouseEvent me)
{
xa=me.getX();
ya=me.getY();
}
public void mouseReleased(MouseEvent me)
{
xb=me.getX();
yb=me.getY();
repaint();
}
public void paint(Graphics g)
{
g.drawRect(200,200,200,200);
g.drawLine(Math.round(xa),Math.round(ya),Math.round(xb),Math.round(yb));
}
public void actionPerformed(ActionEvent ae)
{
key=getGraphics();
key.setColor(Color.white);
key.drawLine(Math.round(xa),Math.round(ya),Math.round(xb),Math.round(yb));
key.setColor(Color.black);
dx=Math.abs(xa-xb);
dy=Math.abs(ya-yb);
if (dx==0)
m=2;
else
m=(yb-ya)/(xb-xa);
if (m>=0 && m<=1)
{
p=2*dy-dx;
if (xa>xb)
{ x=xb;y=yb;xEnd=xa; }
else
{ x=xa;y=ya;xEnd=xb; }
//plot_clip(key);
while (x<xEnd)
{
x=x+1;
if (p<0)
p=p+2*dy;
else
{ y=y+1;p=p+2*(dy-dx); }
plot_clip(key);
}
}
if (m>=1)
{
p=2*dx-dy;
if (ya>yb)
{ x=xb;y=yb;xEnd=ya; }
else
{ x=xa;y=ya;xEnd=yb; }
//plot_clip(key);
while (y<xEnd)
{
y=y+1;
if (p<0)
p=p+2*dx;
else
{x=x+1;p=p+2*(dx-dy);}
plot_clip(key);
}
}
if (m<0 && m>=-1)
{
p=2*dy-dx;
if (xa<xb)
{ x=xb;y=yb;xEnd=xa; }
else
{ x=xa;y=ya;xEnd=xb; }
//plot_clip(key);
while (x>xEnd)
{
x=x-1;
if (p<0)
p=p+2*dy;
else
{ y=y+1;p=p+2*(dy-dx); }
plot_clip(key);
}
}
if (m<-1)
{
p=2*dx-dy;
if (ya<yb)
{ x=xb;y=yb;xEnd=ya; }
else
{ x=xa;y=ya;xEnd=yb; }
//plot_clip(key);
while (y>xEnd)
{
y=y-1;
if (p<0)
p=p+2*dx;
else
{ x=x+1;p=p+2*(dx-dy); }
plot_clip(key);
}
}
}
public void plot_clip(Graphics key)
{
if (x<200 || x>400 || y<200 || y>400)
key.setColor(Color.white);
else
key.setColor(Color.black);
key.drawString(".",Math.round(x),Math.round(y));
}
}
</applet>
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/* <applet code=lineclip height=600 width=600>
</applet>*/
public class lineclip extends Applet implements MouseListener, MouseMotionListener, ActionListener
{
Button bClip=new Button("Clip");
Graphics key;
float x,y,xa,ya,xb,yb,dx,dy,m,p,xEnd;
public void init()
{
add(bClip);
bClip.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mouseClicked(MouseEvent me){}
public void mouseMoved(MouseEvent me){}
public void mouseDragged(MouseEvent me){}
public void mousePressed(MouseEvent me)
{
xa=me.getX();
ya=me.getY();
}
public void mouseReleased(MouseEvent me)
{
xb=me.getX();
yb=me.getY();
repaint();
}
public void paint(Graphics g)
{
g.drawRect(200,200,200,200);
g.drawLine(Math.round(xa),Math.round(ya),Math.round(xb),Math.round(yb));
}
public void actionPerformed(ActionEvent ae)
{
key=getGraphics();
key.setColor(Color.white);
key.drawLine(Math.round(xa),Math.round(ya),Math.round(xb),Math.round(yb));
key.setColor(Color.black);
dx=Math.abs(xa-xb);
dy=Math.abs(ya-yb);
if (dx==0)
m=2;
else
m=(yb-ya)/(xb-xa);
if (m>=0 && m<=1)
{
p=2*dy-dx;
if (xa>xb)
{ x=xb;y=yb;xEnd=xa; }
else
{ x=xa;y=ya;xEnd=xb; }
//plot_clip(key);
while (x<xEnd)
{
x=x+1;
if (p<0)
p=p+2*dy;
else
{ y=y+1;p=p+2*(dy-dx); }
plot_clip(key);
}
}
if (m>=1)
{
p=2*dx-dy;
if (ya>yb)
{ x=xb;y=yb;xEnd=ya; }
else
{ x=xa;y=ya;xEnd=yb; }
//plot_clip(key);
while (y<xEnd)
{
y=y+1;
if (p<0)
p=p+2*dx;
else
{x=x+1;p=p+2*(dx-dy);}
plot_clip(key);
}
}
if (m<0 && m>=-1)
{
p=2*dy-dx;
if (xa<xb)
{ x=xb;y=yb;xEnd=xa; }
else
{ x=xa;y=ya;xEnd=xb; }
//plot_clip(key);
while (x>xEnd)
{
x=x-1;
if (p<0)
p=p+2*dy;
else
{ y=y+1;p=p+2*(dy-dx); }
plot_clip(key);
}
}
if (m<-1)
{
p=2*dx-dy;
if (ya<yb)
{ x=xb;y=yb;xEnd=ya; }
else
{ x=xa;y=ya;xEnd=yb; }
//plot_clip(key);
while (y>xEnd)
{
y=y-1;
if (p<0)
p=p+2*dx;
else
{ x=x+1;p=p+2*(dx-dy); }
plot_clip(key);
}
}
}
public void plot_clip(Graphics key)
{
if (x<200 || x>400 || y<200 || y>400)
key.setColor(Color.white);
else
key.setColor(Color.black);
key.drawString(".",Math.round(x),Math.round(y));
}
}
Subscribe to:
Posts (Atom)