Java examples for 2D Graphics:Rectangle
Draws a 3D rectangle in the specified location with the given line thickness.
//package com.java2s; import java.awt.*; public class Main { /** Draws a 3D rectangle in the specified location * with the given line thickness. left/top * are the <B>center</B> of the lines drawn. * Ie width/height are from the center of one side * to the center of the other. So the inside * width/heights are really lineWidth less than * the values of width and height; the * outside width/heights are lineWidth more. *//from ww w .j a v a 2 s . c om * @param g The Graphics object. * @param left Center of left side edge. * @param top Center of the top edge. * @param width Distance from center of L side to * center of R side. * @param height Distance from center of top side to * center of bottom side. * @param isRaised A boolean variable that determines * if the right and bottom sides are * shaded to try to make the rectangle * look like it is higher than * background (true) or lower (false). * Works best with relatively thin * lines and gray colors. * @param lineWidth The pen thickness. */ public static void draw3DRect(Graphics g, int left, int top, int width, int height, boolean isRaised, int lineWidth) { left = left - lineWidth / 2; top = top - lineWidth / 2; width = width + lineWidth; height = height + lineWidth; for (int i = 0; i < lineWidth; i++) { g.draw3DRect(left, top, width, height, isRaised); left = left + 1; top = top + 1; width = width - 2; height = height - 2; } } /** Draws a 3D rectangle in the specified location * with the given line thickness and color. left/top * are the <B>center</B> of the lines drawn. * Ie width/height are from the center of one side * to the center of the other. So the inside * width/heights are really lineWidth less than * the values of width and height; the * outside width/heights are lineWidth more. * * @param g The Graphics object. * @param left Center of left side edge. * @param top Center of the top edge. * @param width Distance from center of L side to * center of R side. * @param height Distance from center of top side to * center of bottom side. * @param isRaised A boolean variable that determines * if the right and bottom sides are * shaded to try to make the rectangle * look like it is higher than * background (true) or lower (false). * Works best with relatively thin * lines and gray colors. * @param lineWidth The pen thickness. * @param c The pen color. */ public static void draw3DRect(Graphics g, int left, int top, int width, int height, boolean isRaised, int lineWidth, Color c) { Color origColor = g.getColor(); g.setColor(c); draw3DRect(g, left, top, width, height, isRaised, lineWidth); g.setColor(origColor); } /** Draws a 1-pixel thick 3D rectangle in the * specified location with the given color. * * @param g The Graphics object. * @param left The x-coordinate of left side edge. * @param top The y-coordinate of the top edge. * @param width Distance from L side to R side. * @param height Distance from top side bottom side. * @param isRaised A boolean variable that determines * if the right and bottom sides are * shaded to try to make the rectangle * look like it is higher than * background (true) or lower (false). * Works best with gray colors. * @param c The pen color. */ public static void draw3DRect(Graphics g, int left, int top, int width, int height, boolean isRaised, Color c) { draw3DRect(g, left, top, width, height, isRaised, 1, c); } }