Praise D. answered 02/11/23
I teach teenagers to code
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RectangleDrawer extends JPanel {
int[] rectangles;
public RectangleDrawer(int[] rectangles) {
this.rectangles = rectangles;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < rectangles.length; i += 4) {
int x = rectangles[i];
int y = rectangles[i + 1];
int width = rectangles[i + 2];
int height = rectangles[i + 3];
g.drawRect(x, y, width, height);
}
}
public static void main(String[] args) {
int[] rectangles = {10, 10, 50, 50, 70, 70, 150, 150};
JFrame frame = new JFrame();
frame.add(new RectangleDrawer(rectangles));
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I hope this answer helps you