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();
     }
}
    }

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


}

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();
}
}

Friday 8 March 2013


 PROGRAM- 1

   BOUNCING BALL

import java.awt.*;
import java.awt.image.*;
import java.applet.*;
/*<applet code="ball.class" width=810 height=600></applet>*/
public class ball extends Applet implements Runnable
{
int x,y,d=20,height;
private Image img;
private Graphics g2;
public boolean fall;
int r,h;
double delay=4;
public void init()
{
x=0;
y=0;
r=d/2;
height=500;
fall=true;
}
public void start()
{
Thread thread=new Thread(this);
thread.start();
}
public void run()
{
while(true)
{
if(fall && y<530){
y++;
if(y%3==0)x++;
delay-=.003;
}
else if(!fall && y>h)
{
y--;
if(y%3==0)x++;
delay+=0.003;
}
if(y>=525)
{fall=false;height=height*2/3;}
if(y<=h)
{fall=true;y=h;}
repaint();
try{
Thread.sleep((int)delay);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public void update(Graphics g)
{
if(img==null){
img=createImage(this.getSize().width,this.getSize().height);
g2=img.getGraphics();
}
g2.clearRect(0,0,this.getSize().width,this.getSize().height);
paint(g2);
g.drawImage(img,0,0,this);
}
public void paint(Graphics g)
{
g.drawLine(0,550,810,550);
g.fillOval(x,y,d,d);
h=530-height;
}
}



 A BLOG FOR JAVA-APPLET PROGRAMS

     ABOUT JAVA APPLET : 

A Java applet is an applet delivered to users in the form of Java byte code. Java applets can be part of a web page and executed by the Java Virtual Machine (JVM) in a process separate from the web browser, or run in Sun's AppletViewer, a stand-alone tool for testing applets. Java applets were introduced in the first version of the Java language in 1995, and are written in programming languages that compile to Java byte code, usually in Java, but also in other languages such as Jython, JRuby, or Eiffel 
Java applets run at very fast speeds comparable to, but generally slower than, other compiled languages such as C++, but until approximately 2011 many times faster than JavaScript. In addition they can use 3D hardware acceleration that is available from Java. This makes applets well suited for non-trivial, computation intensive visualizations. As browsers have gained support for hardware accelerated graphics thanks to the canvas technology (or specifically WebGL in the case of 3D graphics), as well as just in time compiled JavaScript, the speed difference has become less noticeable.
Since Java's bytecode is cross-platform or platform independent, Java applets can be executed by browsers for many platforms, including Microsoft Windows, Unix, OS X and Linux. It is also trivial to run a Java applet as an application software with very little extra code so that it can be run directly from the integrated development environment (IDE).