Rectangle
Rectangles are drawn between a starting point (x1, y1) and an ending point (x2, y2).
Creating a Rectangle
You can create a Rectangle using the following functions:
Rectangle(x1, y1, x2, y2)
Rectangle(x1, y1, x2, y2, color, fill, thickness, rotation, visibility)
| Parameter | Type | Default | Description |
|---|---|---|---|
x1 |
int or float |
required | The horizontal position of the top-left corner, in pixels. |
y1 |
int or float |
required | The vertical position of the top-left corner, in pixels. |
x2 |
int or float |
required | The horizontal position of the bottom-right corner, in pixels. |
y2 |
int or float |
required | The vertical position of the bottom-right corner, in pixels. |
color |
Color |
Color.BLACK |
The color. |
fill |
bool |
False |
Whether the rectangle is filled in (True) or just an outline (False). |
thickness |
int |
1 |
The outline thickness, in pixels. |
rotation |
int or float |
0 |
How far to turn the rectangle, in degrees, counter-clockwise. |
visibility |
int |
100 |
How visible the rectangle is, from 0 (invisible) to 100 (fully visible). |
For example,
rectangle = Rectangle(50, 30, 100, 150)
Once created, you can add it to a Display using the Display's add() function.
Functions
Once a Rectangle rectangle has been created, the following functions are available:
Position
| Function | Description |
|---|---|
rectangle.getPosition() |
Return the object's position, the top-left corner of its bounding box. |
rectangle.getX() |
Return the object's horizontal position. |
rectangle.getY() |
Return the object's vertical position. |
rectangle.getCenter() |
Return the object's center point. |
rectangle.getCenterX() |
Return the object's horizontal center. |
rectangle.getCenterY() |
Return the object's vertical center. |
rectangle.setPosition(x, y) |
Move the object so the top-left corner of its bounding box sits at the given point. |
rectangle.setX(x) |
Set the object's horizontal position. |
rectangle.setY(y) |
Set the object's vertical position. |
rectangle.setCenter(x, y) |
Move the object so its center sits at the given point. |
rectangle.setCenterX(x) |
Set the object's horizontal center. |
rectangle.setCenterY(y) |
Set the object's vertical center. |
rectangle.move(x, y) |
Move the object to a new position. |
Size
| Function | Description |
|---|---|
rectangle.getSize() |
Return the object's width and height. |
rectangle.getWidth() |
Return the object's width. |
rectangle.getHeight() |
Return the object's height. |
rectangle.setSize(width, height) |
Set the object's width and height. |
rectangle.setWidth(width) |
Set the object's width. |
rectangle.setHeight(height) |
Set the object's height. |
Rotation
| Function | Description |
|---|---|
rectangle.getRotation() |
Return how far the object is turned. |
rectangle.setRotation(rotation) |
Turn the object to a given angle. |
rectangle.rotate(angle) |
Turn the object by an additional angle. |
Visibility
| Function | Description |
|---|---|
rectangle.getVisibility() |
Return how visible the object is. |
rectangle.setVisibility(visibility) |
Set how visible the object is. |
Color
| Function | Description |
|---|---|
rectangle.getColor() |
Return the shape's color. |
rectangle.getFill() |
Report whether the shape is filled in. |
rectangle.getThickness() |
Return the shape's outline thickness. |
rectangle.setColor() |
Set the shape's color. |
rectangle.setFill(fill) |
Set whether the shape is filled in. |
rectangle.setThickness(thickness) |
Set the shape's outline thickness. |
Information
| Function | Description |
|---|---|
rectangle.getEndpoints() |
Return the object's four corners. |
rectangle.getBoundingBox() |
Return the smallest upright box that surrounds the object. |
rectangle.getGroup() |
Return the Group this object belongs to. |
rectangle.getDisplay() |
Return the Display this object is on. |
rectangle.setToolTipText() |
Set the hover text shown over the object. |
Hit Testing
| Function | Description |
|---|---|
rectangle.contains(x, y) |
Report whether a point lies inside the object. |
rectangle.intersects(other) |
Report whether this object overlaps another. |
rectangle.encloses(other) |
Report whether this object completely contains another. |
Events
| Function | Description |
|---|---|
rectangle.onMouseClick(action) |
Set up a function to call when the mouse is clicked on this object. |
rectangle.onMouseDown(action) |
Set up a function to call when the mouse button is pressed on this object. |
rectangle.onMouseUp(action) |
Set up a function to call when the mouse button is released over this object. |
rectangle.onMouseMove(action) |
Set up a function to call when the mouse moves over this object. |
rectangle.onMouseDrag(action) |
Set up a function to call when the mouse is dragged over this object. |
rectangle.onMouseEnter(action) |
Set up a function to call when the mouse moves onto this object. |
rectangle.onMouseExit(action) |
Set up a function to call when the mouse moves off this object. |
rectangle.onKeyType(action) |
Set up a function to call when a key is typed (pressed and released). |
rectangle.onKeyDown(action) |
Set up a function to call when a key is pressed down. |
rectangle.onKeyUp(action) |
Set up a function to call when a key is released. |