Question: Using Java JCreator: Correct the program below to have the proper output. The program should ask the users how many shapes they would like drawn,
Using Java JCreator: Correct the program below to have the proper output. The program should ask the users how many shapes they would like drawn, then the program should draw the amount of shapes input, each as a random shape and random color.
import java.awt.*;
import javax.swing.*;
import java.util.*;
@SuppressWarnings("unused")
public class RandomShapes extends JPanel
{
private int red, green, blue;
static int shape;
public void paintComponent( Graphics g )
{
int width = getWidth();
int height = getHeight();
super.paintComponent(g);
Color myColor = new Color(red, green, blue);
Random myRandom = new Random();
red = myRandom.nextInt(255);
green = myRandom.nextInt(255);
blue = myRandom.nextInt(255);
switch( shape )
{
case 1:
g.setColor(myColor);
g.fillOval(myRandom.nextInt(width), myRandom.nextInt(height), 1 + myRandom.nextInt( width / 2 ),
1 + myRandom.nextInt( height / 2 ));
break;
case 2:
g.setColor(myColor);
g.fillRect(myRandom.nextInt(width), myRandom.nextInt(height), 1 + myRandom.nextInt( width / 2 ),
1 + myRandom.nextInt( height / 2 ));
g.setColor(myColor);
g.fillOval(myRandom.nextInt(width), myRandom.nextInt(height), 1 + myRandom.nextInt( width / 2 ),
1 + myRandom.nextInt( height / 2 ));
break;
}
}
public static void main(String[] args) {
//creating input stream to get user response
Scanner input=new Scanner(System.in);
System.out.print("How many shapes would you like?: ");
shape=input.nextInt();
JFrame jf=new JFrame();
RandomShapes rs=new RandomShapes();
jf.add(rs);
jf.setSize(700,500);
//making it visible
jf.setVisible(true);
//adding the exit function for closing the GUI
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
