Class GObject¶
This class provides basic functionality for drawing to a GView.
You should never make a GObject
directly. Instead, you should use one of its
many subclasses: GRectangle, GEllipse,
GImage, GLabel
While unsupported in the base class, most subclasses of GObject
provide primitive
collision detection via the contains
method. This method takes a Point2
object from the cornell
module.
Constructor¶
- class GObject(**keywords)¶
An class representing a basic graphics object.
A graphics object is an object to draw on the screen. To draw it, you will need an instance of
GView
, which is passed to thedraw()
method.You should never make a GObject directly. Instead, you should use one of the subclasses:
GRectangle
,GEllipse
,GImage
,GLabel
,GTriangle
,GPolygon
, orGPath
.Creates a new GObject to be drawn.
To use the constructor for this class, you should provide it with a list of keyword arguments that initialize various attributes. For example, to initialize the x position and the fill color, use the constructor call:
GObject(x=2,fillcolor=colormodel.RED)
You do not need to provide the keywords as a dictionary. The ** in the parameter keywords does that automatically.
Any attribute of this class may be used as a keyword. The argument must satisfy the invariants of that attribute. See the list of attributes of this class for more information.
- Parameters:
keywords (keys are attribute names) – dictionary of keyword arguments
Attributes¶
- x¶
The horizontal coordinate of the object center.
invariant: Value must be an
int
orfloat
- y¶
The vertical coordinate of the object center.
invariant: Value must be an
int
orfloat
- left¶
The left edge of this shape.
The value depends on the current angle of rotation. If rotation is 0, it is
x-width/2
. Otherwise, it is the left-most value of the bounding box.Changing this value will shift the center of the object so that the left edge matches the new value.
Warning: Accessing this value on a rotated object may slow down your framerate.
invariant: Value must be an
int
orfloat
.
- right¶
The right edge of this shape.
The value depends on the current angle of rotation. If rotation is 0, it is
x+width/2
. Otherwise, it is the right-most value of the bounding box.Changing this value will shift the center of the object so that the right edge matches the new value.
Warning: Accessing this value on a rotated object may slow down your framerate.
invariant: Value must be an
int
orfloat
.
- bottom¶
The vertical coordinate of the bottom edge.
The value depends on the current angle of rotation. If rotation is 0, it is
y-height/2
. Otherwise, it is the bottom-most value of the bounding box.Changing this value will shift the center of the object so that the bottom edge matches the new value.
Warning: Accessing this value on a rotated object may slow down your framerate.
invariant: Value must be an
int
orfloat
.
- top¶
The vertical coordinate of the top edge.
The value depends on the current angle of rotation. If rotation is 0, it is
y+height/2
. Otherwise, it is the top-most value of the bounding box.Changing this value will shift the center of the object so that the top edge matches the new value.
Warning: Accessing this value on a rotated object may slow down your framerate.
invariant: Value must be an
int
orfloat
.
- linecolor¶
The object line color
This is the border color of the shape. If there no value (e.g. the linecolor is
None
), this shape will have no border.The default representation of color in GObject is a 4-element list of floats between 0 and 1 (representing r, g, b, and a). As with the Turtle, you may also assign color an RGB or HSV object from colormodel, or a string with a valid color name. If you chose either of these alternate representations (a string or an object from colormodel), Python will automatically convert the result into a 4-element list.
invariant: Value must be
None
or a 4-element list of floats between 0 and 1.
- fillcolor¶
The object fill color
This value is used to color the backgrounds or, in the case of solid shapes, the shape interior. If there no value (e.g. the fillcolor is
None
), this shape will have no interior.The default representation of color in GObject is a 4-element list of floats between 0 and 1 (representing r, g, b, and a). As with the Turtle, you may also assign color an RGB or HSV object from colormodel, or a string with a valid color name. If you chose either of these alternate representations (a string or an object from colormodel), Python will automatically convert the result into a 4-element list.
invariant: Value must be
None
or a 4-element list of floats between 0 and 1.
- scale¶
The scaling factor of this shape.
The scale is a fast way to cause a shape to grow or shrink in size. Essentially, the object will multiple the width and height by the scale. So a scale less than 1 will shrink the object, while a scale greater than 1 will enlarge the object.
The scale may either be a single number, or a pair of two numbers. If it is a single number, it will scale the width and height by the same amount. If it is a pair, it will scale the width by the first value, and the height by the second.
invariant: Value must be either a number (
int
orfloat
) or a pair of numbers.
- angle¶
The angle of rotation about the center.
The angle is measured in degrees (not radians) counter-clockwise.
invariant: Value must be an
int
orfloat
- name¶
The name of this object.
This value is for debugging purposes only. If you name an object, the name will appear when you convert the object to a string. This will allow you to tell which object is which in your watches.
invariant: Value must be a
str
orNone
Methods¶
- contains(point)¶
Checks whether this shape contains the point
By default, this method just checks the bounding box of the shape.
Warning: Using this method on a rotated object may slow down your framerate.
- Parameters:
point (
Point2
or a pair of numbers) – the point to check- Returns:
True if the shape contains this point
- Return type:
bool
- transform(point)¶
Transforms the point to the local coordinate system
This method is important for mouse selection. It helps you understand where in the shape the selection takes place. In the case of objects with children, like
GScene
, this method is necessary to properly use the contains method on the children.- Parameters:
point (
Point2
or a pair of numbers) – the point to transform- Returns:
The point transformed to local coordinate system
- Return type:
Point2