Java AWT Rectangle Class

Introduction

Rectangle class represents a rectangle.

A Rectangle is defined by three properties:

  • (x, y) coordinates of the upper-left corner
  • Width
  • Height

We can create an object of the Rectangle class by specifying different combinations of its properties.

// Rectangle whose upper-left corner is at (0, 0) with zero width and height 
Rectangle r1 = new Rectangle(); 

// Rectangle from a Point object with zero width and height 
Rectangle r2 = new Rectangle(new Point(10, 10)); 

// Rectangle from a Point object and a Dimension object 
Rectangle r3 = new Rectangle(new Point(10, 10), new Dimension(200, 100)); 

// Rectangle by specifying its upper-left corner's 
// coordinate at (10, 10) and width as 200 and height as 100 
Rectangle r4 = new Rectangle(10, 10, 200, 100); 
import java.awt.Rectangle;

public class Main {
  public static void main(String[] args) {
    Rectangle r4 = new Rectangle(10, 10, 200, 100);

    System.out.println(r4);// ww  w.  j a  v a 2s.  c  o  m
  }
}



PreviousNext

Related