package com.ericgiguere.j2mebook.utils; import com.sun.kjava.Graphics; /** * Utility class for drawing circles using the Palm user interface * classes included with the CLDC reference implementation. No * floating point is required. This code is taken from * Leen Ammeraal's book "Computer Graphics for Java Programmers", * published in 1999 by John Wiley & Sons, ISBN 0471981427. * For more information, see the book, or else consult any good * graphics textbook (or search the web!) for an explanation of * Bresenham's algorithm for drawing circles, of which this is * an adaptation. */ public final class Circle //*********************** { /** * Draws a single point by drawing a line starting and * ending at the same coordinates. * * @param g the Graphics object to draw with. * @param x the x-coordinate of the point. * @param y the y-coordinate of the point. */ private static void putPixel( Graphics g, int x, int y ){ g.drawLine( x, y, x, y, g.PLAIN ); } /** * Draws a circle centered at the given point and with * the given radius. * * @param g the Graphics object to draw with. * @param xC the x-coordinate of the circle's center point. * @param yC the y-coordinate of the circle's center point. * @param r the circle's radius. */ public static void draw( Graphics g, int xC, int yC, int r ){ int x=0, y=r, u=1, v=2*r-1, E=0; while( x < y ){ putPixel( g, xC+x, yC+y ); putPixel( g, xC+y, yC-x ); putPixel( g, xC-x, yC-y ); putPixel( g, xC-y, yC+x ); x++; E += u; u+= 2; if( v < 2 * E ) { y--; E -= v; v -= 2; } if( x > y ) break; putPixel( g, xC+y, yC+x ); putPixel( g, xC+x, yC-y ); putPixel( g, xC-y, yC-x ); putPixel( g, xC-x, yC+y ); } } }