import java.awt.Rectangle; public class RectangleExample { public static void main(String [] args) { Rectangle rect = new Rectangle(); rect.setRect(10, 10, 20, 30); //set x,y coordinates and width, height of rect System.out.println("X: " + rect.getX() + " Y: " + rect.getY() + " Width: " + rect.getWidth() + " Height: " + rect.getHeight()); } }
import java.awt.Rectangle; public class RectangleExample { public static void main(String [] args) { Rectangle rect = new Rectangle(10, 10, 20, 30); //create new rect object System.out.println("Original Rectangle: " + rect); rect.setRect(20, 30, 40, 50); //change the values for x, y, width, height System.out.println("Modified Rectangle: " + rect); } }Output: Original Rectangle: java.awt.Rectangle[x=10,y=10,width=20,height=30] Modified Rectangle: java.awt.Rectangle[x=20,y=30,width=40,height=50] The setRect() method is part of the java.awt package library.