Skip to content

Icon

Create an image loaded from a file.

Creating an Icon

You can create an Icon using the following functions:

Icon(filename)
Icon(filename, width, height, rotation, visibility)
Parameter Type Default Description
filename str required The image file to load, ending in ".jpg" or ".png".
width int or float None The width to scale the image to, in pixels. Defaults to the image's own width.
height int or float None The height to scale the image to, in pixels. Defaults to the image's own height.
rotation int or float 0 How far to turn the image, in degrees, counter-clockwise.
visibility int 100 How visible the image is, from 0 (invisible) to 100 (fully visible).

For example,

icon = Icon("mona-lisa.jpg")

Once created, you can add it to a Display using the Display's add() function.

Functions

Once an Icon icon has been created, the following functions are available:

Function Description
icon.getPixel(column, row) Return the color of one pixel.
icon.getPixels() Return every pixel in the image.
icon.getColor() Return the last color used with setColor().
icon.setPixel(column, row, color) Set the color of one pixel.
icon.setPixels(pixels) Replace every pixel in the image.
icon.setColor() Sets every pixel to a single color.
icon.crop(x, y, width, height) Crop the image to a rectangular region.
icon.save(filename) Save the image to a file.

Position

Function Description
icon.getPosition() Return the object's position, the top-left corner of its bounding box.
icon.getX() Return the object's horizontal position.
icon.getY() Return the object's vertical position.
icon.getCenter() Return the object's center point.
icon.getCenterX() Return the object's horizontal center.
icon.getCenterY() Return the object's vertical center.
icon.setPosition(x, y) Move the object so the top-left corner of its bounding box sits at the given point.
icon.setX(x) Set the object's horizontal position.
icon.setY(y) Set the object's vertical position.
icon.setCenter(x, y) Move the object so its center sits at the given point.
icon.setCenterX(x) Set the object's horizontal center.
icon.setCenterY(y) Set the object's vertical center.
icon.move(x, y) Move the object to a new position.

Size

Function Description
icon.getSize() Return the object's width and height.
icon.getWidth() Return the object's width.
icon.getHeight() Return the object's height.
icon.setSize(width, height) Set the object's width and height.
icon.setWidth(width) Set the object's width.
icon.setHeight(height) Set the object's height.

Rotation

Function Description
icon.getRotation() Return how far the object is turned.
icon.setRotation(rotation) Turn the object to a given angle.
icon.rotate(angle) Turn the object by an additional angle.

Visibility

Function Description
icon.getVisibility() Return how visible the object is.
icon.setVisibility(visibility) Set how visible the object is.

Color

Function Description
icon.getFill() Report whether the shape is filled in.
icon.getThickness() Return the shape's outline thickness.
icon.setFill(fill) Set whether the shape is filled in.
icon.setThickness(thickness) Set the shape's outline thickness.

Information

Function Description
icon.getEndpoints() Return the object's four corners.
icon.getBoundingBox() Return the smallest upright box that surrounds the object.
icon.getGroup() Return the Group this object belongs to.
icon.getDisplay() Return the Display this object is on.
icon.setToolTipText() Set the hover text shown over the object.

Hit Testing

Function Description
icon.contains(x, y) Report whether a point lies inside the object.
icon.intersects(other) Report whether this object overlaps another.
icon.encloses(other) Report whether this object completely contains another.

Events

Function Description
icon.onMouseClick(action) Set up a function to call when the mouse is clicked on this object.
icon.onMouseDown(action) Set up a function to call when the mouse button is pressed on this object.
icon.onMouseUp(action) Set up a function to call when the mouse button is released over this object.
icon.onMouseMove(action) Set up a function to call when the mouse moves over this object.
icon.onMouseDrag(action) Set up a function to call when the mouse is dragged over this object.
icon.onMouseEnter(action) Set up a function to call when the mouse moves onto this object.
icon.onMouseExit(action) Set up a function to call when the mouse moves off this object.
icon.onKeyType(action) Set up a function to call when a key is typed (pressed and released).
icon.onKeyDown(action) Set up a function to call when a key is pressed down.
icon.onKeyUp(action) Set up a function to call when a key is released.