Draws a line from (x1, y1) to (x2, y2) using the specified pen thickness. - Java 2D Graphics

Java examples for 2D Graphics:Line

Description

Draws a line from (x1, y1) to (x2, y2) using the specified pen thickness.

Demo Code


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

public class Main {
    /** Draws a line from (x1, y1) to (x2, y2) using the
     *  specified pen thickness./* w  ww.  j  a  va2 s.  c o m*/
     *
     * @param g The Graphics object.
     * @param x1 x position of start of line.
     * @param y1 y position of start of line.
     * @param x2 x position of end of line.
     * @param y2 y position of end of line.
     * @param lineWidth Thickness of line drawn.
     */

    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);
        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);
        }
    }

    /** Draws a line from (x1, y1) to (x2, y2) using the
     *  specified pen thickness and color.
     *
     * @param g The Graphics object.
     * @param x1 x position of start of line.
     * @param y1 y position of start of line.
     * @param x2 x position of end of line.
     * @param y2 y position of end of line.
     * @param lineWidth Thickness of line drawn.
     * @param c The color in which to draw.
     */

    public static void drawLine(Graphics g, int x1, int y1, int x2, int y2,
            int lineWidth, Color c) {
        Color origColor = g.getColor();
        g.setColor(c);
        drawLine(g, x1, y1, x2, y2, lineWidth);
        g.setColor(origColor);
    }

    /** Draws a 1-pixel wide line from (x1, y1) to
     *  (x2, y2) using the specified color.
     *
     * @param g The Graphics object.
     * @param x1 x position of start of line.
     * @param y1 y position of start of line.
     * @param x2 x position of end of line.
     * @param y2 y position of end of line.
     * @param c The color in which to draw.
     */

    public static void drawLine(Graphics g, int x1, int y1, int x2, int y2,
            Color c) {
        drawLine(g, x1, y1, x2, y2, 1, c);
    }

    /** Draws a solid polygon in the specified color. */

    public static void fillPolygon(Graphics g, int[] xs, int[] ys,
            int numPoints, Color c) {
        Color origColor = g.getColor();
        g.setColor(c);
        g.fillPolygon(xs, ys, numPoints);
        g.setColor(origColor);
    }

    /** Draws a solid polygon in the specified color. */

    public static void fillPolygon(Graphics g, Polygon p, Color c) {
        Color origColor = g.getColor();
        g.setColor(c);
        g.fillPolygon(p);
        g.setColor(origColor);
    }
}

Related Tutorials