Java Draw Dashed Line drawDashedLine(Graphics2D graphics, double x1, double y1, double x2, double y2)

Here you can find the source of drawDashedLine(Graphics2D graphics, double x1, double y1, double x2, double y2)

Description

draws a dashed line in the specified graphics context.

License

Open Source License

Parameter

Parameter Description
graphics graphics context for drawing.
x1 starting x coordinate of line.
y1 starting y coordinate of line.
x2 ending x coordinate of line.
y2 ending y coordinate of line.

Declaration

public static void drawDashedLine(Graphics2D graphics, double x1, double y1, double x2, double y2) 

Method Source Code


//package com.java2s;
import java.awt.BasicStroke;
import java.awt.Color;

import java.awt.Graphics2D;
import java.awt.Stroke;

import java.awt.geom.Line2D;

public class Main {
    private static final Stroke DASHED_STROKE_LINE = new BasicStroke(0.0f, BasicStroke.CAP_BUTT,
            BasicStroke.JOIN_MITER, 1.0f, new float[] { 2.0f, 3.0f }, 0.0f);
    public static final Color GRAY = new Color(0.5f, 0.5f, 0.5f, 0.4f);

    /**/*from w  w  w  .j  a  va  2 s .c o m*/
     * draws a dashed line in the specified graphics context.
     * <br/><br/>
     * @param graphics  graphics context for drawing.
     * @param x1        starting x coordinate of line.
     * @param y1        starting y coordinate of line.
     * @param x2        ending   x coordinate of line.
     * @param y2        ending   y coordinate of line.
     */
    public static void drawDashedLine(Graphics2D graphics, double x1, double y1, double x2, double y2) {
        // save the current drawing color and stroke
        Stroke oldStroke = graphics.getStroke();
        Color oldColor = graphics.getColor();

        // set the new drawing color and stroke
        graphics.setStroke(DASHED_STROKE_LINE);
        graphics.setColor(GRAY);

        // draw the dashed line
        graphics.draw(new Line2D.Double(x1, y1, x2, y2));

        // restore the old drawing color and stroke
        graphics.setStroke(oldStroke);
        graphics.setColor(oldColor);
    }
}

Related

  1. drawDashedRect(Graphics g, int x, int y, int width, int height)
  2. drawDashedRect(Graphics g, int x, int y, int width, int height)
  3. drawDashedRect(Graphics2D graphics, Rectangle2D rectangle)