Java Draw Line awt drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)

Here you can find the source of drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)

Description

draw Line

License

Open Source License

Declaration

public static void drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth) 

Method Source Code

//package com.java2s;

import java.awt.*;

public class Main {
    public static void drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth) {
        if (lineWidth == 1)
            g.drawLine(x1, y1, x2, y2);// w  ww. ja v a2s . co m
        else {
            double angle;
            double halfWidth = ((double) lineWidth) / 2.0;
            double deltaX = (double) (x2 - x1);
            double deltaY = (double) (y2 - y1);
            if (x1 == x2)
                angle = Math.PI;
            else
                angle = Math.atan(deltaY / deltaX) + Math.PI / 2;
            int xOffset = (int) (halfWidth * Math.cos(angle));
            int yOffset = (int) (halfWidth * Math.sin(angle));
            int[] xCorners = { x1 - xOffset, x2 - xOffset + 1, x2 + xOffset + 1, x1 + xOffset };
            int[] yCorners = { y1 - yOffset, y2 - yOffset, y2 + yOffset + 1, y1 + yOffset + 1 };
            g.fillPolygon(xCorners, yCorners, 4);
        }
    }
}

Related

  1. drawLine(Graphics g, int x1, int y1, int x2, int y2)
  2. drawLine(Graphics g, int x1, int y1, int x2, int y2, boolean thick)
  3. drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth)
  4. drawLine(Graphics render, int row, String text)