Rectangle rect1 = new Rectangle(10, 10, 20, 20); Rectangle rect2 = new Rectangle(20, 20, 30, 30); Rectangle unionRect = rect1.union(rect2); System.out.println("Union of rect1 and rect2: " + unionRect);
Rectangle rect1 = new Rectangle(0, 0, 50, 50); Rectangle rect2 = new Rectangle(100, 100, 30, 30); Rectangle unionRect = rect1.union(rect2); System.out.println("Union of rect1 and rect2: " + unionRect);In this example, two rectangles `rect1` and `rect2` are created with non-overlapping coordinates and dimensions. The `union()` method is called on `rect1` with `rect2` as an argument, and the resulting Rectangle is stored in `unionRect`. The output will be: `Union of rect1 and rect2: java.awt.Rectangle[x=0,y=0,width=130,height=130]`. Overall, the java.awt Rectangle union method is useful for determining the smallest Rectangle that encompasses multiple Rectangle objects.