Java Draw Thick Line drawThickLine(Graphics graphics, int x1, int y1, int x2, int y2, int linewidth)

Here you can find the source of drawThickLine(Graphics graphics, int x1, int y1, int x2, int y2, int linewidth)

Description

draw Thick Line

License

Open Source License

Declaration

public static void drawThickLine(Graphics graphics, int x1, int y1, int x2, int y2, int linewidth) 

Method Source Code


//package com.java2s;
import java.awt.*;

public class Main {
    private static final int DDA_SCALE = 8192;

    public static void drawThickLine(Graphics graphics, int x1, int y1, int x2, int y2, int linewidth) {
        // Draw the starting point filled.
        graphics.fillOval(x1 - linewidth / 2, y1 - linewidth / 2, linewidth, linewidth);

        // Short-circuit zero-length lines.
        if (x1 == x2 && y1 == y2)
            return;

        /* Draw, using a simple DDA. */
        if (Math.abs(x2 - x1) > Math.abs(y2 - y1)) {
            // Loop over X domain.
            int dy, srow;
            int dx, col, row, prevrow;

            if (x2 > x1)
                dx = 1;/*from w  w  w . j a  va2  s .c  om*/
            else
                dx = -1;
            dy = (y2 - y1) * DDA_SCALE / Math.abs(x2 - x1);
            prevrow = row = y1;
            srow = row * DDA_SCALE + DDA_SCALE / 2;
            col = x1;
            for (;;) {
                if (row != prevrow) {
                    graphics.drawOval(col - linewidth / 2, prevrow - linewidth / 2, linewidth, linewidth);
                    prevrow = row;
                }
                graphics.drawOval(col - linewidth / 2, row - linewidth / 2, linewidth, linewidth);
                if (col == x2)
                    break;
                srow += dy;
                row = srow / DDA_SCALE;
                col += dx;
            }
        } else {
            // Loop over Y domain.
            int dx, scol;
            int dy, col, row, prevcol;

            if (y2 > y1)
                dy = 1;
            else
                dy = -1;
            dx = (x2 - x1) * DDA_SCALE / Math.abs(y2 - y1);
            row = y1;
            prevcol = col = x1;
            scol = col * DDA_SCALE + DDA_SCALE / 2;
            for (;;) {
                if (col != prevcol) {
                    graphics.drawOval(prevcol - linewidth / 2, row - linewidth / 2, linewidth, linewidth);
                    prevcol = col;
                }
                graphics.drawOval(col - linewidth / 2, row - linewidth / 2, linewidth, linewidth);
                if (row == y2)
                    break;
                row += dy;
                scol += dx;
                col = scol / DDA_SCALE;
            }
        }
    }
}

Related

  1. drawThickLine(Graphics g, int x1, int y1, int x2, int y2)
  2. drawThickLine(Graphics g, int x1, int y1, int x2, int y2, double t)
  3. drawThickLine(Graphics g, int x1, int y1, int x2, int y2, int thickness)