Java Reference
In-Depth Information
LISTING 6.6
continued
diameter -= (2 * RING_WIDTH);
x += RING_WIDTH;
y += RING_WIDTH;
}
// Draw the red bullseye in the center
page.setColor (Color.red);
page.fillOval (x, y, diameter, diameter);
}
}
cover the inner part of the larger circles, creating the ring effect. At the end, a final
red circle is drawn for the bull's-eye.
Let's look at another example. Listing 6.7 shows the Boxes class, which
instantiates and displays BoxesPanel , shown in Listing 6.8. The purpose of this
program is to draw several randomly sized rectangles in random locations. If the
width of a rectangle is below a certain thickness (5 pixels), the box is filled with
the color yellow. If the height is less than the same minimal thickness, the box is
filled with the color green. Otherwise, the box is drawn, unfilled, in white.
LISTING 6.7
//********************************************************************
// Boxes.java Author: Lewis/Loftus
//
// Demonstrates the use of loops to draw.
//********************************************************************
import javax.swing.JFrame;
public class Boxes
{
//-----------------------------------------------------------------
// Creates the main frame of the program.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Boxes");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
 
Search WWH ::




Custom Search