Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Java project with the following three files, and run it to be sure it works. import javax.swing.*; import java.awt.*; public class Splat {

Create a Java project with the following three files, and run it to be sure it works.

import javax.swing.*; import java.awt.*; public class Splat { public static void main(String[] args) { JFrame frame = new JFrame("Splat"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane() .add(new SplatPanel());

frame.pack(); frame.setVisible(true); } }

import javax.swing.*; import java.awt.*;

public class SplatPanel extends JPanel { private Circle circle1, circle2, circle3, circle4, circle5; public SplatPanel() { circle1 = new Circle(30, Color.red, 70, 35); circle2 = new Circle(50, Color.green, 30, 20); circle3 = new Circle(100, Color.cyan, 60, 85); circle4 = new Circle(45, Color.yellow, 170, 30); circle5 = new Circle(60, Color.blue, 200, 60);

setPreferredSize(new Dimension(300, 200)); setBackground(Color.black); }

public void paintComponent(Graphics page) { super.paintComponent(page);

circle1.draw(page); circle2.draw(page); circle3.draw(page); circle4.draw(page); circle5.draw(page); } }

import java.awt.*;

public class Circle { private int diameter, x, y; private Color color; public Circle(int size, Color shade, int upperX, int upperY) { diameter = size; color = shade; x = upperX; y = upperY; }

public void draw(Graphics page) { page.setColor(color); page.fillOval(x, y, diameter, diameter); }

public void setDiameter(int size) { diameter = size; }

public void setColor (Color shade) { color = shade; }

public void setX(int upperX) { x = upperX; }

public void setY(int upperY) { y = upperY; }

public int getDiameter() {

Then, modify the program so that each Circle object has a label that it stores and displays (when drawn via its draw()) method. You can assume that the label is a very short String that will fit in the Circle. Test that your program works by modifying SplatPAnel to give the circles names. (You can change the circles to be not filled-in and draw the text in white as in the next figure.)

Modify the SplatPanel program so that instead of storing and creating and drawing five arbitrary Circles, it generates n random Circles and stores them on an ArrayList. Prompt the user via dialog box for the number of circles. This entails a lot of things:

1) Use of the dialog box to prompt for and retrieve the number of circles.

2) Replacing the five Circle instance variables with a single ArrayList instance variable.

3) Writing a loop that generates the appropriate number of random circles. Be sure to generate circles that fit on-screen. USe appropriate logic on the random number generation to be sure.

4) Use the index of the loop to "name" each Circle

5) Generate random color like this:

Color color = new Color (generator.nextInt (0xffffff));

(where generator is your Random object).

6) Modify the paintComponent() method so that instead of trying to print the five arbitrary Circles (which you've trashed), it iterates through the list of Circles and prints each one.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

What are examples of a functional level in an organization?P-968

Answered: 1 week ago

Question

2. What, according to Sergey, was strange at this meeting?

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago