Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a RandomShapes class that is suppose to draw a 16 circles and squares. Th

ID: 3645915 • Letter: I

Question

I have a RandomShapes class that is suppose to draw a 16 circles and squares. The Circle class and Square class are inherited from the Shapes class and draw the shapes at a random size and in a random position. I cannot get my RandomShapes class to work correctly.




import java.awt.*;
public class RandomShapes {

public void randomArray()
{
Square drawSquare = new Square();
Circle drawCircle = new Circle();

Shape[] shapes = new Shape [17];
for (int i = 0; i<shapes.length; i++)
{
int randomShapes = ((int) Math.random()*2);
if(randomShapes == 1)
drawSquare.randomize();
if(randomShapes == 0)
drawCircle.randomize();
}
}




import java.awt.Graphics;
public class Shape
{
protected int x, y, size;


protected Shape()
{
randomize();
}

protected void randomize()
{
x = (int) (200 * Math.random()) + Abutton.BUTTON_WIDTH * 2;
y = (int) (200 * Math.random()) + Abutton.BUTTON_HEIGHT * 2;
size = (int) (100*Math.random());
}

public void paint(Graphics pane)
{

}
}





public class Circle extends Shape
{

public void paint(Graphics pane)
{
pane.setColor(Color.cyan);
pane.fillOval(x, y, size, size);
pane.setColor(Color.black);
pane.drawOval(x, y, size, size);
}
}




public class Square extends Shape
{

public void paint(Graphics pane)
{
pane.setColor(Color.green);
pane.fillRect(x, y, size, size);
pane.setColor(Color.black);
pane.drawRect(x, y, size, size);
}
}



Explanation / Answer

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Combo extends JFrame { private JComboBox shapeComboBox; private String shapes[] = { "Square", "Oval", "Rectangle", "Circle" }; // set up GUI public Combo() { super( "Combo Test" ); // get content pane and set its layout Container container = getContentPane(); container.setLayout( new FlowLayout() ); // set up JComboBox and register its event handler shapeComboBox = new JComboBox( shapes ); shapeComboBox.setMaximumRowCount( 3 ); shapeComboBox.addItemListener( new ItemListener() { // anonymous inner class // handle JComboBox event public void itemStateChanged( ItemEvent event ) { } } // end anonymous inner class ); // end call to addItemListener container.add( shapeComboBox ); setSize( 350, 100 ); setVisible( true ); } // end Combo constructor public static void main( String args[] ) { Combo application = new Combo(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } } // end class Combo