import java.awt.Rectangle; import javax.swing.JFrame; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; import java.awt.Color; /** This class exercises the Rectangle class of the Java API. It stores and displays a collection of exactly 4 rectangles. */ public class RectangleTester extends JComponent { // Stored rectangle objects to be displayed private Rectangle r1, r2, r3, r4; /** A constructor that takes the 4 rectangles to be displayed @param rect1 the first rectangle (to be displayed in red) @param rect2 the first rectangle (to be displayed in orange) @param rect3 the first rectangle (to be displayed in green) @param rect4 the first rectangle (to be displayed in blue) */ public RectangleTester(Rectangle rect1, Rectangle rect2, Rectangle rect3, Rectangle rect4) { r1 = rect1; r2 = rect2; r3 = rect3; r4 = rect4; } /** A test method for the RectangleTester class */ public void test() { JFrame frame = new JFrame(); frame.setSize(500, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(this); frame.setVisible(true); } /** Paints the Rectangles r1 through r4 onto a given Graphics object @param g the Graphics object */ public void paintComponent(Graphics g) { // RecoverGraphics2D Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED); g2.fill(r1); g2.setColor(Color.ORANGE); g2.fill(r2); g2.setColor(Color.GREEN); g2.fill(r3); g2.setColor(Color.BLUE); g2.fill(r4); } /** This main method invokes a test method for the RectangleTester class */ public static void main(String args[]) { Rectangle redRectangle = /* a new Rectangle of width 80 and height 60 whose top-left corner is at (60,20) */ Rectangle orangeRectangle = redRectangle; Rectangle greenRectangle = /* a new Rectangle of width 70 and height 50 whose top-left corner is at (240,40) */ /* translate redRectangle 90 units to the right and 20 units down */ Rectangle blueRectangle = /* a new Rectangle with the same width, height, and position as the CURRENT value of redRectangle. You should not have to do any arithmetic */ /* translate blueRectangle 60 units to the left and 70 units down */ RectangleTester tester1 = new RectangleTester(redRectangle, orangeRectangle, greenRectangle, blueRectangle); tester1.test(); orangeRectangle = /* a new rectangle with the same width, height, and position as the ORIGINAL value of redRectangle */ RectangleTester tester2 = new RectangleTester(redRectangle, orangeRectangle, greenRectangle, blueRectangle); tester2.test(); } }