Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 23-3: Using a java.awt.Rectangle object

// This sketch uses two Rectangle objects. 
Rectangle rect1, rect2;

void setup() {
  size(200,200);
  // The arguments for the constructor (x,y,width,height) are documented in the Java reference: 
  // http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Rectangle.html.
  rect1 = new Rectangle(25,25,50,50); 
  rect2 = new Rectangle(125,75,50,75);
}

void draw() {
  background(255);
  stroke(0);
  
  // The contains() function is used to determine if the mouse is located inside the rectangle.
  if (rect1.contains(mouseX,mouseY)) { 
    fill(200);
  } else {
    fill(100);
  }
  
  // A Rectangle object only knows about the variables associated with a rectangle. 
  // It cannot display itself and so we use Processing's rect() function in combination with the Rectangle's data.
  rect(rect1.x, rect1.y, rect1.width,rect1.height); 
  
  // Repeat for the second Rectangle
  // (Of course, we could use an array or ArrayList here!)
  if (rect2.contains(mouseX,mouseY)) {
    fill(200);
  } else {
    fill(100);
  }
  rect(rect2.x, rect2.y, rect2.width,rect2.height);
}
  • http://myernore.com Myer Nore

    What a great idea! Somehow I never looked to see if the awt.Rectangle class was interesting. Turns out it is! The contains method alone is convenient enough to use it in simple Axis Aligned Bounding Box collision tests. Awesome!

  • Santhosh Mamulla

    it’s giving error cannot find method rect(int,int,int,int);
    how to solve the error

  • shiffman

    try adding:

    import java.awt.Rectangle;

    to top of code.

  • zhuhuihui

    It’s giving error cannot find a class or a type named “Rectangle” , how could I solve this error

  • shiffman

    import java.awt.Rectangle;

  • BSB

    I get the same rectangle error. Am on win 7 x 64. Version 1.5 crashes if I run the sketch. Version 2.1.2 tells me I need to run in 64 bit mode. version 2.2.1 (32) also says I need to run in 64 bit mode, but what pooches everything is that if I run it on 2.2.1 (x64) it tells me I need to run it in x32 bit mode. Can only assume something is pooched and that despite adding the line you suggest to the top of the code on all 4 versions of processing.

  • shiffman

    try posting to forum.processing.org and check http://wiki.processing.org/w/Troubleshooting