Rectangle rect1 = new Rectangle(10, 10, 50, 50); // x, y, width, height Rectangle rect2 = new Rectangle(20, 20, 30, 30); if (rect1.intersects(rect2)) { System.out.println("Rectangles intersect!"); } else { System.out.println("Rectangles do not intersect."); }
Rectangle rect1 = new Rectangle(10, 10, 50, 50); // x, y, width, height Rectangle rect2 = new Rectangle(40, 40, 30, 30); Rectangle intersection = rect1.intersection(rect2); System.out.println("Intersection: " + intersection);In this example, two rectangles are created and their coordinates are defined. The intersection() method is called on rect1 to find the intersection between rect1 and rect2, and the result is stored in a new Rectangle object called intersection. The output will be the intersection rectangle's coordinates and size: "Intersection: java.awt.Rectangle[x=40,y=40,width=20,height=20]". These methods are part of the java.awt package library, which includes classes for creating and displaying graphical user interfaces (GUI) such as windows, buttons, and menus.