/*<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();
}
}
}
Tuesday, 3 September 2013
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)