Design, write, and test two programs
Design, write, and test two programs, each of which draws one hundred random circles. One program should use a while statement; the other, a for statement.
The circles should be placed at random locations, with random diameters and colors. Each circle should use two random colors: one for the fill (interior), and one for the stroke (circumference).
Each circle must fit entirely within the drawing window; however, circles may overlap.
6.5)
//********************************************************************
// Bullseye.java Author: Lewis/Loftus
//
// Demonstrates the use of loops to draw.
//********************************************************************
import javax.swing.JFrame;
public class Bullseye
{
//-----------------------------------------------------------------
// Creates the main frame of the program.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Bullseye");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
BullseyePanel panel = new BullseyePanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
6.6)
//********************************************************************
// BullseyePanel.java Author: Lewis/Loftus
//
// Demonstrates the use of conditionals and loops to guide drawing.
//********************************************************************
import javax.swing.JPanel;
import java.awt.*;
public class BullseyePanel extends JPanel
{
private final int MAX_WIDTH = 300, NUM_RINGS = 5, RING_WIDTH = 25;
//-----------------------------------------------------------------
// Sets up the bullseye panel.
//-----------------------------------------------------------------
public BullseyePanel ()
{
setBackground (Color.cyan);
setPreferredSize (new Dimension(300,300));
}
//-----------------------------------------------------------------
// Paints a bullseye target.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
int x = 0, y = 0, diameter = MAX_WIDTH;
page.setColor (Color.white);
for (int count = 0; count < NUM_RINGS; count++)
{
if (page.getColor() == Color.black) // alternate colors
page.setColor (Color.white);
else
page.setColor (Color.black);
page.fillOval (x, y, diameter, diameter);
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);
}
}
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);
BoxesPanel panel = new BoxesPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
6.8)
//********************************************************************
// BoxesPanel.java Author: Lewis/Loftus
//
// Demonstrates the use of conditionals and loops to guide drawing.
//********************************************************************
import javax.swing.JPanel;
import java.awt.*;
import java.util.Random;
public class BoxesPanel extends JPanel
{
private final int NUM_BOXES = 50, THICKNESS = 5, MAX_SIDE = 50;
private final int MAX_X = 350, MAX_Y = 250;
private Random generator;
//-----------------------------------------------------------------
// Sets up the drawing panel.
//-----------------------------------------------------------------
public BoxesPanel ()
{
generator = new Random();
setBackground (Color.black);
setPreferredSize (new Dimension(400, 300));
}
//-----------------------------------------------------------------
// Paints boxes of random width and height in a random location.
// Narrow or short boxes are highlighted with a fill color.
//-----------------------------------------------------------------
public void paintComponent(Graphics page)
{
super.paintComponent (page);
int x, y, width, height;
for (int count = 0; count < NUM_BOXES; count++)
{
x = generator.nextInt(MAX_X) + 1;
y = generator.nextInt(MAX_Y) + 1;
width = generator.nextInt(MAX_SIDE) + 1;
height = generator.nextInt(MAX_SIDE) + 1;
if (width <= THICKNESS) // check for narrow box
{
page.setColor (Color.yellow);
page.fillRect (x, y, width, height);
}
else
if (height <= THICKNESS) // check for short box
{
page.setColor (Color.green);
page.fillRect (x, y, width, height);
}
else
{
page.setColor (Color.white);
page.drawRect (x, y, width, height);
}
}
}
}
12 years ago
Purchase the answer to view it
- randomcirclesforloop.java
- randomcircleswhileloop.java