Rectangle rect = new Rectangle(10, 20, 30, 40); //creates new rectangle object rect.setBounds(50, 60, 70, 80); //sets new boundaries for the rectangle
Graphics g = getGraphics(); Rectangle rect = new Rectangle(10, 20, 30, 40); //creates new rectangle object g.drawRect(rect.x, rect.y, rect.width, rect.height); //draws the rectangle rect.setBounds(50, 60, 70, 80); //sets new boundaries for the rectangle g.drawRect(rect.x, rect.y, rect.width, rect.height); //draws the rectangle with new boundariesThis example creates a new rectangle object with initial boundaries of (10, 20, 30, 40), draws the rectangle on a graphics object, sets new boundaries of (50, 60, 70, 80) using the setBounds method, and then draws the rectangle again with the new boundaries.