com.javaranch.common
Class IRect

java.lang.Object
  |
  +--com.javaranch.common.IRect
All Implemented Interfaces:
java.io.Serializable

public class IRect
extends java.lang.Object
implements java.io.Serializable

A very lean serialized rectangle type made of ints.

- - - - - - - - - - - - - - - - -

Copyright (c) 1998-2004 Paul Wheaton

You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...

My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.

The original source can be found at JavaRanch

- - - - - - - - - - - - - - - - -

Author:
Paul Wheaton
See Also:
Serialized Form

Constructor Summary
IRect()
          Create an IRect object at 0,0 with a width of 0 and a height of 0.
IRect(int x, int y, int width, int height)
          Create an IRect object with a specific X, Y, Width and Height.
IRect(IPoint p)
          Create an IRect object with a specified location, a width of zero and a height of zero.
IRect(IPoint p, int width, int height)
          Create an IRect object with a specific point, Width and Height.
IRect(IPoint p1, IPoint p2)
          Create an IRect object from two opposing points.
IRect(IRect r)
          Create an IRect object with the same size and location as another IRect object.
IRect(java.awt.Rectangle r)
          Create an IRect object with the same size and location as a java.awt.Rectangle object.
 
Method Summary
 void adjust(int x, int y, int width, int height)
          Adjust all possible attributes of this object.
 boolean equals(IRect r)
          Are the two IRect objects at the same location and the same size.
 boolean equals(java.lang.Object obj)
           
 int getBottom()
          Get the Y value of the bottom edge of the rectangle.
 IPoint getBottomLeft()
          Get the point that represents the bottom, left corner of the rectangle.
 IPoint getBottomRight()
          Get the point that represents the bottom, right corner of the rectangle.
 int getHeight()
           
 int getLeft()
          Get the X value of the left edge of the rectangle.
 java.awt.Rectangle getRectangle()
          Convert the IRect format to a java.awt.Rectangle object.
 int getRight()
          Get the X value of the right edge of the rectangle.
 int getTop()
          Get the Y value of the top edge of the rectangle.
 IPoint getTopLeft()
          Get the point that represents the top, left corner of the rectangle.
 IPoint getTopRight()
          Get the point that represents the top, right corner of the rectangle.
 int getWidth()
           
 int getX()
          The left edge.
 int getY()
          The top edge.
 void inc(IPoint p)
          Move the object without changing the width or height.
 void incHeight(int val)
          Change the height without changing the top edge.
 void incWidth(int val)
          Change the width without changing the left edge.
 void incX(int val)
          Change the left edge without changing the width.
 void incY(int val)
          Change the top edge without changing the height.
static boolean PointOnRect(IPoint p, IRect r)
          Does the given point touch the given rectangle.
static boolean RectOnRect(IRect r1, IRect r2)
          Do these two rectangles touch.
 void set(IPoint p)
          Move the top left corner of this object to a specific point.
 void set(IRect r)
          Set this object to be equal to another IRect object.
 void setBottom(int b)
          Change the height of this object so that the bottom edge will line up to a specific position.
 void setHeight(int newHeight)
          Move the bottom edge of this object to accomidate the new height.
 void setRight(int r)
          Change the width of this object so that the right edge will line up to a specific position.
 void setWidth(int newWidth)
          Move the right edge of this object to accomidate the new width.
 void setX(int newX)
          Move this object so the left edge will will line up with a specific position.
 void setY(int newY)
          Move this object so the top edge will will line up with a specific position.
 void shrink(int val)
          Change the rectangle to be a slightly smaller rectangle centered within the old rectangle.
 void swell(int val)
          Change the rectangle to be a slightly larger rectangle.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IRect

public IRect()
Create an IRect object at 0,0 with a width of 0 and a height of 0.


IRect

public IRect(IRect r)
Create an IRect object with the same size and location as another IRect object.


IRect

public IRect(int x,
             int y,
             int width,
             int height)
Create an IRect object with a specific X, Y, Width and Height.

Parameters:
x - The left edge.

y - The top edge.


IRect

public IRect(java.awt.Rectangle r)
Create an IRect object with the same size and location as a java.awt.Rectangle object.


