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

-There are 2 classes that i will include to help solve this problem. package dra

ID: 671243 • Letter: #

Question

-There are 2 classes that i will include to help solve this problem.

package drawing;

import java.awt.Graphics;
import java.util.Random;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class SquarePattern extends JPanel {
   private final Random rand = new Random();
  
   @Override
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);
      
       // TODO: write code to produce the pattern described in the handout
      
   }

}
-------------------------------------------------------

package drawing;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class SquarePatternApp extends JFrame {

   public static void main(String[] args) {
       new SquarePatternApp().run();
   }
  
   public void run() {
       setBounds(100, 10, 500, 500);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   add(new SquarePattern());
   setVisible(true);
   }

}

SquarePattern.java Override the method paintComponent to produce the following pattern: A black square with 3 squares of random color. When the Window is resized the colors get re-drawn (change) The bottom left quadrant has no colorful square. The distance between the colorful squares is 20. Sample Output:

Explanation / Answer

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.WHITE);
g.setFont(new Font("serif", Font.BOLD, 60));
g.drawString(s, getWidth() / 2 - g.getFontMetrics().stringWidth(s) / 2,getHeight() / 2 + g.getFontMetrics().getHeight() / 2);
}