// Private methods created to clean up Paint(Graphics); private void prepareCircle(Graphics canvas, int i) { // Chooses a random location on the canvas to paint circle number i; X[i] = random.nextInt(canvas.getWidth() - circleDiameter); Y[i] = random.nextInt(canvas.getWidth() - circleDiameter); // Chooses a random color for circle number i; Color[i] = rgbToColor(random.nextInt(256), random.nextInt(256), random.nextInt(256), 255); // Randomly selects whether circle number i will be an outline or a filled object; isFill[i] = (random.nextDouble() < 0.5); }
@Override // Paints circles to the canvas, according to the parameters X, Y, Color, isFill, as declared by // the prepareCircle method; public void paint(Graphics canvas) { // Clear the canvas for future painting; canvas.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); // Prepares all of the circles to print to the screen when the program is initialized; if (!isFirstTime) { for (int i = 0; i < nCircles; i++) { prepareCircle(canvas, i); } isFirstTime = true; } // Moves the least recently adjusted circle to a new random location with a new random color; else { prepareCircle(canvas, currentCircle); } // Paints the circles to the screen; Multiple for loops ensure that the most recently painted // circles remain on top; for (int i = currentCircle + 1; i < nCircles; i++) { paintCircle(canvas, i); } for (int i = 0; i <= currentCircle; i++) { paintCircle(canvas, i); } // Pushes currentCircle forward through the Arrays; resets currentCircle at the end of the // cycle; currentCircle++; if (currentCircle == nCircles) { currentCircle = 0; } }