In: Computer Science
Use BlueJ java to finish this PolkaDots class, class circle is included below as well.
public class PolkaDots
{
private ArrayList<Circle> dots;// an arrayList field to hold
an ArrayList.
/**
* Create a new list of Circles, starts with 5 default
circles.
*/
public PolkaDots()
{
dots = new ArrayList<Circle>(); // an arrayList to hold a
bunch of circles
Circle circle1 = new Circle(30, 10, 10, "blue");//create the 1st
circle
dots.add(circle1);// add the 1st circle to the arrayList
Circle circle2 = new Circle(10, 50, 50, "red");
dots.add(circle2);
Circle circle3 = new Circle(50, 50, 150, "green");
dots.add(circle3);
Circle circle4 = new Circle(20, 100, 100, "yellow");
dots.add(circle4);
Circle circle5 = new Circle(30, 100, 150, "magenta");
dots.add(circle5);
}
/**
* Create a new list of Circles. Starts with the number of
circles
* given by the integer parameter.
*/
//Write another constructor method that accepts a single
integer
//parameter "dotNumber" and uses a while loop to add "dotNumber"
circles
//to the "dots" ArrayList.
//The added circles should be created with the Circle class's
default constructor
//(i.e. they will all be "blue" circles with a diameter of 30 &
etc.)
/**
* Adds a new Circle to the "dots" arrayList.
*/
public void addDot(Circle newDot)
{
dots.add(newDot);
}
/**
* Make dots visible.
*/
public void makeVisible()
{
Circle thisDot;
int index = 0;
while(index < dots.size())
{
thisDot = dots.get(index);
thisDot.makeVisible();
index++;
}
}
/**
* Make all dots invisible.
*/
public void makeInvisible()
{
Circle thisDot;
int index = 0;
while(index < dots.size())
{
thisDot = dots.get(index);
thisDot.makeInvisible();
index++;
}
}
public class Circle
{
private int diameter;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create a new circle at default position with default color.
*/
public Circle()
{
diameter = 30;
xPosition = 20;
yPosition = 60;
color = "blue";
isVisible = true;
draw();
}
/**
* Create a new circle with given values.
*/
public Circle(int diameter, int xPosition, int yPosition, String
color)
{
this.diameter = diameter;
this.xPosition = xPosition;
this.yPosition = yPosition;
this.color = color;
isVisible = true;
draw();
}
/**
* Make this circle visible. If it was already visible,
do nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this circle invisible. If it was already
invisible, do nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Change the position of the circle.
*/
public void changePosition(int newX, int newY)
{
xPosition = newX;
yPosition = newY;
draw();
}
/**
* Change the size to the new size (in pixels). Size must be >=
0.
*/
public void changeSize(int newDiameter)
{
diameter = newDiameter;
draw();
}
/**
* Change the color. Valid colors are "red", "yellow", "blue",
"green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/**
* Change the color by using a code number.
* Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(int newColorNumber)
{
if(newColorNumber > 0 && newColorNumber < 7)
{
if(newColorNumber == 1)
{
changeColor("red");
}
else if( newColorNumber == 2)
{
changeColor("yellow");
}
else if( newColorNumber == 3)
{
changeColor("green");
}
else if( newColorNumber == 4)
{
changeColor("blue");
}
else if( newColorNumber == 5)
{
changeColor("magenta");
}
else
{
changeColor("black");
}
}
}
/*
* Draw the circle with current specifications on
screen.
*/
private void draw()
{
if(isVisible) {
Canvas canvas =
Canvas.getCanvas();
canvas.draw(this, color, new Ellipse2D.Double(xPosition,
yPosition,
diameter, diameter));
canvas.wait(10);
}
}
/*
* Erase the circle on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas =
Canvas.getCanvas();
canvas.erase(this);
}
}
/*
* get Diameter
*/
public int getDiameter()
{
return diameter;
}
/*
* get xPosition
*/
public int getXPosition()
{
return xPosition;
}
/*
* get yPosition
*/
public int getYPosition()
{
return yPosition;
}
}
Attached the complete code. If you get any queries, feel free to comment.
Program Screenshot for Indentation Reference:
Program code to copy:
import java.util.*;
public class PolkaDots {
private ArrayList<Circle> dots;// an
arrayList field to hold an ArrayList.
/**
* Create a new list of Circles, starts
with 5 default circles.
*/
public PolkaDots() {
dots = new
ArrayList<Circle>(); // an arrayList to hold a bunch of
circles
Circle circle1 = new
Circle(30, 10, 10, "blue");// create the 1st circle
dots.add(circle1);// add
the 1st circle to the arrayList
Circle circle2 = new
Circle(10, 50, 50, "red");
dots.add(circle2);
Circle circle3 = new
Circle(50, 50, 150, "green");
dots.add(circle3);
Circle circle4 = new
Circle(20, 100, 100, "yellow");
dots.add(circle4);
Circle circle5 = new
Circle(30, 100, 150, "magenta");
dots.add(circle5);
}
/**
* Create a new list of Circles. Starts
with the number of circles given by the
* integer parameter.
*/
// Write another constructor method that accepts
a single integer
// parameter "dotNumber" and uses a while loop
to add "dotNumber" circles
// to the "dots" ArrayList.
// The added circles should be created with the
Circle class's default
// constructor
// (i.e. they will all be "blue" circles with a
diameter of 30 & etc.)
public PolkaDots(int dotNumber) {
// create an empty array
list
dots = new
ArrayList<Circle>();
// add circles
for (int i = 0; i <
dotNumber; i++) {
dots.add(new Circle());
}
}
/**
* Adds a new Circle to the "dots"
arrayList.
*/
public void addDot(Circle newDot) {
dots.add(newDot);
}
/**
* Make dots visible.
*/
public void makeVisible() {
Circle thisDot;
int index = 0;
while (index <
dots.size()) {
thisDot = dots.get(index);
thisDot.makeVisible();
index++;
}
}
/**
* Make all dots invisible.
*/
public void makeInvisible() {
Circle thisDot;
int index = 0;
while (index <
dots.size()) {
thisDot = dots.get(index);
thisDot.makeInvisible();
index++;
}
}
public void randomizeSizes() {
// get random
class
Random rand = new
Random();
// loop and set size
for (Circle c : dots)
{
// 1 and 100 inclusive random number
c.changeSize(rand.nextInt(100) + 1);
}
}
public void randomizeColors() {
// get random
class
Random rand = new
Random();
// loop and set
color
for (Circle c : dots)
{
// 1 and 6 inclusive random number
c.changeSize(rand.nextInt(6) + 1);
}
}
public void randomizePositions() {
// get random
class
Random rand = new
Random();
// get number of items
in dot
int len =
dots.size();
// loop using while
loop
int i = 0; // current
index
while (i < len)
{
// set the random positions
// OPTIONAL
// get a random position between 1 and
// 300 - circle diameter to partial
// overflow
int x = rand.nextInt(300 - dots.get(i).getDiameter()) + 1;
int y = rand.nextInt(300 - dots.get(i).getDiameter()) + 1;
dots.get(i).changePosition(x, y);
// increment index
i++;
}
}
}