The Art and Science of Java - Chapter 5 - Exercise 5 - Rewrite Target Program
Chapter 5 - Exercise 5 - Rewrite Target Program
/* * RewriteTarget.java * ------------------ * This program rewrites the target program to use * createFilledCircle method and use constants to number * and dimensions of the circles. */
import java.awt.Color;
import acm.program.*; import acm.graphics.*;
public class RewriteTarget extends GraphicsProgram{ public void run(){ double dx = getWidth()/2; double dy = getHeight()/2; double circleSize = OUTER_RADIUS; for(int i = 0; i < N_CIRCLES; i++){ if(i % 2 == 0){ add(createFilledCircle(dx, dy, circleSize , Color.RED)); circleSize -= INNER_RADIUS; } else{ add(createFilledCircle(dx, dy, circleSize , Color.WHITE)); circleSize -= INNER_RADIUS; } } } private GOval createFilledCircle(double x, double y, double r, Color color){ GOval circle = new GOval(x - r, y - r, 2 * r, 2 * r); circle.setColor(color); circle.setFilled(true); return circle; } /*private constants*/ private static final int N_CIRCLES = 5; private static final double OUTER_RADIUS = 75; private static final double INNER_RADIUS = 15; }










