Dda Line Drawing

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

DDA LINE DRAWING

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;
import java.math.*;
import java.io.*;
/*<applet code="dda" width=400 height=400>
</applet>*/
public class dda extends Applet implements ActionListener
{
int x,y,m,x1,x2,y1,y2;
float a,b,sx,sy;
Label p,q,r,s;
TextField p1,q1,r1,s1;
public void init()
{
Button ch=new Button("Draw");
add(ch);
ch.addActionListener(this);
p1=new TextField(10);
q1=new TextField(10);
r1=new TextField(10);
s1=new TextField(10);

Label p=new Label("x1",Label.RIGHT);


Label q=new Label("x2",Label.RIGHT);
Label r=new Label("y1",Label.RIGHT);
Label s=new Label("y2",Label.RIGHT);
p1.addActionListener(this);
q1.addActionListener(this);
r1.addActionListener(this);
s1.addActionListener(this);

add(p);
add(p1);
add(q);
add(q1);
add(r);
add(r1);
add(s);
add(s1);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void paint(Graphics g)
{
try
{
x1=Integer.parseInt(p1.getText());
x2=Integer.parseInt(q1.getText());
y1=Integer.parseInt(r1.getText());
y2=Integer.parseInt(s1.getText());
}
catch(NumberFormatException e)
{
}
int dx=x2-x1;
int dy=y2-y1;
if(Math.abs(dx)>Math.abs(dy))
{
m=Math.abs(dx);
}
else
{
m=Math.abs(dy);
}
try{
sx=dx/m;
sy=dy/m;
x=x1;
y=y1;
g.drawString(" ' ",x,y);
for(int k=1;k<=m;k++)
{
a=x+sx;
b=y+sy;
x=Math.round(a);
y=Math.round(b);
g.drawString(" ' ",x,y);
}
}
catch(ArithmeticException e)
{
}
}
}

You might also like