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

java programing Class SquaresPanel is a JComponent that maintains a list ofsquar

ID: 3613041 • Letter: J

Question

java programing
Class SquaresPanel is a JComponent that maintains a list ofsquares, can display them, and is able to generate a new square andadd it its list of squares. Here is the plan: in fields wehave SIDE (the length of the sides of each square), n (the numberof squares we have), and x and y (arrays that store the pixelcoordinates of their corners in indexes 0..n-1). Write a method ofSquaresPanel called addSquare that generates a random square andadds it to its list of squares. The generated square should beentirely contained within our area and equally likely to beanywhere. (Use getWidth() and getHeight() to determine theareas of this SquaresPanel.) java programing
Class SquaresPanel is a JComponent that maintains a list ofsquares, can display them, and is able to generate a new square andadd it its list of squares. Here is the plan: in fields wehave SIDE (the length of the sides of each square), n (the numberof squares we have), and x and y (arrays that store the pixelcoordinates of their corners in indexes 0..n-1). Write a method ofSquaresPanel called addSquare that generates a random square andadds it to its list of squares. The generated square should beentirely contained within our area and equally likely to beanywhere. (Use getWidth() and getHeight() to determine theareas of this SquaresPanel.)

Explanation / Answer

import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.util.*;class SquaresPanel extends JPanel {private Color _lineColor; // Color of lineprivate int []x;private int []y; int n,W;public SquaresPanel() {int h,w,i;n=10;Random rand = new Random();w = getWidth(); // Size might have changed ifh = getHeight(); // user resized window.W=4;_lineColor = Color.GREEN; // Initial color.x=new int[n];y=new int[n];setPreferredSize(new Dimension(100,100));addSquare(10,10);}@Override public void paintComponent(Graphics g) {super.paintComponent(g); // Ask parent to paint background.int i;g.setColor(_lineColor);int w = getWidth(); // Size might have changed ifint h = getHeight(); // user resized window.//g.fillOval(0, 0, w, h); for(i=0;i