Friday 19 April 2013

SPIRAL JAVA APPLET PROGRAM

                

Program :

/*<applet code = spiral height = 800 width = 800>
</applet>*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class spiral extends Applet implements ActionListener {
Dimension d;
Label l = new Label("radius = ");
TextField t = new TextField(3);
Button b = new Button("DRAW");
int xc,yc,rad;
public void init(){
add(l);
add(t);
add(b);
b.addActionListener(this);
}
public void  paint(Graphics g)
{
d=getSize();
xc = d.width/2;
yc=d.width/4;
rad=Integer.parseInt(t.getText());
int check=1;
int increment =1;
while(rad>0 && increment<rad)
{
int x=0;
int y=increment;
float p=5/4-y;
while(x<y)
{
if(p<0)
{
x++;
p=p+2*x+2+1;
}
else
{
x++;
y--;
p=p+2*x+2+1-2*y+2;
}
switch(check)
{
case 1:g.drawString(".",xc+x,yc+y);
g.setColor(Color.red);
break;
case 2:g.drawString(".",xc+y,yc+x);
g.setColor(Color.blue);
break;
case 3:g.drawString(".",xc-x,yc+y);
g.setColor(Color.green);
break;
case 4:g.drawString(".",xc-y,yc+x);
g.setColor(Color.black);
break;
case 5:g.drawString(".",xc-x,yc-y);
g.setColor(Color.yellow);
break;
case 6:g.drawString(".",xc-y,yc-x);
g.setColor(Color.orange);
break;
case 7:g.drawString(".",xc+x,yc-y);
g.setColor(Color.pink);
break;
case 8:g.drawString(".",xc+y,yc-x);
g.setColor(Color.magenta);
break;
}
}
check++;
if(check>8)
check=1;
increment+=1;
}
}
public void actionPerformed(ActionEvent aw)
repaint();
}
}

ELLIPSE - MID POINT

                                    

Program : 



/*<applet code = ellipse height = 400 width = 400>
</applet>*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class ellipse extends Applet implements ActionListener {
float x,y,xc,yc,rx,ry,p1,p2;
Label l1 = new Label("semi major axis = ");
Label l2 = new Label("semi minor axis = ");
Label l3 = new Label("x = ");
Label l4 = new Label("y = ");

TextField t1 = new TextField(3);
TextField t2 = new TextField(3);
TextField t3 = new TextField(3);
TextField t4 = new TextField(3);
Button bw = new Button("DRAW");

public void init(){
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
add(bw);
bw.addActionListener(this);
}
public void  paint(Graphics g)
{
rx=Integer.parseInt(t1.getText());
ry=Integer.parseInt(t2.getText());
xc=Integer.parseInt(t3.getText());
yc=Integer.parseInt(t4.getText());
x=0;
y=ry;
p1=(ry*ry)-(rx*rx*y)+(rx*rx/4);
do{
g.drawString(".",Math.round(xc-x),Math.round(yc+y));
g.drawString(".",Math.round(xc-x),Math.round(yc-y));
g.drawString(".",Math.round(xc+x),Math.round(yc+y));
g.drawString(".",Math.round(xc+x),Math.round(yc-y));
x++;
if(p1<0)
p1=p1+(2*ry*ry*x)+(ry*ry);
else
{
y-=1;
p1=p1+(2*ry*ry*x)-(2*rx*rx*y)+(ry*ry);
}
}
while((2*ry*ry*x)<(2*rx*rx*y));
p2= (ry*ry*(x+1/2)*(x+1/2))+(rx*rx*(y-1)*(y-1))-(ry*ry*rx*rx);
while(y>0)
{
g.drawString(".",Math.round(xc-x),Math.round(yc+y));
g.drawString(".",Math.round(xc-x),Math.round(yc-y));
g.drawString(".",Math.round(xc+x),Math.round(yc+y));
g.drawString(".",Math.round(xc+x),Math.round(yc-y));
y--;
if(p2>0)
p2 = p2-(2*rx*rx*y)+(ry*ry);
else{
x+=1;
p2 = p2=(2*ry*ry*x)+(2*rx*rx*y)+(ry*ry);
}
}
public void actionPerformed(ActionEvent aw)
repaint();
}
}



MID POINT CIRCLE

                                    

Program : 


/*<applet code = circle height=400 width= 400>
</applet>*/

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

public class circle extends Applet implements ActionListener {
Label l1 = new Label("x0 = ");
Label l2 = new Label("y0 = ");
Label l3 = new Label("radius = ");


TextField t1 = new TextField(3);
TextField t2 = new TextField(3);
TextField t3 = new TextField(3);

Button bw = new Button("DRAW");

public void init(){
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(bw);
bw.addActionListener(this);
}
public void  paint(Graphics g)
{
int xc=Integer.parseInt(t1.getText());
int yc=Integer.parseInt(t2.getText());
int rad=Integer.parseInt(t3.getText());

int X=0;
int Y=rad;
int p = 5/4-rad;
while(X<Y)
{
if(p<0)
{
X++;
p=p+2*X+2+1;
}
else
{
X++;
Y--;
p = p+2*X+2+1-2*Y+2;
}
g.drawString(".",xc+X,yc+Y);
g.drawString(".",xc+Y,yc-X);
g.drawString(".",xc-Y,yc+X);
g.drawString(".",xc-X,yc+Y);
g.drawString(".",xc+X,yc-Y);
g.drawString(".",xc+Y,yc+X);
g.drawString(".",xc-Y,yc-X);
g.drawString(".",xc-X,yc-Y);
}
}
public void actionPerformed(ActionEvent aw)
repaint();
}
}

Tuesday 2 April 2013

3) Bresenham's line drawing algorithm

        

PROGRAM:

/*<applet code = bresen height=400 width= 400>
</applet>*/

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

public class bresen extends Applet implements ActionListener {

Label l1 = new Label("x1 = ");
Label l2 = new Label("x2 = ");
Label l3 = new Label("y1 = ");
Label l4 = new Label("y2 = ");

TextField t1 = new TextField(3);
TextField t2 = new TextField(3);
TextField t3 = new TextField(3);
TextField t4 = new TextField(3);
Button bw = new Button("DRAW");

public void init(){
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
add(bw);
bw.addActionListener(this);
}
public void  paint(Graphics g)
{
int x1=Integer.parseInt(t1.getText());
int x2=Integer.parseInt(t2.getText());
int y1=Integer.parseInt(t3.getText());
int y2=Integer.parseInt(t4.getText());

int dx=x2-x1;
int dy=y2-y1;
int count=dx;
int p=2*dy-dx;
int xend,X,Y;
if(x1>x2){
X=x2;
Y=y2;
xend=x1;
}
else
{
X=x1;
Y=y1;
xend=x2;
}
g.drawString(".",X,Y);
while(count>0)
{
X+=1;
if(p<0)
{
p=p+2*dy;
}
else
{
Y+=1;
p=p+2*(dy-dx);
}
g.drawString(".",X,Y);
count--;
}
}
public void actionPerformed(ActionEvent aw)

repaint();
}
}

2) DDA - LINE DRAWING ALGORITHM

       

/*<applet code = DDA height = 400 width = 400>
</applet>*/

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

public class DDA extends Applet implements ActionListener {
int x1,x2,y1,y2,step,dx,dy;
float xinc,yinc,X,Y;

Label l1 = new Label("x1 = ");
Label l2 = new Label("x2 = ");
Label l3 = new Label("y1 = ");
Label l4 = new Label("y2 = ");

TextField t1 = new TextField(3);
TextField t2 = new TextField(3);
TextField t3 = new TextField(3);
TextField t4 = new TextField(3);
Button b = new Button("DRAW");

public void init(){
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
add(b);
b.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());
dx=x2-x1;
dy=y2-y1;
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;
//g.setColor(Color.Red);
g.drawString(".",(int)X,(int)Y);
for(int i=1;i<step;i++)
{
X=X+xinc;
Y=Y+yinc;
//g.setColor(Color.RED);
g.drawString(".",(int)X,(int)Y);
}
}
public void actionPerformed(ActionEvent a)
{
repaint();
}
}