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

No comments:

Post a Comment