For Java. How can I fix my code so that it can fit these requirements: n is an i
ID: 3593021 • Letter: F
Question
For Java. How can I fix my code so that it can fit these requirements:
n is an integer that will be entered by a user in a JTextField
Add a JLabel prompt so the user will know what to enter in the text field
You must use the BorderLayout for the content pane in this application.
You can use the library method drawOval to draw an individual planet or create a more
advanced shape (i.e., using Polygon class).
Planets should be painted in random colors.
Use ActionEvent (button or text field) or MouseEvent to trigger drawing the planets.
my code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Random1 extends Applet
{
public void paint (Graphics page)
{
int NumOfCircles = 100;
int diameter;
int x, y;
setBackground(Color.black);
Random generator = new Random();
for (int count = 0; count < NumOfCircles; count++)
{
diameter = generator.nextInt(60);
x = generator.nextInt(300);
y = generator.nextInt(500);
page.setColor(new Color(generator.nextInt(256), generator.nextInt(256), generator.nextInt(256)));
page.fillOval(x, y, diameter, diameter);
}
}}
Explanation / Answer
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Random1 extends Applet
{
public void paint (Graphics page)
{
int NumOfCircles = 100;
int diameter;
int x, y;
setBackground(Color.black);
Random generator = new Random();
for (int count = 0; count < NumOfCircles; count++)
{
diameter = generator.nextInt(60);
x = generator.nextInt(300);
y = generator.nextInt(500);
page.setColor(new Color(generator.nextInt(256), generator.nextInt(256), generator.nextInt(256)));
page.fillOval(x, y, diameter, diameter);
}
}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.