Java examples for 2D Graphics:Rectangle
Draws a rounded rectangle at the specified location with the supplied pen thickness.
//package com.java2s; import java.awt.*; public class Main { /** Draws a rounded rectangle at the specified * location with the supplied pen 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, and the * outside width/heights are lineWidth more. *//from w ww . ja 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 arcWidth Horizontal diameter of arc at * corners. * @param arcHeight Vertical diameter of arc at * corners. * @param lineWidth Pen thickness. */ public static void drawRoundRect(Graphics g, int left, int top, int width, int height, int arcWidth, int arcHeight, int lineWidth) { left = left - lineWidth / 2; top = top - lineWidth / 2; width = width + lineWidth; height = height + lineWidth; for (int i = 0; i < lineWidth; i++) { g.drawRoundRect(left, top, width, height, arcWidth, arcHeight); if ((i + 1) < lineWidth) { g.drawRoundRect(left, top, width - 1, height - 1, arcWidth, arcHeight); g.drawRoundRect(left + 1, top, width - 1, height - 1, arcWidth, arcHeight); g.drawRoundRect(left, top + 1, width - 1, height - 1, arcWidth, arcHeight); g.drawRoundRect(left + 1, top + 1, width - 1, height - 1, arcWidth, arcHeight); left = left + 1; top = top + 1; width = width - 2; height = height - 2; } } } /** Draws a rounded rectangle at the specified * location with the supplied pen 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, and 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 arcWidth Horizontal diameter of arc at * corners. * @param arcHeight Vertical diameter of arc at * corners. * @param lineWidth Pen thickness. * @param c Pen color. */ public static void drawRoundRect(Graphics g, int left, int top, int width, int height, int arcWidth, int arcHeight, int lineWidth, Color c) { Color origColor = g.getColor(); g.setColor(c); drawRoundRect(g, left, top, width, height, arcWidth, arcHeight, lineWidth); g.setColor(origColor); } /** Draws a 1-pixel wide rounded rectangle with the * specified color. Same as g.drawRoundRect except * for the color. * * @param g The Graphics object. * @param left The x-coordinate of left 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 to bottom side. * @param arcWidth Horizontal diameter of arc at * corners. * @param arcHeight Vertical diameter of arc at * corners. * @param c Pen color. */ public static void drawRoundRect(Graphics g, int left, int top, int width, int height, int arcWidth, int arcHeight, Color c) { drawRoundRect(g, left, top, width, height, arcWidth, arcHeight, 1, c); } }