0% found this document useful (0 votes)
131 views

DDA Java

This document describes a Java applet that implements the Digital Differential Analyzer (DDA) algorithm for drawing lines. It allows the user to drag the mouse to create a "rubber band" line and then animates the drawing of that line using the DDA algorithm. Key points: - It uses mouse listeners to track mouse drag events and set the start/end points of the rubber band line. - When the mouse is released, it calls the DDA algorithm on a separate thread to animate the line drawing. - The DDA algorithm calculates the slope and y-intercept of the line, then uses a for loop to increment x and calculate the corresponding y values to draw each pixel of

Uploaded by

Ikhary
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

DDA Java

This document describes a Java applet that implements the Digital Differential Analyzer (DDA) algorithm for drawing lines. It allows the user to drag the mouse to create a "rubber band" line and then animates the drawing of that line using the DDA algorithm. Key points: - It uses mouse listeners to track mouse drag events and set the start/end points of the rubber band line. - When the mouse is released, it calls the DDA algorithm on a separate thread to animate the line drawing. - The DDA algorithm calculates the slope and y-intercept of the line, then uses a for loop to increment x and calculate the corresponding y values to draw each pixel of

Uploaded by

Ikhary
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.awt.*; import java.awt.event.*; import java.applet.

*; /** Animation of DDA (Digital Differential Analyzer) Algorithm for line drawing */ public class DDA extends BigPixelApplet implements Runnable{ public void init() { super.init(); canvas.addMouseListener(new MyAdapter()); canvas.addMouseMotionListener(new RubberLine()); } int startx, starty; //start of line we are rubber-banding int endx, endy; //end of line we are rubber-banding public void run() { drawLine(startx,starty,endx,endy); } /** Implements a rubber band line that follows cursor while button is down */ class RubberLine extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { Graphics g = canvas.getGraphics(); g.setXORMode(Color.cyan); //erase the old line (because of XOR mode) g.drawLine(toScreenX(startx),toScreenY(starty),toScreenX(endx),toScreenY(e ndy)); endx = fromScreenX(e.getX()); endy = fromScreenY(e.getY()); //draw new line g.drawLine(toScreenX(startx),toScreenY(starty),toScreenX(endx),toScreenY(e ndy)); } } class MyAdapter extends MouseAdapter { public void mousePressed(MouseEvent e) { startx = fromScreenX(e.getX()); starty = fromScreenY(e.getY()); endx = startx; endy = starty; } public void mouseReleased(MouseEvent e) { endx = fromScreenX(e.getX()); endy = fromScreenY(e.getY()); Polygon p = new Polygon(); p.addPoint(startx,starty); p.addPoint(endx,endy); canvas.setPolygon(p); begin(); //this calls run() in another thread to do the animation } } /* These two variables contain the state of the DDA algorithm */ int x; float y; public String showState() {

return "x = " + x + "\ny = " + y; } /** The actual DDA algorithm */ void drawLine(int x1, int y1, int x2, int y2) { int dx = x2 - x1; int dy = y2 - y1; float m = dy / (float) dx; float b = y1 - m*x1; for (x = x1; x <= x2; x++) { y = m*x+b; drawPixel(x,Math.round(y)); if(step()) return; } } }

You might also like