Home | Back to the J2ME Companion Site
 
Drawing Circles with the CLDC

The CLDC reference implementation includes a Graphics class for drawing lines and text, but there is no method for drawing circles! Even though the set of user interface classes included with the CLDC are unsupported (profiles replace them with their own set of user interface classes), it would have been nice if a few more graphics primitives had been exposed. Still, you can draw circles yourself using Bresenham's algorithm for circles, which was originally developed for drawing circles on plotters. I use this for the tic-tac-toe sample on the CD-ROM that accompanies my book on J2ME.

The code in the following class (which you can also download instead of typing it all in) is an adaptation of the Bresenham algorithm and is taken verbatim from the book Computer Graphics for Java Programmers by Leen Ammeraal:

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 );
	}
    }
}

Running this kind of code in Java is not the best way to do graphics, though, because on a Palm device (or even in the emulator) the time to draw the circle is noticeable. It would definitely be better to have a native method for drawing circles. If you're drawing a lot of same-size circles you may even be better off by copying a bitmap of a circle onto the screen!

Home | Back to the J2ME Companion Site
 

Copyright ©2000-2001 by Eric Giguère. This page was last modified on September 2