Calls the drawOval method of java.awt.Graphics with a square bounding box centered at specified location with width/height of 2r. - Java 2D Graphics

Java examples for 2D Graphics:Oval

Description

Calls the drawOval method of java.awt.Graphics with a square bounding box centered at specified location with width/height of 2r.

Demo Code


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

public class Main {
    /** Calls the drawOval method of java.awt.Graphics
     *  with a square bounding box centered at specified
     *  location with width/height of 2r.
     */*from  w ww  . j ava  2  s.c o m*/
     * @param g The Graphics object.
     * @param x The x-coordinate of the center of the
     *          circle.
     * @param y The y-coordinate of the center of the
     *          circle.
     * @param r The radius of the circle.
     */

    public static void drawCircle(Graphics g, int x, int y, int r) {
        g.drawOval(x - r, y - r, 2 * r, 2 * r);
    }

    /** Draws a circle of radius r at location (x,y) with
     *  the specified line width. Note that the radius r
     *  is to the <B>center</B> of the doughnut drawn.
     *  The outside radius will be r+lineWidth/2 (rounded
     *  down). Inside radius will be r-lineWidth/2
     *  (rounded down).
     *
     * @param g The Graphics object.
     * @param x The x-coordinate of the center of the
     *          circle.
     * @param y The y-coordinate of the center of the
     *          circle.
     * @param r The radius of the circle.
     * @param lineWidth Pen thickness of circle drawn.
     */

    public static void drawCircle(Graphics g, int x, int y, int r,
            int lineWidth) {
        r = r + lineWidth / 2;
        for (int i = 0; i < lineWidth; i++) {
            drawCircle(g, x, y, r);
            if ((i + 1) < lineWidth) {
                drawCircle(g, x + 1, y, r - 1);
                drawCircle(g, x - 1, y, r - 1);
                drawCircle(g, x, y + 1, r - 1);
                drawCircle(g, x, y - 1, r - 1);
                r = r - 1;
            }
        }
    }

    /** Draws a circle of radius r at location (x,y) with
     *  the specified line width and color. Note that
     *  the radius r is to the <B>center</B> of the
     *  doughnut drawn. The outside radius will
     *  be r+lineWidth/2 (rounded down). Inside radius
     *  will be r-lineWidth/2 (rounded down).
     *
     * @param g The Graphics object.
     * @param x The x-coordinate of the center of the
     *          circle.
     * @param y The y-coordinate of the center of the
     *          circle.
     * @param r The radius of the circle.
     * @param lineWidth Pen thickness of circle drawn.
     * @param c The color in which to draw.
     */

    public static void drawCircle(Graphics g, int x, int y, int r,
            int lineWidth, Color c) {
        Color origColor = g.getColor();
        g.setColor(c);
        drawCircle(g, x, y, r, lineWidth);
        g.setColor(origColor);
    }

    /** Calls the drawOval method of java.awt.Graphics
     *  with a square bounding box centered at specified
     *  location with width/height of 2r. Draws in the
     *  color specified.
     *
     * @param g The Graphics object.
     * @param x The x-coordinate of the center of the
     *          circle.
     * @param y The y-coordinate of the center of the
     *          circle.
     * @param r The radius of the circle.
     * @param c The color in which to draw.
     */

    public static void drawCircle(Graphics g, int x, int y, int r, Color c) {
        drawCircle(g, x, y, r, 1, c);
    }

    /** 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
     *  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