0% found this document useful (0 votes)
12 views2 pages

Canvas For Practical-03

The document contains a Java program that implements a DDA (Digital Differential Analyzer) line drawing algorithm using AWT for graphical rendering. It defines a Canvas class that handles the drawing of lines and axes, updating the graphics as the line is drawn. The program utilizes threading to animate the line drawing process on the canvas.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Canvas For Practical-03

The document contains a Java program that implements a DDA (Digital Differential Analyzer) line drawing algorithm using AWT for graphical rendering. It defines a Canvas class that handles the drawing of lines and axes, updating the graphics as the line is drawn. The program utilizes threading to animate the line drawing process on the canvas.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Canvas for Practical-01

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class pr2 extends Canvas implements Runnable{

float x,y,x1,y1,x2,y2,dx,dy,length,e;

public void drawDDALine(float xP, float yP, float xQ, float yQ){

x1 = xP; y1 = yP;
x2 = xQ; y2 = yQ;

new Thread(this).start();
}

public void run(){

dx = Math.abs(x2 - x1);
dy = Math.abs(y2 - y1);

x = x1;
y = y1;
e = 2 * dy - dx;
int i = 1;

do {
repaint();

while (e >= 0) {
y += (y2 > y1) ? 1 : -1;
e -= 2 * dx;
}

x += (x2 > x1) ? 1 : -1;


e += 2 * dy;
i++;

try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e.toString());
}
} while (i <= dx);
}

public void drawXY(Graphics g){

Dimension d = getSize();
int maxX = d.width - 1;
int maxY = d.height - 1;
Graphics2D g2d = (Graphics2D) g.create();

//
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS
_ON);
// BasicStroke bsThick = new BasicStroke(3.0f);
// g2d.setStroke(bsThick);
g2d.drawRect(0, 0, maxX, maxY);//Canvas Border-Size Rectangle

//Graph Value on Y-Axe


for(int i=0; i<=maxY; i=i+50)
g.drawString(Integer.toString(i), 0, i);

//Graph Value on X-Axe


for(int i=0; i<=maxX; i=i+50)
g.drawString(Integer.toString(i), i, 10);
}

public void update(Graphics g){paint(g);}//update is called when the window is


re-sized.

public void paint(Graphics g)


{
drawXY(g);
Graphics2D g2d = (Graphics2D) g.create();

//Use of antialiasing to have nicer lines

//g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALI
AS_ON);

//Draw the shapes a little bit thicker or thinner.


//BasicStroke bsThick = new BasicStroke(2.0f);
g2d.fillOval((int)x, (int)y, 2, 2);
//Point2D.Float point = new Point2D.Float();

//point.setLocation(x, y);
//g2d.setStroke(bsThick);
//g2d.draw(new Line2D.Float(point, point));
}
}

You might also like