Java Draw Dashed Line drawDashedRect(Graphics g, int x, int y, int width, int height)

Here you can find the source of drawDashedRect(Graphics g, int x, int y, int width, int height)

Description

Draws a rectangle, simulating a dotted stroke by painting only every second pixel along the one-pixel thick edge.

License

Open Source License

Parameter

Parameter Description
g the graphics into which the rectangle is drawn.
x the x coordinate of the rectangle.
y the y coordinate of the rectangle.
width the width of the rectangle in pixels.
height the height of the rectangle in pixels.

Declaration

public static void drawDashedRect(Graphics g, int x, int y, int width, int height) 

Method Source Code

//package com.java2s;
/* BasicGraphicsUtils.java
   Copyright (C) 2003 Free Software Foundation, Inc.
    //www .  java 2  s  .com
This file is part of GNU Classpath.
    
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
    
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
    
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
    
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

import java.awt.Graphics;

public class Main {
    /**
     * Draws a rectangle, simulating a dotted stroke by painting only
     * every second pixel along the one-pixel thick edge. The color of
     * those pixels is the current color of the Graphics <code>g</code>.
     * Any other pixels are left unchanged.
     *
     * <p><img src="doc-files/BasicGraphicsUtils-7.png" width="360"
     * height="200" alt="[An illustration that shows which pixels
     * get painted]" />
     *
     * @param g the graphics into which the rectangle is drawn.
     * @param x the x coordinate of the rectangle.
     * @param y the y coordinate of the rectangle.
     * @param width the width of the rectangle in pixels.
     * @param height the height of the rectangle in pixels.
     */
    public static void drawDashedRect(Graphics g, int x, int y, int width, int height) {
        int right = x + width - 1;
        int bottom = y + height - 1;

        /* Draw the top and bottom edge of the dotted rectangle. */
        for (int i = x; i <= right; i += 2) {
            g.drawLine(i, y, i, y);
            g.drawLine(i, bottom, i, bottom);
        }

        /* Draw the left and right edge of the dotted rectangle. */
        for (int i = y; i <= bottom; i += 2) {
            g.drawLine(x, i, x, i);
            g.drawLine(right, i, right, i);
        }
    }
}

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(Graphics2D graphics, Rectangle2D rectangle)