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

this is a question about java ASSIGNMENT 6 QUESTION 1 Write the class BoxCircleP

ID: 3673751 • Letter: T

Question

this is a question about java

ASSIGNMENT 6 QUESTION 1 Write the class BoxCirclePanel which draws a random box and circle. The box is red and placed at a random location inside the panel. The square box also has a random size which is at most the current width of the panel. Similarly with the green circle. You can find out the current size of the panel with getWidth() and getHeight() which both return an int. Each time the code is run, the box and circle will be at a different location and have different sizes. The locations/sizes change as the window is re-sized. At times, only a partial box/circle will be shown. It is also possible for one of the box/circle to cover the other. BoxCircle.java is provided. Please submit both BoxCirclePanel.java and BoxCircle.java Sample run: Initial window: BoxCircle

Explanation / Answer

import javax.swing.JPanel;

import java.awt.*;

import java.util.Random;

public class ExtraCreditPanel extends JPanel // programming hw 5.23

{

    final private int NUM_CIRCLES = 10;

    final private int MAX_DIAMETER = 600;

    private Random generator;

     

    public ExtraCreditPanel ()

    {

        generator = new Random ();

         

        setBackground (Color.WHITE);

        setPreferredSize (new Dimension(600, 600));

         

    }

     

    public void paintComponent(Graphics page)

    {

        super.paintComponent (page);

         

        int x = 0,y = 0;

        int diameter = MAX_DIAMETER;

        int radius;

         

        for (int count = 0; count < NUM_CIRCLES; count++)

        {  

            radius = generator.nextInt(MAX_DIAMETER) + 1;

             

            diameter -= (2 * radius);

             

            x += radius;

            y += radius;

            page.setColor (Color.RED);

            page.drawOval(x, y, diameter, diameter);

             

        }

    }

}