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

Modify the following solution so that instead of generating and drawing a bunch

ID: 3834157 • Letter: M

Question

Modify the following solution so that instead of generating and drawing a bunch of circles, it mixes up a variety of shapes.

//Splat.java
import javax.swing.*;
import java.awt.*;
public class Splat
{
   public static void main(String[] args)
   {
       JFrame frame = new JFrame("Splat");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
       //Prompt user to enter number of circles
       //convert string to integer
       int numCircles=Integer.parseInt(JOptionPane.showInputDialog("Enter number of circles to draw"));
    
       //Create an instance of SplatPanel class with numCircles
       frame.getContentPane() .add(new SplatPanel(numCircles));
       frame.pack();
       frame.setVisible(true);
   }
}

import java.awt.Component;


import javax.swing.*;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class SplatPanel extends JPanel
{
   private ArrayList<Circle>circleList;
   public SplatPanel(int numCircles)
   {
       //create an instance of Random class
       Random generator=new Random();
       //declare a circle object
       Circle circle=null;
       //create a arraylist of Circle class
       circleList=new ArrayList<Circle>(numCircles);
    
       //generate user given number of circles
       for (int i = 0; i < numCircles; i++) {
        
           int size=generator.nextInt(100);
           int upperX=generator.nextInt(100);
           int upperY=generator.nextInt(100);
           Color shade = new Color (generator.nextInt (0xffffff));
           circle=new Circle(size, shade, upperX, upperY);
        
           //add circle to circleList
           circleList.add(circle);        
       }
            
       setPreferredSize(new Dimension(300, 200));
       setBackground(Color.black);
   }

   public void paintComponent(Graphics page)
   {
       super.paintComponent(page);
       //draw circle object on the canvas
       for (Circle circle : circleList) {
           circle.draw(page);
       }
    
    
   }
}

ic void setDiameter(int size)
   {
       diameter = size;
   }
   public void setColor (Color shade)
   {
       color = shade;
   }
   public void setX(int upperX)
   {
       x = upperX;
   }
   public void setY(int upperY)
   {
       y = upperY;
   }
   public int getDiameter()
   {
       return diameter;
   }
}

(1) Take the above solution and make a change that will have no impact on the functionality of your program: create a

class called Shape that provides nothing but an empty method whose signature matches the draw() method in Circles:
public void draw(Graphics page) {}
Modify your Circle classso that it tells JAva it is a subclass of Shape. At this point, you should be able to run your

program and it appears unchanged.

(2) Now you need to plug Shape into the panel class: modify the panel class so that instead of storing a list of Circles,

it stores a list of Shapes. Because Circle is a subclass of Shape, you will have no problem adding Circle objects to a

list of Shape objects. But when you go to draw the shapes, you must be careful to note that you are drawing Shapes, not

Circles.

At this point, you should again be able to run your program and see no change. But here is the beauty: while this version of your program functions identically to the earlier version, it is archtected differently and is more extensible.

(3) Now you can extend. Create another class analogous to Circle that models squares. IT should be a subclass of Shape just like Circle is. (You don't need to include any unneded code, such as all those accessors and mutators.)

(4) Modify the panel class so that instead of generating n random Circles, it randomly chooses to generate a Circle or a Square. You should now be able to see a functional difference. Notice you have not needed to change the drawing loop in the paintComponent() method in the panel class.

(5) Choose at least one more shape to model and generate and draw! In the following picture, you can see circles, squares, rectangles, ovals, and triangles. Each would be added the same way you added the Square class (and incorporated the additional logic into random shape generation). Again, you need not make any changes to the paintComponent() method.

Note you have some discretion about how to generate the sizes of the various shapes. You can also choose to write the labels of the shapes in the same color of the shape, to make them easier to identify.

Explanation / Answer

import javax.swing.*;
import java.awt.*;
public class Splat
{
   public static void main(String[] args)
   {
       JFrame frame = new JFrame("Splat");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
       //Prompt user to enter number of circles
       //convert string to integer
       int numCircles=Integer.parseInt(JOptionPane.showInputDialog("Enter number of circles to draw"));
    
       //Create an instance of SplatPanel class with numCircles
       frame.getContentPane() .add(new SplatPanel(numCircles));
       frame.pack();
       frame.setVisible(true);
   }
}

import java.awt.Component;


import javax.swing.*;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class SplatPanel extends JPanel
{
   private ArrayList<Circle>circleList;
   public SplatPanel(int numCircles)
   {
       //create an instance of Random class
       Random generator=new Random();
       //declare a circle object
       Circle circle=null;
       //create a arraylist of Circle class
       circleList=new ArrayList<Circle>(numCircles);
    
       //generate user given number of circles
       for (int i = 0; i < numCircles; i++) {
        
           int size=generator.nextInt(100);
           int upperX=generator.nextInt(100);
           int upperY=generator.nextInt(100);
           Color shade = new Color (generator.nextInt (0xffffff));
           circle=new Circle(size, shade, upperX, upperY);
        
           //add circle to circleList
           circleList.add(circle);        
       }
            
       setPreferredSize(new Dimension(300, 200));
       setBackground(Color.black);
   }

   public void paintComponent(Graphics page)
   {
       super.paintComponent(page);
       //draw circle object on the canvas
       for (Circle circle : circleList) {
           circle.draw(page);
       }
    
    
   }
}

ic void setDiameter(int size)
   {
       diameter = size;
   }
   public void setColor (Color shade)
   {
       color = shade;
   }
   public void setX(int upperX)
   {
       x = upperX;
   }
   public void setY(int upperY)
   {
       y = upperY;
   }
   public int getDiameter()
   {
       return diameter;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote