Interface Graphics
This interface is a thin wrapper around the underlying rendering backend (Processing) with a much smaller surface area.
Coordinates and units
- Coordinates are in pixels.
- Default origin is the top-left of the drawable viewport: (0, 0).
- Positive x goes right. Positive y goes down.
Colors
- RGB(A) channel values are in
[0, 255]. - Grayscale values are in
[0, 255].
State and isolation
- Fill, stroke, stroke weight, text size, and image tint are part of the current drawing state, along with transformations.
- Use
isolate(Runnable)to contain transformations and style changes. - Use
isolateStyle(Runnable)to contain style changes only (fill, stroke, stroke weight, text size, and image tint).
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumLine end-cap style used for stroked lines and shape outlines. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidRunsbodyafter translating to (x, y), then restores the previous state.default voidRuns a drawing block with its origin at the given position, restoring the previous state afterward.default voidbackground(double gray) Fills the viewport with a solid grayscale color.default voidbackground(double gray, double a) Fills the viewport with a solid grayscale color.voidbackground(double r, double g, double b) Fills the viewport with a solid RGB color.voidbackground(double r, double g, double b, double a) Fills the viewport with a solid RGB color.default voidbackground(Color c) Fills the viewport with aColor.voidcircle(double cx, double cy, double r) Draws a circle defined by center and radius.default voidDraws a circle centered at a vector at.voidclosedShape(Runnable body) Draws a closed shape defined byvertex(double, double)orcurveVertex(double, double)calls inside the block.voidcurveVertex(double x, double y) Adds a smooth curve point to the current shape.default voidcurveVertex(Vec2 position) Adds a smooth curve vertex at the given position.doublecx()Returns center x coordinate of the viewport (width() / 2.0).doublecy()Returns center y coordinate of the viewport (height() / 2.0).voidellipse(double cx, double cy, double w, double h) Draws an ellipse defined by center, width, and height.default voidfill(double gray) Sets the fill color to a grayscale value.default voidfill(double gray, double a) Sets the fill color to a grayscale value.voidfill(double r, double g, double b) Sets the fill color for subsequent shapes.voidfill(double r, double g, double b, double a) Sets the fill color for subsequent shapes.default voidSets the fillColorfor subsequent shapes.default inth()Short alias forheight().intheight()Returns height of the drawable viewport in pixels.default voidDraws an image at its native dimensions.voidDraws an image, resizing it to the given destination dimensions.voidRuns the block with isolated transform and style state.default voidConvenience overload for isolating a block that wants the currentGraphics.voidisolateStyle(Runnable block) Runs the block with isolated style state only.voidline(double x1, double y1, double x2, double y2) Draws a line from (x1, y1) to (x2, y2).default voidDraws a line between two positions.Loads an image from a file or packaged resource.voidnoFill()Disables filling for subsequent shapes.voidnoStroke()Disables drawing outlines for subsequent shapes.voidnoTint()Clears the image and sprite tint for subsequent draws.voidpoint(double x, double y) Draws a single point at (x, y).default voidDraws a point at a vector position.voidDraws a connected open polyline defined byvertex(double, double)orcurveVertex(double, double)calls inside the block.voidquad(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) Draws a quadrilateral connecting four points.voidrect(double x, double y, double w, double h) Draws a rectangle with a top-left corner at (x, y).default voidDraws a rectangle at the given top-left position.default voidDraws a rectangle using vectors for its position and dimensions.default voidrectCenter(double cx, double cy, double w, double h) Draws a rectangle centered at (cx, cy).default voidrectCenter(Vec2 center, double width, double height) Draws a rectangle centered at the given position.voidrotate(double radians) Rotates the coordinate system around the current origin (0, 0).default voidRunsbodyafter rotating by radians, then restores the previous state.default voidRunsbodywith its origin translated to (cx, cy) and rotated.voidscale(double factor) Scales the coordinate system uniformly in both dimensions.voidscale(double x, double y) Scales the coordinate system independently in x and y.default voidRunsbodyafter scaling uniformly byfactor, then restores the previous state.default voidRunsbodyafter translating to (cx, cy) and uniformly scaling the coordinate system.intscene()Current scene index selected by the user (typically 1..9).voidshearX(double radians) Shears the coordinate system along the x-axis.voidshearY(double radians) Shears the coordinate system along the y-axis.default voidsprite(SpriteDraw draw) Draws a sprite using its position, anchor, scale, rotation, flips, and optional tint.default voidDraws a sprite at its native dimensions.voidDraws a region of an image at the given destination dimensions.voidsquare(double x, double y, double side) Draws a square with a top-left corner at (x, y).default voidstroke(double gray) Sets the stroke (outline) color to a grayscale value.default voidstroke(double gray, double a) Sets the stroke (outline) color to a grayscale value.voidstroke(double r, double g, double b) Sets the stroke (outline) color for subsequent shapes.voidstroke(double r, double g, double b, double a) Sets the stroke (outline) color for subsequent shapes.default voidSets the stroke (outline)Colorfor subsequent shapes.voidSets the stroke end-cap style for subsequent stroked drawing operations.voidstrokeWeight(double px) Sets the stroke width in pixels.default voidDraws text at (x, y) with the default size of 16 pixels.voidDraws text at (x, y), with y specifying the text baseline.voidtint(double r, double g, double b, double a) Sets a color multiplier for subsequently drawn images and sprites.default voidSets the tint for images and sprites.voidtranslate(double x, double y) Offsets the coordinate system by the given amount.default voidTranslates the coordinate system by a vector.voidtriangle(double x1, double y1, double x2, double y2, double x3, double y3) Draws a triangle connecting three points.voidvertex(double x, double y) Adds a point to the current shape.default voidAdds a vertex at the given position to the current shape.default intw()Short alias forwidth().intwidth()Returns width of the drawable viewport in pixels.
-
Method Details
-
width
int width()Returns width of the drawable viewport in pixels.- Returns:
- width of the viewport
-
height
int height()Returns height of the drawable viewport in pixels.- Returns:
- height of the viewport
-
cx
double cx()Returns center x coordinate of the viewport (width() / 2.0).- Returns:
- center x coordinate of the viewport
-
cy
double cy()Returns center y coordinate of the viewport (height() / 2.0).- Returns:
- center y coordinate of the viewport
-
scene
int scene()Current scene index selected by the user (typically 1..9).How scenes are selected is backend-controlled. Treat this as a read-only selector used to switch between different visuals.
- Returns:
- current scene index
-
background
void background(double r, double g, double b) Fills the viewport with a solid RGB color.- Parameters:
r- red (0..255)g- green (0..255)b- blue (0..255)
-
background
void background(double r, double g, double b, double a) Fills the viewport with a solid RGB color.- Parameters:
r- red (0..255)g- green (0..255)b- blue (0..255)a- alpha (0..255)
-
background
-
background
default void background(double gray) Fills the viewport with a solid grayscale color.- Parameters:
gray- value (0=black, 255=white)
-
background
default void background(double gray, double a) Fills the viewport with a solid grayscale color.- Parameters:
gray- value (0=black, 255=white)a- alpha (0..255)
-
fill
void fill(double r, double g, double b) Sets the fill color for subsequent shapes.- Parameters:
r- red (0..255)g- green (0..255)b- blue (0..255)
-
fill
void fill(double r, double g, double b, double a) Sets the fill color for subsequent shapes.- Parameters:
r- red (0..255)g- green (0..255)b- blue (0..255)a- alpha (0..255)
-
fill
-
fill
default void fill(double gray) Sets the fill color to a grayscale value.- Parameters:
gray- value (0=black, 255=white)
-
fill
default void fill(double gray, double a) Sets the fill color to a grayscale value.- Parameters:
gray- value (0=black, 255=white)a- alpha (0..255)
-
noFill
void noFill()Disables filling for subsequent shapes. -
stroke
void stroke(double r, double g, double b) Sets the stroke (outline) color for subsequent shapes.- Parameters:
r- red (0..255)g- green (0..255)b- blue (0..255)
-
stroke
-
stroke
void stroke(double r, double g, double b, double a) Sets the stroke (outline) color for subsequent shapes.- Parameters:
r- red (0..255)g- green (0..255)b- blue (0..255)a- alpha (0..255)
-
stroke
default void stroke(double gray) Sets the stroke (outline) color to a grayscale value.- Parameters:
gray- value (0=black, 255=white)
-
stroke
default void stroke(double gray, double a) Sets the stroke (outline) color to a grayscale value.- Parameters:
gray- value (0=black, 255=white)a- alpha (0..255)
-
noStroke
void noStroke()Disables drawing outlines for subsequent shapes. -
strokeWeight
void strokeWeight(double px) Sets the stroke width in pixels.- Parameters:
px- width in pixels
-
strokeCap
Sets the stroke end-cap style for subsequent stroked drawing operations.The default is SQUARE.
Example:
g.strokeWeight(6); g.strokeCap(Graphics.StrokeCap.ROUND); g.line(50, 50, 250, 50);- Parameters:
cap- the desired stroke cap style (must not benull)- Throws:
NullPointerException- ifcapisnull
-
line
void line(double x1, double y1, double x2, double y2) Draws a line from (x1, y1) to (x2, y2).- Parameters:
x1- first endpoint x-coordinatey1- first endpoint y-coordinatex2- second endpoint x-coordinatey2- second endpoint y-coordinate
-
rect
void rect(double x, double y, double w, double h) Draws a rectangle with a top-left corner at (x, y).- Parameters:
x- top-left xy- top-left yw- widthh- height
-
square
void square(double x, double y, double side) Draws a square with a top-left corner at (x, y).- Parameters:
x- top-left xy- top-left yside- side length
-
ellipse
void ellipse(double cx, double cy, double w, double h) Draws an ellipse defined by center, width, and height.- Parameters:
cx- center xcy- center yw- widthh- height
-
circle
void circle(double cx, double cy, double r) Draws a circle defined by center and radius.- Parameters:
cx- center xcy- center yr- radius (pixels)
-
triangle
void triangle(double x1, double y1, double x2, double y2, double x3, double y3) Draws a triangle connecting three points.- Parameters:
x1- x of point 1y1- y of point 1x2- x of point 2y2- y of point 2x3- x of point 3y3- y of point 3
-
quad
void quad(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) Draws a quadrilateral connecting four points.- Parameters:
x1- x of point 1y1- y of point 1x2- x of point 2y2- y of point 2x3- x of point 3y3- y of point 3x4- x of point 4y4- y of point 4
-
point
void point(double x, double y) Draws a single point at (x, y).- Parameters:
x- horizontal coordinatey- vertical coordinate
-
translate
void translate(double x, double y) Offsets the coordinate system by the given amount.- Parameters:
x- horizontal offsety- vertical offset
-
rotate
void rotate(double radians) Rotates the coordinate system around the current origin (0, 0).With the default screen coordinate system (y down), positive angles rotate clockwise.
- Parameters:
radians- angle to rotate, in radians
-
shearX
void shearX(double radians) Shears the coordinate system along the x-axis.- Parameters:
radians- shear angle in radians
-
shearY
void shearY(double radians) Shears the coordinate system along the y-axis.- Parameters:
radians- shear angle in radians
-
scale
void scale(double factor) Scales the coordinate system uniformly in both dimensions.- Parameters:
factor- scale factor
-
scale
void scale(double x, double y) Scales the coordinate system independently in x and y.- Parameters:
x- horizontal scale factory- vertical scale factor
-
isolate
Runs the block with isolated transform and style state.Any changes made inside the block (translate/rotate/scale and fill/stroke/stroke weight/text size/image tint) are discarded when this method returns.
- Parameters:
block- code to run in isolation
-
isolate
-
isolateStyle
Runs the block with isolated style state only.Style changes (fill, stroke, strokeWeight/Cap, text size, tint) are discarded when this method returns. Transform changes (translate/rotate/scale) are not isolated.
- Parameters:
block- code to run with isolated style
-
w
-
h
-
rectCenter
default void rectCenter(double cx, double cy, double w, double h) Draws a rectangle centered at (cx, cy).- Parameters:
cx- center xcy- center yw- widthh- height
-
at
Runsbodyafter translating to (x, y), then restores the previous state.- Parameters:
x- horizontal offsety- vertical offsetbody- drawing operations to run
-
rotated
Runsbodyafter rotating by radians, then restores the previous state.- Parameters:
radians- angle of rotationbody- drawing operations to run
-
rotatedAt
Runsbodywith its origin translated to (cx, cy) and rotated.Inside the block, drawing at (0, 0) places a shape at the pivot. Coordinates are relative to that pivot, and the previous transform and style are restored after the block finishes.
- Parameters:
radians- rotation angle in radianscx- pivot x-coordinatecy- pivot y-coordinatebody- drawing operations to run
-
scaled
Runsbodyafter scaling uniformly byfactor, then restores the previous state.- Parameters:
factor- scale multiplierbody- drawing operations to run
-
scaledAt
Runsbodyafter translating to (cx, cy) and uniformly scaling the coordinate system. The previous state is restored afterward.- Parameters:
factor- scale multipliercx- x-coordinate of the new origincy- y-coordinate of the new originbody- drawing operations to run
-
vertex
void vertex(double x, double y) Adds a point to the current shape.Only valid inside
polyline(Runnable)orclosedShape(Runnable).- Parameters:
x- vertex x-coordinatey- vertex y-coordinate
-
curveVertex
void curveVertex(double x, double y) Adds a smooth curve point to the current shape.Only valid inside
polyline(Runnable)orclosedShape(Runnable).- Parameters:
x- curve vertex x-coordinatey- curve vertex y-coordinate
-
polyline
Draws a connected open polyline defined byvertex(double, double)orcurveVertex(double, double)calls inside the block.The block should only call
vertex(double, double)and/orcurveVertex(double, double). Nested shape blocks are not supported. Style changes are isolated; transform changes inside the block are not isolated.g.polyline(() -> { g.curveVertex(0, 100); g.curveVertex(50, 0); g.curveVertex(100, 100); });- Parameters:
body- code that callsvertex(double, double)orcurveVertex(double, double)to define the shape
-
closedShape
Draws a closed shape defined byvertex(double, double)orcurveVertex(double, double)calls inside the block.The block should only call
vertex(double, double)and/orcurveVertex(double, double). Nested shape blocks are not supported. Style changes are isolated; transform changes inside the block are not isolated.g.fill(255, 0, 0); g.closedShape(() -> { g.vertex(0, 100); g.vertex(50, 0); g.vertex(100, 100); });- Parameters:
body- code that callsvertex(double, double)orcurveVertex(double, double)to define the shape
-
text
Draws text at (x, y), with y specifying the text baseline.- Parameters:
x- horizontal position in pixelsy- baseline position in pixelstext- text to drawsize- text size in pixels
-
text
Draws text at (x, y) with the default size of 16 pixels. The y-coordinate specifies the text baseline.- Parameters:
x- horizontal position in pixelsy- baseline position in pixelstext- text to draw
-
tint
void tint(double r, double g, double b, double a) Sets a color multiplier for subsequently drawn images and sprites. White preserves the source colors; the alpha channel controls opacity. Tint does not affect geometric shapes.- Parameters:
r- red multiplier, 0..255g- green multiplier, 0..255b- blue multiplier, 0..255a- opacity, 0..255
-
tint
Sets the tint for images and sprites.- Parameters:
color- color multiplier, including alpha
-
noTint
void noTint()Clears the image and sprite tint for subsequent draws. -
loadImage
Loads an image from a file or packaged resource.If images are stored in the project's
assets/directory, pass the directory as part of the path, for exampleg.loadImage("assets/characters.png"). Theassets/prefix is not added automatically.- Parameters:
path- image path or resource name- Returns:
- loaded image
-
image
Draws an image, resizing it to the given destination dimensions.- Parameters:
image- image to drawx- destination left edgey- destination top edgew- destination width in pixelsh- destination height in pixels
-
image
Draws an image at its native dimensions.- Parameters:
image- image to drawx- destination left edgey- destination top edge
-
sprite
Draws a region of an image at the given destination dimensions.- Parameters:
sprite- source sprite regionx- destination left edgey- destination top edgew- destination width in pixelsh- destination height in pixels
-
sprite
Draws a sprite at its native dimensions.- Parameters:
sprite- source sprite regionx- destination left edgey- destination top edge
-
sprite
Draws a sprite using its position, anchor, scale, rotation, flips, and optional tint.Style and transforms are isolated, so the drawing instructions do not change the state used for subsequent shapes or sprites.
- Parameters:
draw- drawing instructions for the sprite
-
point
Draws a point at a vector position.- Parameters:
position- point location
-
line
-
circle
Draws a circle centered at a vector at.- Parameters:
center- circle centerradius- circle radius in pixels
-
translate
Translates the coordinate system by a vector.- Parameters:
offset- horizontal and vertical displacement
-
at
-
rect
Draws a rectangle at the given top-left position.- Parameters:
position- top-left cornerwidth- rectangle widthheight- rectangle height
-
rect
-
rectCenter
Draws a rectangle centered at the given position.- Parameters:
center- rectangle centerwidth- rectangle widthheight- rectangle height
-
vertex
Adds a vertex at the given position to the current shape.- Parameters:
position- vertex coordinates
-
curveVertex
Adds a smooth curve vertex at the given position.- Parameters:
position- curve vertex coordinates
-