Java examples for 2D Graphics:Rectangle
draw Round Rect
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.awt.image.DirectColorModel; import java.awt.image.WritableRaster; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Main{ public static void drawRoundRect(Graphics g, Rectangle rec) { int xStart = (int) Math.round(rec.x); int xEnd = (int) Math.round(rec.maxX()); int yStart = (int) Math.round(rec.y); int yEnd = (int) Math.round(rec.maxY()); DrawUtil.drawLine(g, xStart + 1, yStart, xEnd - 1, yStart); DrawUtil.drawLine(g, xStart + 1, yEnd, xEnd - 1, yEnd); DrawUtil.drawLine(g, xStart, yStart + 1, xStart, yEnd - 1); DrawUtil.drawLine(g, xEnd, yStart + 1, xEnd, yEnd - 1); }//from w ww . j av a 2 s. c om public static void drawRoundRect(Graphics g, Rectangle rec, Color color) { final Color oldColor = g.getColor(); g.setColor(color); DrawUtil.drawRoundRect(g, rec); g.setColor(oldColor); } /** * Draws a line, using the current color, between the points (xStart, * yStart) and (xEnd, yEnd) in this graphics context's coordinate system. * * @param g * @param xStart * @param yStart * @param xEnd * @param yEnd */ public static void drawLine(Graphics g, int xStart, int yStart, int xEnd, int yEnd) { g.drawLine(xStart, yStart, xEnd, yEnd); } }