Shift all the circles a little bit to the right.
The left edge of the leftmost circle is 0 pixels from the left edge. The right edge of the rightmost circle is 30 - 2*radius = 30 - 20 = 10 pixels from the edge. If every circle was shifted over five pixels, the drawing would be fixed. This can be done, by adding yet more arithmetic to the applet:
import java.applet.Applet;
import java.awt.*;
// Assume that the drawing area is 300 by 150.
// Draw ten red circles across the drawing area.
public class tenShiftedCircles extends Applet
{
final int width = 300, height = 150;
public void paint ( Graphics gr )
{
gr.setColor( Color.red );
int radius = 10;
int Y = height/2 - radius; // the top edge of the squares
int count = 0 ;
while ( count < 10 )
{
int X = count*(width/10) + ((width/10)-2*radius)/2;
gr.drawOval( X, Y, 2*radius, 2*radius );
count = count + 1;
}
}
}
Here is what it outputs:
The arithmetic used in drawing these circles is getting somewhat messy. It is not, for example, very clear what the statement
int X = count*(width/10) + ((width/10)-2*radius)/2;
does, even after it has been explained.