IRect

public IRect(IPoint p,
             int width,
             int height)
Create an IRect object with a specific point, Width and Height.

Parameters:
p - The top left corner.


IRect

public IRect(IPoint p1,
             IPoint p2)
Create an IRect object from two opposing points.


IRect

public IRect(IPoint p)
Create an IRect object with a specified location, a width of zero and a height of zero.

Method Detail

equals

public boolean equals(IRect r)
Are the two IRect objects at the same location and the same size.


equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

getX

public int getX()
The left edge.


getY

public int getY()
The top edge.


getWidth

public int getWidth()

getHeight

public int getHeight()

getLeft

public int getLeft()
Get the X value of the left edge of the rectangle.


getTop

public int getTop()
Get the Y value of the top edge of the rectangle.


getRight

public int getRight()
Get the X value of the right edge of the rectangle.


getBottom

public int getBottom()
Get the Y value of the bottom edge of the rectangle.


getTopLeft

public IPoint getTopLeft()
Get the point that represents the top, left corner of the rectangle.


getTopRight

public IPoint getTopRight()
Get the point that represents the top, right corner of the rectangle.


getBottomLeft

public IPoint getBottomLeft()
Get the point that represents the bottom, left corner of the rectangle.


getBottomRight

public IPoint getBottomRight()
Get the point that represents the bottom, right corner of the rectangle.


getRectangle

public java.awt.Rectangle getRectangle()
Convert the IRect format to a java.awt.Rectangle object.


set

public void set(IRect r)
Set this object to be equal to another IRect object.


set

public void set(IPoint p)
Move the top left corner of this object to a specific point.

Width and height are not changed.


setRight

public void setRight(int r)
Change the width of this object so that the right edge will line up to a specific position.


setBottom

public void setBottom(int b)
Change the height of this object so that the bottom edge will line up to a specific position.


setX

public void setX(int newX)
Move this object so the left edge will will line up with a specific position.

Width is not changed.


setY

public void setY(int newY)
Move this object so the top edge will will line up with a specific position.

Height is not changed.


setWidth

public void setWidth(int newWidth)
Move the right edge of this object to accomidate the new width.


setHeight

public void setHeight(int newHeight)
Move the bottom edge of this object to accomidate the new height.


inc

public void inc(IPoint p)
Move the object without changing the width or height.

Positive values in p.x will move this object to the right. Negative values to the left.

Positive values in p.y will move this object down. Negative values up.


adjust

public void adjust(int x,
                   int y,
                   int width,
                   int height)
Adjust all possible attributes of this object.

Parameters:
x - Change the left edge without changing the width.

y - Change the top edge without changing the height.

width - Change the width without changing the left edge.

height - Change the height without changing the top edge.


incX

public void incX(int val)
Change the left edge without changing the width.


incY

public void incY(int val)
Change the top edge without changing the height.


incWidth

public void incWidth(int val)
Change the width without changing the left edge.


incHeight

public void incHeight(int val)
Change the height without changing the top edge.


shrink

public void shrink(int val)
Change the rectangle to be a slightly smaller rectangle centered within the old rectangle.

Calling shrink(1) will make this rectangle fit exactly within the old rectangle. The left edge will be one pixel further right than it was. The right edge will be one pixel to the left of where it was. The top edge will be one pixel lower and the bottom edge will be one pixel higher.

Parameters:
val - how many pixels to shrink.


swell

public void swell(int val)
Change the rectangle to be a slightly larger rectangle.

Calling swell(1) will make this rectangle wrap exactly around the old rectangle. The left edge will be one pixel further left than it was. The right edge will be one pixel to the right of where it was. The top edge will be one pixel higher and the bottom edge will be one pixel lower.

Parameters:
val - how many pixels to swell.


PointOnRect

public static boolean PointOnRect(IPoint p,
                                  IRect r)
Does the given point touch the given rectangle.


RectOnRect

public static boolean RectOnRect(IRect r1,
                                 IRect r2)
Do these two rectangles touch.



Copyright ©2004 Paul Wheaton All Rights Reserved