My java code project is finished. But I want to make sure that I completed all t
ID: 3595600 • Letter: M
Question
My java code project is finished. But I want to make sure that I completed all the requirements. Can you please check if I completed these requirements and fix what I missed?
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.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Stars extends JFrame implements ActionListener {
private Random random = new Random();
private JButton button;
private JPanel panel;
private JTextField tf;
public static void main(String[] args) {
Stars demo = new Stars();
demo.setSize(600,600);
demo.createGUI();
demo.setVisible(true);
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
button = new JButton("go");
window.add(button);
button.addActionListener(this);
random = new Random();
setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setLayout(new FlowLayout() );
panel = new JPanel();
panel.setPreferredSize(new Dimension(500, 500));
panel.setBackground(Color.white);
window.add(panel);
tf = new JTextField("mmmmmmmmmmmmmmmmmmm");
window.add(tf);
button = new JButton("Press me");
window.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
Graphics paper = panel.getGraphics();
paper.setColor(Color.black);
paper.fillRect(0, 0, 500, 500);
Random rand = new Random();
int red = rand.nextInt(256);
int green = rand.nextInt(256);
int blue = rand.nextInt(256);
Color randColor = new Color(red, green, blue);
paper.setColor(Color.white);
for (int count = 0; count < 20; count++)
{
int x, y, radius;
x = random.nextInt(500);
y = random.nextInt(500);
radius = 5;
paper.fillOval(x, y, radius, radius);
}
int number;
int max=10, min = 7;
number = min + random.nextInt(max-min+1);
tf.setText("r 7 to 10 " + number);
// to draw the circles
for (int count = 0; count < 25; count++) //number of circles
{
int x, y, radius;
x = random.nextInt(500);
y = random.nextInt(500);
radius = random.nextInt(50);
red = rand.nextInt(256);
green = rand.nextInt(256);
blue = rand.nextInt(256);
randColor = new Color(red, green, blue);
paper.setColor(randColor);
paper.drawOval(x, y, radius, radius); // drawOval method required
paper.fillOval(x, y, radius, radius);
}
int number2;
int max2 = 15, min2 = 5;
number = min + random.nextInt(max2 - min2 + 1);
}
}
Explanation / Answer
There were some problems in your code. First of all, you were not receiving the number ‘n’ from the textfield. Use of BorderLayout was also missing. There was no JLabel field; and both the buttons go and pressme invokes the same action. So I’ve modified your code. Defined a JLabel which hints the user to enter a value between two range, namely 7 to 20. I have put the label, textfield and the button press me into a panel and added at the end of the screen. Used BorderLayout as content pane layout, defined all the other UI elements and placed them on the frame using BorderLayout attributes. actionPerformed() event has been modified, if the user clicks go random button, a random number of planets are generated (between 7 and 20) in random coordinates . If the user clicks press me button, the input in the textfield is taken, if it is a valid number between 7 – 20 , that number of planets are displayed, if the number is more than 20, 20 is used as n, if less than 7, 7 is used as n. invalid inputs are not accepted. Run the code, and you can find more details from the comments included.
//Stars.java file
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Stars extends JFrame implements ActionListener {
private Random random = new Random();
private JButton button;
private JButton goRandom;
private JPanel panel;
private JTextField tf;
private JLabel label;
private JPanel textPanel; /* adding a panel to put label, input textfield and the press me button*/
public static void main(String[] args) {
Stars demo = new Stars();
demo.setSize(600, 600);
demo.createGUI();
demo.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
goRandom = new JButton("go random"); /*a button to create random number of planets, ranging from 5 - 10*/
goRandom.addActionListener(this);
random = new Random();
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container pane=getContentPane(); /*getting the content pane*/
pane.setLayout(new BorderLayout()); /*setting BorderLayout to the content pane*/
panel = new JPanel();
panel.setBackground(Color.white);
textPanel=new JPanel();
label=new JLabel("Enter a value between 7 and 20");
tf = new JTextField();
tf.setPreferredSize(new Dimension(100,30) );
button = new JButton("Press me");
textPanel.add(label);
textPanel.add(tf);
textPanel.add(button);
pane.add(panel,BorderLayout.CENTER);
pane.add(goRandom,BorderLayout.LINE_START);
pane.add(textPanel,BorderLayout.PAGE_END);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
int n=0; /*to denote the number of planets to be created*/
String command=event.getActionCommand();
if(command.equalsIgnoreCase("go random")){ /*go random button is clicked*/
n=random.nextInt(20-5+1)+5; /*generating a number between 7 and 20 */
}else{ /**press me button is clicked, so getting the input from user*/
try{
n=Integer.parseInt(tf.getText().toString()); /**fetching the input from tf textfield*/
if(n>20){
n=20;
}
if(n<7){
n=7;
}
}catch (Exception e) {
System.err.println("wrong input"); /**tf contains an invalid number*/
}
}
Graphics paper = panel.getGraphics();
paper.setColor(Color.black);
paper.fillRect(0, 0, panel.getWidth(), panel.getHeight());
//Random rand = new Random();
int red = random.nextInt(256);/**generating a random color**/
int green = random.nextInt(256);
int blue = random.nextInt(256);
Color randColor = new Color(red, green, blue);
paper.setColor(Color.white);
for (int count = 0; count < 20; count++) { /**filling random particles**/
int x, y, radius;
x = random.nextInt(500);
y = random.nextInt(500);
radius = 5;
paper.fillOval(x, y, radius, radius);
}
tf.setText(""+n);
// to draw the circles
for (int count = 0; count < n; count++) /** n number of planets are being generated*/
{
int x, y, radius;
x = random.nextInt(panel.getWidth()-100); /**getting a random coordinate within the panel to display a planet*/
y = random.nextInt(panel.getHeight()-100);
radius = random.nextInt(50-20)+20;
red = random.nextInt(256);
green = random.nextInt(256);
blue = random.nextInt(256);
randColor = new Color(red, green, blue);
paper.setColor(randColor);
paper.drawOval(x, y, radius, radius); /**drawing the planet**/
paper.fillOval(x, y, radius, radius); /**filling the planet with random color**/
}
}
}
//Output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.