Draws an oval in the specified bounding rectangle with the specified pen thickness. - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Draws an oval in the specified bounding rectangle with the specified pen thickness.

Demo Code


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

public class Main {
    /** Draws an oval in the specified bounding rectangle
     *  with the specified pen thickness. Note that the
     *  rectangle bounds the <B>center</B> (not the
     *  outside) of the oval. So the oval will really go
     *  lineWidth/2 pixels inside and outside the
     *  bounding rectangle. Specifying a width of 1 has
     *  the identical effect to/*from   w w w.jav a  2s . c  o  m*/
     *  g.drawOval(left, top, width, height).
     *
     * @param g The Graphics object.
     * @param left The left side of the bounding rectangle.
     * @param top The y-coordinate of the top of the
     *            bounding rectangle.
     * @param width The width of the bounding rectangle.
     * @param height The height of the bounding rectangle.
     * @param lineWidth The pen thickness.
     */

    public static void drawOval(Graphics g, int left, int top, int width,
            int height, int lineWidth) {
        left = left - lineWidth / 2;
        top = top - lineWidth / 2;
        width = width + lineWidth;
        height = height + lineWidth;
        for (int i = 0; i < lineWidth; i++) {
            g.drawOval(left, top, width, height);
            if ((i + 1) < lineWidth) {
                g.drawOval(left, top, width - 1, height - 1);
                g.drawOval(left + 1, top, width - 1, height - 1);
                g.drawOval(left, top + 1, width - 1, height - 1);
                g.drawOval(left + 1, top + 1, width - 1, height - 1);
                left = left + 1;
                top = top + 1;
                width = width - 2;
                height = height - 2;
            }
        }
    }

    /** Draws an oval in the specified bounding rectangle
     *  with the specified pen thickness and color. Note
     *  that the rectangle bounds the <B>center</B> (not
     *  the outside) of the oval. So the oval will really
     *  go lineWidth/2 pixels inside and outside the
     *  bounding rectangle. Specifying a width of 1 has
     *  the identical effect to
     *  g.drawOval(left, top, width, height).
     *
     * @param g The Graphics object.
     * @param left The left side of the bounding rectangle.
     * @param top The y-coordinate of the top of the
     *            bounding rectangle.
     * @param width The width of the bounding rectangle.
     * @param height The height of the bounding rectangle.
     * @param lineWidth The pen thickness.
     * @param c The color in which to draw.
     */

    public static void drawOval(Graphics g, int left, int top, int width,
            int height, int lineWidth, Color c) {
        Color origColor = g.getColor();
        g.setColor(c);
        drawOval(g, left, top, width, height, lineWidth);
        g.setColor(origColor);
    }

    /** Draws a 1-pixel thick oval in the specified
     *  bounding rectangle with the specified color.
     *
     * @param g The Graphics object.
     * @param left The left side of the bounding rectangle.
     * @param top The y-coordinate of the top of the
     *            bounding rectangle.
     * @param width The width of the bounding rectangle.
     * @param height The height of the bounding rectangle.
     * @param c The color in which to draw.
     */

    public static void drawOval(Graphics g, int left, int top, int width,
            int height, Color c) {
        drawOval(g, left, top, width, height, 1, c);
    }
}

Related Tutorials