Java Draw Dashed Line drawDashedRect(Graphics2D graphics, Rectangle2D rectangle)

Here you can find the source of drawDashedRect(Graphics2D graphics, Rectangle2D rectangle)

Description

Draws a dashed rectangle in the graphics context, with an X through the middle.

License

Open Source License

Parameter

Parameter Description
graphics graphics context for drawing.
rectangle the rectangle to be drawn.

Declaration

public static void drawDashedRect(Graphics2D graphics, Rectangle2D rectangle) 

Method Source Code


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

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

import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

public class Main {
    private static final Stroke DASHED_STROKE_RECT = new BasicStroke(0.0f, BasicStroke.CAP_BUTT,
            BasicStroke.JOIN_MITER, 1.0f, new float[] { 1.0f, 1.0f }, 0.0f);

    /**/*from   www .  jav  a  2 s .  c  o  m*/
     * Draws a dashed rectangle in the graphics context, with an X through the middle.
     * Used for diagnostic painting.
     * <br/><br/>
     * @param graphics   graphics context for drawing.
     * @param rectangle  the rectangle to be drawn.
     */
    public static void drawDashedRect(Graphics2D graphics, Rectangle2D rectangle) {
        // save the current stroke
        Stroke oldStroke = graphics.getStroke();

        // set the new drawing stroke to a dashed line
        graphics.setStroke(DASHED_STROKE_RECT);

        // draw the dashed rectangle
        graphics.draw(rectangle);

        // Draw an X through the box.
        graphics.draw(
                new Line2D.Double(rectangle.getX(), rectangle.getY(), rectangle.getMaxX(), rectangle.getMaxY()));
        graphics.draw(
                new Line2D.Double(rectangle.getX(), rectangle.getMaxY(), rectangle.getMaxX(), rectangle.getY()));

        // restore the old stroke
        graphics.setStroke(oldStroke);
    }
}

Related

  1. drawDashedLine(Graphics2D graphics, double x1, double y1, double x2, double y2)
  2. drawDashedRect(Graphics g, int x, int y, int width, int height)
  3. drawDashedRect(Graphics g, int x, int y, int width, int height)