Package codedraw
Class CodeDraw
java.lang.Object
codedraw.CodeDraw
CodeDraw is an easy-to-use drawing library where you use code to create pictures and animations.
It is made for beginners that understand little about programming
and makes it very simple to draw and animate various shapes and images to a canvas.
You can view the CodeDraw repository here.
To get a brief overview and a bunch of examples visit the README of the CodeDraw repository.
Read the introduction to CodeDraw for a beginners guide to CodeDraw.
For the JavaDoc visit CodeDrawJavaDoc.
An example to get you started:
You can view the CodeDraw repository here.
To get a brief overview and a bunch of examples visit the README of the CodeDraw repository.
Read the introduction to CodeDraw for a beginners guide to CodeDraw.
For the JavaDoc visit CodeDrawJavaDoc.
An example to get you started:
import codedraw.*;
public class MyProgram {
public static void main(String[] args) {
// Creates a new CodeDraw window with the size of 600x600 pixel
CodeDraw cd = new CodeDraw();
// Sets the drawing color to red
cd.setColor(Palette.RED);
// Draws the outline of a rectangle
cd.drawRectangle(100, 100, 200, 100);
// Draws a filled Square
cd.fillSquare(180, 150, 80);
// Changes the color to light blue
cd.setColor(Palette.LIGHT_BLUE);
cd.fillCircle(300, 200, 50);
// Finally, the method show must be called
// to display the drawn shapes in the CodeDraw window.
cd.show();
}
}
Fun Fact: You can copy the currently displayed canvas to your clipboard by pressing Ctrl + C-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Colors the whole canvas in white.void
Colors the whole canvas in the color given as a parameter.void
dispose()
Closes the frame and disposes all created resources associated with this CodeDraw instance.void
dispose
(boolean exit) Closes the frame and disposes all created resources associated with this CodeDraw instance.void
drawArc
(double x, double y, double radius, double startRadians, double sweepRadians) Draws the outline of an arc with the center being the (x, y) coordinates.void
drawArc
(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws the outline of an arc with the center being the (x, y) coordinates.void
drawBezier
(double startX, double startY, double control1X, double control1Y, double control2X, double control2Y, double endX, double endY) Draws a cubic bezier curve.void
drawCircle
(double x, double y, double radius) Draws the outline of a circle.void
drawCurve
(double startX, double startY, double controlX, double controlY, double endX, double endY) Draws a quadratic bezier curve.void
drawEllipse
(double x, double y, double horizontalRadius, double verticalRadius) Draws the outline of an ellipse.void
Draws an image at the specified (x, y) coordinate.void
Draws an image at the specified (x, y) coordinate.void
Draws an image at the specified (x, y) coordinate.void
Draws an image at the specified (x, y) coordinate.void
Draws an image at the specified (x, y) coordinate.void
Draws an image at the specified (x, y) coordinate.void
drawLine
(double startX, double startY, double endX, double endY) Draws a straight line between the start point and end point.void
drawPie
(double x, double y, double radius, double startRadians, double sweepRadians) Draws the outline of a pie with the center being the (x, y) coordinates.void
drawPie
(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws the outline of a pie with the center being the (x, y) coordinates.void
drawPixel
(double x, double y) Draws a point which is exactly 1x1 pixel in size.void
drawPoint
(double x, double y) Draws a point which changes size depending on thegetLineWidth()
void
drawPolygon
(double... points) Draws the outline of a polygon.void
drawRectangle
(double x, double y, double width, double height) Draws the outline of a rectangle.void
drawSquare
(double x, double y, double sideLength) Draws the outline of a square.void
Draws the text at the specified (x, y) coordinate.void
drawTriangle
(double x1, double y1, double x2, double y2, double x3, double y3) Draws the outline of a triangle.void
fillCircle
(double x, double y, double radius) Draws a filled circle.void
fillEllipse
(double x, double y, double horizontalRadius, double verticalRadius) Draws a filled ellipse.void
fillPie
(double x, double y, double radius, double startRadians, double sweepRadians) Draws a filled pie with the center being the (x, y) coordinates.void
fillPie
(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws a filled pie with the center being the (x, y) coordinates.void
fillPolygon
(double... points) Draws a filled polygon.void
fillRectangle
(double x, double y, double width, double height) Draws a filled rectangle.void
fillSquare
(double x, double y, double sideLength) Draws a filled square.void
fillTriangle
(double x1, double y1, double x2, double y2, double x3, double y3) Draws a filled triangle.int
Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas.int
Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas.getColor()
Defines the color that is used for drawing all shapes.Defines how the corners of drawn shapes should look.Defines the style of the cursor while hovering of the CodeDraw canvas.int
This value cannot be changed once set via the constructor.double
Defines the width or thickness of drawn shapes and lines.Defines the styling of drawn text.getTitle()
The title is the text displayed in the top left corner of the CodeDraw window.int
getWidth()
This value cannot be changed once set via the constructor.int
Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window.int
Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window.boolean
Defines whether draw text, drawn shapes and filled shapes are anti-aliased.void
isAntiAliased
(boolean isAntiAliased) Defines whether drawn text, drawn shapes and filled shapes are anti-aliased.onKeyDown
(EventHandler<CodeDraw, KeyEvent> handler) Trigger exactly once when a key is pressed down.onKeyPress
(EventHandler<CodeDraw, KeyEvent> handler) onKeyPress will continuously trigger while a key is being held down.onKeyUp
(EventHandler<CodeDraw, KeyEvent> handler) Trigger when a key is released.onMouseClick
(EventHandler<CodeDraw, MouseEvent> handler) Triggers once when a mouse button is pressed down and quickly released again.onMouseDown
(EventHandler<CodeDraw, MouseEvent> handler) Triggers exactly once when a mouse button is pressed down.onMouseEnter
(EventHandler<CodeDraw, MouseEvent> handler) Triggers when the mouse enters the canvas.onMouseLeave
(EventHandler<CodeDraw, MouseEvent> handler) Triggers when the mouse leaves the canvas.onMouseMove
(EventHandler<CodeDraw, MouseEvent> handler) Triggers continuously while the mouse is being moved.onMouseUp
(EventHandler<CodeDraw, MouseEvent> handler) Triggers when a mouse button is released.onMouseWheel
(EventHandler<CodeDraw, MouseWheelEvent> handler) Triggers each time the mouse wheel is turned.onWindowMove
(EventHandler<CodeDraw, ComponentEvent> handler) Triggers every time the CodeDraw window is moved.Creates a copy of the current buffer (not the displayed image) and returns it as a buffered image.void
setCanvasPositionX
(int x) Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas.void
setCanvasPositionY
(int y) Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas.void
Defines the color that is used for drawing all shapes.void
Defines how the corners of drawn shapes should look.void
setCursorStyle
(CursorStyle cursorStyle) Defines the style of the cursor while hovering of the CodeDraw canvas.void
setLineWidth
(double lineWidth) Defines the width or thickness of drawn shapes and lines.void
setTextFormat
(TextFormat textFormat) Defines the styling of drawn text.void
The title is the text displayed in the top left corner of the CodeDraw window.void
setWindowPositionX
(int x) Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window.void
setWindowPositionY
(int y) Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window.void
show()
Displays the drawn shapes and images on the canvas.void
show
(int waitMilliseconds) Displays the drawn shapes and images on the canvas and then waits for the given amount of milliseconds.toString()
-
Constructor Details
-
CodeDraw
public CodeDraw()Creates a canvas with size 600x600 pixels. The frame surrounding the canvas will be slightly bigger. The size remains fixed after calling this constructor. -
CodeDraw
public CodeDraw(int canvasWidth, int canvasHeight) Creates a canvas with the specified size. The frame surrounding the canvas will be slightly bigger. Once the size is set via this constructor it remains fixed.- Parameters:
canvasWidth
- must be at least 150 pixelcanvasHeight
- must be at least 1 pixel
-
-
Method Details
-
getWindowPositionX
public int getWindowPositionX()Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window. Changing the window position also changes the canvas position.- Returns:
- The distance in pixel from the left side of the main screen to the left of the CodeDraw window.
-
getWindowPositionY
public int getWindowPositionY()Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window. Changing the window position also changes the canvas position.- Returns:
- The distance in pixel from the top side of the main screen to the top of the CodeDraw window.
-
setWindowPositionX
public void setWindowPositionX(int x) Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window. Changing the window position also changes the canvas position.- Parameters:
x
- The distance in pixel from the left side of the main screen to the left of the CodeDraw window.
-
setWindowPositionY
public void setWindowPositionY(int y) Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw window. Changing the window position also changes the canvas position.- Parameters:
y
- The distance in pixel from the top side of the main screen to the top of the CodeDraw window.
-
getCanvasPositionX
public int getCanvasPositionX()Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas. The top left corner of the canvas is the origin point for all drawn objects. Changing the canvas position also changes the window position.- Returns:
- The distance in pixel from the left side of the main screen to the left of the CodeDraw canvas.
-
getCanvasPositionY
public int getCanvasPositionY()Gets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas. The top left corner of the canvas is the origin point for all drawn objects. Changing the canvas position also changes the window position.- Returns:
- The distance in pixel from the top side of the main screen to the top of the CodeDraw canvas.
-
setCanvasPositionX
public void setCanvasPositionX(int x) Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas. The top left corner of the canvas is the origin point for all drawn objects. Changing the canvas position also changes the window position.- Parameters:
x
- The distance in pixel from the left side of the main screen to the left of the CodeDraw canvas.
-
setCanvasPositionY
public void setCanvasPositionY(int y) Sets the distance in pixel from the top left corner of the screen to the top left corner of CodeDraw canvas. The top left corner of the canvas is the origin point for all drawn objects. Changing the canvas position also changes the window position.- Parameters:
y
- The distance in pixel from the top side of the main screen to the top of the CodeDraw canvas.
-
getWidth
public int getWidth()This value cannot be changed once set via the constructor.- Returns:
- the width of the canvas.
-
getHeight
public int getHeight()This value cannot be changed once set via the constructor.- Returns:
- the height of the canvas.
-
getLineWidth
public double getLineWidth()Defines the width or thickness of drawn shapes and lines.- Returns:
- the lineWidth of this CodeDraw window.
-
setLineWidth
public void setLineWidth(double lineWidth) Defines the width or thickness of drawn shapes and lines.- Parameters:
lineWidth
- Sets the lineWidth of this CodeDraw window.
-
getTextFormat
Defines the styling of drawn text. See alsodrawText(double, double, String)
- Returns:
- the text formatting options of this CodeDraw window.
-
setTextFormat
Defines the styling of drawn text. See alsodrawText(double, double, String)
- Parameters:
textFormat
- Sets the text formatting options of this CodeDraw window.
-
getCursorStyle
Defines the style of the cursor while hovering of the CodeDraw canvas. See alsoCursorStyle
.- Returns:
- the cursor style of this CodeDraw canvas.
-
setCursorStyle
Defines the style of the cursor while hovering of the CodeDraw canvas. See alsoCursorStyle
.- Parameters:
cursorStyle
- Sets the cursor style of this CodeDraw canvas.
-
isAntiAliased
public boolean isAntiAliased()Defines whether draw text, drawn shapes and filled shapes are anti-aliased. See Wikipedia Spatial Anti-aliasing- Returns:
- Whether this CodeDraw window anti aliases.
-
isAntiAliased
public void isAntiAliased(boolean isAntiAliased) Defines whether drawn text, drawn shapes and filled shapes are anti-aliased. See Wikipedia Spatial Anti-aliasing- Parameters:
isAntiAliased
- Sets whether this CodeDraw window anti aliases.
-
getCorner
Defines how the corners of drawn shapes should look.- Returns:
- the corner style of this CodeDraw window.
-
setCorner
Defines how the corners of drawn shapes should look.- Parameters:
corner
- Sets the corner style of this CodeDraw window.
-
getTitle
The title is the text displayed in the top left corner of the CodeDraw window.- Returns:
- the text of the title.
-
setTitle
The title is the text displayed in the top left corner of the CodeDraw window.- Parameters:
title
- Sets the text of the title.
-
getColor
Defines the color that is used for drawing all shapes.- Returns:
- the drawing color of this CodeDraw window.
-
setColor
Defines the color that is used for drawing all shapes.- Parameters:
color
- Sets the drawing color of this CodeDraw window.
-
onMouseClick
Triggers once when a mouse button is pressed down and quickly released again.- Parameters:
handler
- A lambda or function reference.
-
onMouseMove
Triggers continuously while the mouse is being moved.- Parameters:
handler
- A lambda or function reference.
-
onMouseDown
Triggers exactly once when a mouse button is pressed down.- Parameters:
handler
- A lambda or function reference.
-
onMouseUp
Triggers when a mouse button is released.- Parameters:
handler
- A lambda or function reference.
-
onMouseEnter
Triggers when the mouse enters the canvas.- Parameters:
handler
- A lambda or function reference.
-
onMouseLeave
Triggers when the mouse leaves the canvas.- Parameters:
handler
- A lambda or function reference.
-
onMouseWheel
Triggers each time the mouse wheel is turned.- Parameters:
handler
- A lambda or function reference.
-
onKeyDown
Trigger exactly once when a key is pressed down.- Parameters:
handler
- A lambda or function reference.
-
onKeyUp
Trigger when a key is released.- Parameters:
handler
- A lambda or function reference.
-
onKeyPress
onKeyPress will continuously trigger while a key is being held down.- Parameters:
handler
- A lambda or function reference.
-
onWindowMove
Triggers every time the CodeDraw window is moved.- Parameters:
handler
- A lambda or function reference.
-
drawText
Draws the text at the specified (x, y) coordinate. Formatting options can be set via the TextFormat object. SeegetTextFormat()
,setTextFormat(TextFormat)
and theTextFormat
class. If not specified otherwise in the TextFormat object the (x, y) coordinates will be in the top left corner of the text.- Parameters:
x
- The distance in pixel from the left side of the canvas.y
- The distance in pixel from the top side of the canvas.text
- The text or string to be drawn.
-
drawPixel
public void drawPixel(double x, double y) Draws a point which is exactly 1x1 pixel in size.- Parameters:
x
- The distance in pixel from the left side of the canvas.y
- The distance in pixel from the top side of the canvas.
-
drawPoint
public void drawPoint(double x, double y) Draws a point which changes size depending on thegetLineWidth()
- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the point.y
- The distance in pixel from the top side of the canvas to the center of the point.
-
drawLine
public void drawLine(double startX, double startY, double endX, double endY) Draws a straight line between the start point and end point.- Parameters:
startX
- The distance in pixel from the left side of the canvas to the start of the line.startY
- The distance in pixel from the top side of the canvas to the start of the line.endX
- The distance in pixel from the left side of the canvas to the end of the line.endY
- The distance in pixel from the top side of the canvas to the end of the line.
-
drawCurve
public void drawCurve(double startX, double startY, double controlX, double controlY, double endX, double endY) Draws a quadratic bezier curve. See: Wikipedia Bezier Curve The start and end of the curve will be precisely where startX/Y and endX/Y are specified. The controlX/Y parameter specifies in what way the curve will be bent.- Parameters:
startX
- The distance in pixel from the left side of the canvas to the start of the curve.startY
- The distance in pixel from the top side of the canvas to the start of the curve.controlX
- Defines the way the curve bends in the x direction.controlY
- Defines the way the curve bends in the y direction.endX
- The distance in pixel from the left side of the canvas to the end of the curve.endY
- The distance in pixel from the top side of the canvas to the end of the curve.
-
drawBezier
public void drawBezier(double startX, double startY, double control1X, double control1Y, double control2X, double control2Y, double endX, double endY) Draws a cubic bezier curve. See Wikipedia Bezier Curve The start and end of the curve will be precisely where startX/Y and endX/Y are specified. The control1X/Y and control2X/Y parameter specify in what way the curve will be bent.- Parameters:
startX
- The distance in pixel from the left side of the canvas to the start of the curve.startY
- The distance in pixel from the top side of the canvas to the start of the curve.control1X
- Defines the way the curve bends in the x direction.control1Y
- Defines the way the curve bends in the y direction.control2X
- Defines the way the curve bends in the x direction.control2Y
- Defines the way the curve bends in the y direction.endX
- The distance in pixel from the left side of the canvas to the end of the curve.endY
- The distance in pixel from the top side of the canvas to the end of the curve.
-
drawSquare
public void drawSquare(double x, double y, double sideLength) Draws the outline of a square.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left size of the square.y
- The distance in pixel from the top side of the canvas to the top side of the square.sideLength
- The width and the height of the square in pixel.
-
fillSquare
public void fillSquare(double x, double y, double sideLength) Draws a filled square.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left size of the square.y
- The distance in pixel from the top side of the canvas to the top side of the square.sideLength
- The width and the height of the square in pixel.
-
drawRectangle
public void drawRectangle(double x, double y, double width, double height) Draws the outline of a rectangle.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left size of the rectangle.y
- The distance in pixel from the top side of the canvas to the top side of the rectangle.height
- The height of the rectangle in pixel.width
- The width of the rectangle in pixel.
-
fillRectangle
public void fillRectangle(double x, double y, double width, double height) Draws a filled rectangle.- Parameters:
x
- The distance in pixel from the left side of the canvas to the left size of the rectangle.y
- The distance in pixel from the top side of the canvas to the top side of the rectangle.height
- The height of the rectangle in pixel.width
- The width of the rectangle in pixel.
-
drawCircle
public void drawCircle(double x, double y, double radius) Draws the outline of a circle. The center of the circle will be at the specified (x, y) coordinate.- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the circle.y
- The distance in pixel from the top side of the canvas to the center of the circle.radius
- The radius of the circle in pixel.
-
fillCircle
public void fillCircle(double x, double y, double radius) Draws a filled circle. The center of the circle will be at the specified (x, y) coordinate.- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the circle.y
- The distance in pixel from the top side of the canvas to the center of the circle.radius
- The radius of the circle in pixel.
-
drawEllipse
public void drawEllipse(double x, double y, double horizontalRadius, double verticalRadius) Draws the outline of an ellipse. The center of the ellipse will be at the specified (x, y) coordinate.- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the ellipse.y
- The distance in pixel from the top side of the canvas to the center of the ellipse.horizontalRadius
- The horizontal radius of the ellipse in pixel. The width of the ellipse is 2 * horizontalRadius.verticalRadius
- The vertical radius of the ellipse in pixel. The height of the ellipse is 2 * verticalRadius.
-
fillEllipse
public void fillEllipse(double x, double y, double horizontalRadius, double verticalRadius) Draws a filled ellipse. The center of the ellipse will be at the specified (x, y) coordinate.- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the ellipse.y
- The distance in pixel from the top side of the canvas to the center of the ellipse.horizontalRadius
- The horizontal radius of the ellipse in pixel. The width of the ellipse is 2 * horizontalRadius.verticalRadius
- The vertical radius of the ellipse in pixel. The height of the ellipse is 2 * verticalRadius.
-
drawArc
public void drawArc(double x, double y, double radius, double startRadians, double sweepRadians) Draws the outline of an arc with the center being the (x, y) coordinates. The arc starts at the 12 o'clock position offset by the startRadians parameter. The total length of the arc is defined by the sweepRadians parameter.- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the arc.y
- The distance in pixel from the top side of the canvas to the center of the arc.radius
- The radius of the arc in pixel.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 12 o'clock going clock-wise.sweepRadians
- The length of the arc in radians from the start angle in a clockwise direction.
-
drawArc
public void drawArc(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws the outline of an arc with the center being the (x, y) coordinates. The width is 2 * horizontalRadius and the height is 2 * verticalRadius. The arc starts at the 12 o'clock position offset by the startRadians parameter. The total length of the arc is defined by the sweepRadians parameter.- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the arc.y
- The distance in pixel from the top side of the canvas to the center of the arc.horizontalRadius
- The horizontal radius of the arc in pixel. The width of the arc is 2 * horizontalRadius.verticalRadius
- The vertical radius of the arc in pixel. The height of the arc is 2 * verticalRadius.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 12 o'clock going clock-wise.sweepRadians
- The length of the arc in radians from the start angle in a clockwise direction.
-
drawPie
public void drawPie(double x, double y, double radius, double startRadians, double sweepRadians) Draws the outline of a pie with the center being the (x, y) coordinates. The pie starts at the 12 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the pie.y
- The distance in pixel from the top side of the canvas to the center of the pie.radius
- The radius of the pie in pixel.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 12 o'clock going clock-wise.sweepRadians
- The length of the pie in radians from the start angle in a clockwise direction.
-
drawPie
public void drawPie(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws the outline of a pie with the center being the (x, y) coordinates. The width is 2 * horizontalRadius and the height is the 2 * verticalRadius. The pie starts at the 12 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the pie.y
- The distance in pixel from the top side of the canvas to the center of the pie.horizontalRadius
- The horizontal radius of the pie in pixel. The width of the pie is 2 * horizontalRadius.verticalRadius
- The vertical radius of the pie in pixel. The height of the pie is 2 * verticalRadius.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 12 o'clock going clock-wise.sweepRadians
- The length of the pie in radians from the start angle in a clockwise direction.
-
fillPie
public void fillPie(double x, double y, double radius, double startRadians, double sweepRadians) Draws a filled pie with the center being the (x, y) coordinates. The pie starts at the 12 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the pie.y
- The distance in pixel from the top side of the canvas to the center of the pie.radius
- The radius of the pie in pixel.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 12 o'clock going clock-wise.sweepRadians
- The length of the pie in radians from the start angle in a clockwise direction.
-
fillPie
public void fillPie(double x, double y, double horizontalRadius, double verticalRadius, double startRadians, double sweepRadians) Draws a filled pie with the center being the (x, y) coordinates. The width is 2 * horizontalRadius and the height is the 2 * verticalRadius. The pie starts at the 12 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.- Parameters:
x
- The distance in pixel from the left side of the canvas to the center of the pie.y
- The distance in pixel from the top side of the canvas to the center of the pie.horizontalRadius
- The horizontal radius of the pie in pixel. The width of the pie is 2 * horizontalRadius.verticalRadius
- The vertical radius of the pie in pixel. The height of the pie is 2 * verticalRadius.startRadians
- The starting angle in radians. A 0 radians angle would be interpreted as starting at 12 o'clock going clock-wise.sweepRadians
- The length of the pie in radians from the start angle in a clockwise direction.
-
drawTriangle
public void drawTriangle(double x1, double y1, double x2, double y2, double x3, double y3) Draws the outline of a triangle.- Parameters:
x1
- The distance in pixel from the left side of the canvas to the first corner of the triangle.y1
- The distance in pixel from the top side of the canvas to the first corner of the triangle.x2
- The distance in pixel from the left side of the canvas to the second corner of the triangle.y2
- The distance in pixel from the top side of the canvas to the second corner of the triangle.x3
- The distance in pixel from the left side of the canvas to the third corner of the triangle.y3
- The distance in pixel from the top side of the canvas to the third corner of the triangle.
-
fillTriangle
public void fillTriangle(double x1, double y1, double x2, double y2, double x3, double y3) Draws a filled triangle.- Parameters:
x1
- The distance in pixel from the left side of the canvas to the first corner of the triangle.y1
- The distance in pixel from the top side of the canvas to the first corner of the triangle.x2
- The distance in pixel from the left side of the canvas to the second corner of the triangle.y2
- The distance in pixel from the top side of the canvas to the second corner of the triangle.x3
- The distance in pixel from the left side of the canvas to the third corner of the triangle.y3
- The distance in pixel from the top side of the canvas to the third corner of the triangle.
-
drawPolygon
public void drawPolygon(double... points) Draws the outline of a polygon. This function must be called with an even number of parameters. Each parameter pair (x, y) represents a corner of the polygon. Must be called with at least two points as parameter. Each point passed to drawPolygon will be connected to the following points and the last point will be connected to the first point.cd.drawPolygon( 200, 100, 100, 200, 300, 200 );
- Parameters:
points
- An even number of doubles. Each pair represents one corner of the polygon.
-
fillPolygon
public void fillPolygon(double... points) Draws a filled polygon. This function must be called with an even number of parameters. Each parameter pair (x, y) represents a corner of the polygon. Must be called with at least two points as parameter. Each point passed to drawPolygon will be connected to the following points and the last point will be connected to the first point.cd.fillPolygon( 200, 100, 100, 200, 300, 200 );
- Parameters:
points
- An even number of doubles. Each pair represents one corner of the polygon.
-
drawImage
Draws an image at the specified (x, y) coordinate. The width and height of the image will be used to draw the image.CodeDraw cd = new CodeDraw(); BufferedImage img; try { img = ImageIO.read(new File("C:\\pathToDirectory\\filename.png")); } catch (IOException e) { throw new UncheckedIOException(e); } cd.drawImage(100, 100, img); cd.show();
- Parameters:
x
- The distance in pixel from the left side of the canvas to the left side of the image.y
- The distance in pixel from the top side of the canvas to the top side of the image.image
- The image that will be drawn on the canvas.
-
drawImage
Draws an image at the specified (x, y) coordinate. The width and height of the image will be used to draw the image.CodeDraw cd = new CodeDraw(); cd.drawImage(100, 100, new File("C:\\pathToDirectory\\filename.png")); cd.show();
Supported image formats are: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).- Parameters:
x
- The distance in pixel from the left side of the canvas to the left side of the image.y
- The distance in pixel from the top side of the canvas to the top side of the image.file
- A file that points to an image file. SeeImageIO.read(File)
-
drawImage
Draws an image at the specified (x, y) coordinate. The width and height of the image will be used to draw the image.CodeDraw cd = new CodeDraw(); cd.drawImage(100, 100, "C:\\pathToDirectory\\filename.png"); cd.show();
Supported image formats are: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).- Parameters:
x
- The distance in pixel from the left side of the canvas to the left side of the image.y
- The distance in pixel from the top side of the canvas to the top side of the image.fileName
- A fileName that points to an image file. SeeImageIO.read(File)
-
drawImage
Draws an image at the specified (x, y) coordinate. The image will be rescaled to fit within the width and height given as parameters.
The size of the example image will be 200x200 pixel.CodeDraw cd = new CodeDraw(); BufferedImage img; try { img = ImageIO.read(new File("C:\\pathToDirectory\\filename.png")); } catch (IOException e) { throw new UncheckedIOException(e); } cd.drawImage(100, 100, 200, 200, img); cd.show();
- Parameters:
x
- The distance in pixel from the left side of the canvas to the left side of the image.y
- The distance in pixel from the top side of the canvas to the top side of the image.image
- The image that will be drawn on the canvas.
-
drawImage
Draws an image at the specified (x, y) coordinate. The image will be rescaled to fit within the width and height given as parameters.
The size of the example image will be 200x200 pixel.CodeDraw cd = new CodeDraw(); cd.drawImage(100, 100, 200, 200, new File("C:\\pathToDirectory\\filename.png")); cd.show();
Supported image formats are: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).- Parameters:
x
- The distance in pixel from the left side of the canvas to the left side of the image.y
- The distance in pixel from the top side of the canvas to the top side of the image.file
- A file that points to an image file. SeeImageIO.read(File)
-
drawImage
Draws an image at the specified (x, y) coordinate. The image will be rescaled to fit within the width and height given as parameters.
The size of the example image will be 200x200 pixel.CodeDraw cd = new CodeDraw(); cd.drawImage(100, 100, 200, 200, "C:\\pathToDirectory\\filename.png"); cd.show();
Supported image formats are: .jpg or .jpeg (JPEG), .bmp (Bitmap), .gif (Graphics Interchange Format), .png (Portable Network Graphic) and .wbmp (Wireless Application Protocol Bitmap Format).- Parameters:
x
- The distance in pixel from the left side of the canvas to the left side of the image.y
- The distance in pixel from the top side of the canvas to the top side of the image.fileName
- A fileName that points to an image file. SeeImageIO.read(File)
-
saveCanvas
Creates a copy of the current buffer (not the displayed image) and returns it as a buffered image. A BufferedImage can be saved as a file with the following code:
Fun Fact: You can copy the currently displayed canvas to your clipboard by pressing Ctrl + CCodeDraw cd = new CodeDraw(); // some drawing occurs here try { ImageIO.write( cd.saveCanvas(), "png", new File("C:\\pathToDirectory\\filename.png") ); } catch (IOException e) { throw new UncheckedIOException(e); }
.- Returns:
- The current buffer as an image object.
-
clear
public void clear()Colors the whole canvas in white. -
clear
Colors the whole canvas in the color given as a parameter.- Parameters:
color
- The color the canvas will be colored in.
-
show
public void show()Displays the drawn shapes and images on the canvas. Calling show while executing an event is generally not recommended because rendering is slow and this will slow down you program. Instead, you should call show from the main thread. -
show
public void show(int waitMilliseconds) Displays the drawn shapes and images on the canvas and then waits for the given amount of milliseconds. The copying of the buffer to the screen also takes a bit of time and that will be the minimum time it takes for this method to return.
The amount of milliseconds this method must be called with to display a certain amount of frames per second:
30 fps ~ 33ms
60 fps ~ 16ms
120 fps ~ 8ms
Calling show or sleeping while executing an event will block all other events from executing. It is generally not recommended. Instead, you should call show from the main thread.- Parameters:
waitMilliseconds
- Minimum time it takes this function to return.
-
dispose
public void dispose()Closes the frame and disposes all created resources associated with this CodeDraw instance. Terminates the process when all CodeDraw instances are closed. To prevent termination pass false to this function. Seedispose(boolean)
. -
dispose
public void dispose(boolean exit) Closes the frame and disposes all created resources associated with this CodeDraw instance.- Parameters:
exit
- When true terminates the process when all CodeDraw instances are closed. When false lets the process continue even though all CodeDraw instances have been closed.
-
toString
-