INTERACTIVE APPLICATION IN JAVA

0 Vani Manikkam · September 20, 2015
How to draw a line using DDA algorithm having attributes solid and dottted?????

Post a Reply

Replies

Oldest  Newest  Rating
0 Milan Obrenovic · September 20, 2015
show the code that you tried
0 Vani Manikkam · October 18, 2015
in fact i need the code foe a dashed line in dda
i hve the codes for dotted one
can you help me please

-----------------------------------------------------------------------------------------------------------------------------------------------------------

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Event;
import java.awt.Point;

public class Draw extends java.applet.Applet { 
  final int MAXLINES = 100;
  Point starts[] = new Point[MAXLINES]; // starting points of past lines
  Point ends[] = new Point[MAXLINES];  // starting points of end lines
  Point anchor;  // start of current line
  int currline = 0; // number of lines
  Point currentpoint; // current end of line
  public void init() {
    setBackground(Color.white);
  }
  public boolean mouseDown(Event evt, int x, int y) {
    if (currline < MAXLINES)
      anchor = new Point(x,y);
    else System.out.println("Too many lines");
    return true;
  }
  public boolean mouseUp(Event evt, int x, int y) {
    if (currline < MAXLINES)
      addline(x,y);
    else System.out.println("Too many lines");
    return true;
  }
  public boolean mouseDrag(Event evt, int x, int y) {
    if (currline < MAXLINES) {
      currentpoint = new Point(x,y);
      repaint();
    }
    return true;
  }
  void addline(int x,int y) {
    starts[currline] = anchor;
    ends[currline] = new Point(x,y);
    currline++;
    currentpoint = null;
    anchor = null;
    repaint();
  }
  boolean flag;
  int count;   
      int sign(int val){
            if (val > 0 ) return 1;
            if (val == 0) return 0;
            return -1;
      }
      void drawDotLine(int x1, int y1, int x2, int y2, int thickness, Graphics g){
            flag = true;
            count = 0;
            double x,y;
            float deltaX, deltaY;
            int Length;
            if (Math.abs(x2 -x1) >= Math.abs(y2 -y1))
                  Length = Math.abs(x2 - x1);
            else
            Length = Math.abs(y2 - y1);           
            deltaX = (float)(((float)x2 - (float)x1) / (float)Length);
            deltaY = (float)(((float)y2  - (float)y1) / (float)Length);
            x=x1;
            y=y1;
            int i = 1;
            
            while (i++ <= Length){
               if (flag){
                        g.fillOval((int)x, (int)y, thickness+1, thickness+1);
                        count++;
                        if (count == thickness + 1){
                              flag = false;
                              count = 0;
                        }
                  }
                  else{
                        count++;
                        if (count == thickness + 1){
                           flag = true;
                           count = 0;
                        }
                  }
                  x += deltaX;
                  y += deltaY;
            }
          g.fillOval((int)x, (int)y, thickness +1, thickness + 1);// Insure the endpoint is also drawn
      }
  public void paint(Graphics g) {
    // Draw existing lines
    for (int i = 0; i < currline; i++) {
      drawDotLine(starts.x, starts.y,
             ends.x, ends.y, 1,  g);
    }
    // draw current line
    g.setColor(Color.blue);
    if (currentpoint != null) 
      drawDotLine(anchor.x,anchor.y,currentpoint.x,currentpoint.y, 1, g);
  }
}
  • 1

Java / Android Development

107,040 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator