Package codedraw
Class CodeDraw
java.lang.Object
codedraw.CodeDraw
- All Implemented Interfaces:
AutoCloseable
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.
The source code can be found in the CodeDraw repository.
If you are unfamiliar with graphical output and/or want more details you can read the Introduction to CodeDraw.
For the JavaDoc visit CodeDrawJavaDoc.
Here is an example to get you started:
The source code can be found in the CodeDraw repository.
If you are unfamiliar with graphical output and/or want more details you can read the Introduction to CodeDraw.
For the JavaDoc visit CodeDrawJavaDoc.
Here is 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 cd.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
close()
Closes the frame and disposes all created resources associated with this CodeDraw instance.void
close
(boolean terminateProcess) Closes the frame and disposes all created resources associated with this CodeDraw instance.Creates a copy of the current buffer (not the displayed image) and returns it as a buffered image.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 Bézier 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 Bézier curve.void
drawEllipse
(double x, double y, double horizontalRadius, double verticalRadius) Draws the outline of an ellipse.void
drawImage
(double x, double y, double width, double height, CodeDrawImage image) Draws an image at the specified (x, y) coordinate.void
drawImage
(double x, double y, double width, double height, CodeDrawImage image, Interpolation interpolation) Draws an image at the specified (x, y) coordinate.void
drawImage
(double x, double y, CodeDrawImage image) 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
boolean
Defines whether draw text, drawn shapes and filled shapes are anti-aliased.boolean
When InstantDraw is disabled CodeDraw will only draw shapes to the window once show is called.onKeyDown
(EventHandler<KeyDownEventArgs> handler) Trigger exactly once when a key is pressed down.onKeyPress
(EventHandler<KeyPressEventArgs> handler) Triggers continuously while a key is being held down.onKeyUp
(EventHandler<KeyUpEventArgs> handler) Trigger exactly once when a key is released.onMouseClick
(EventHandler<MouseClickEventArgs> handler) Triggers once when a mouse button is pressed down and quickly released again.onMouseDown
(EventHandler<MouseDownEventArgs> handler) Triggers exactly once when a mouse button is pressed down.onMouseEnter
(EventHandler<MouseEnterEventArgs> handler) Triggers when the mouse enters the canvas.onMouseLeave
(EventHandler<MouseLeaveEventArgs> handler) Triggers when the mouse leaves the canvas.onMouseMove
(EventHandler<MouseMoveEventArgs> handler) Triggers continuously while the mouse is being moved.onMouseUp
(EventHandler<MouseUpEventArgs> handler) Triggers when a mouse button is released.onMouseWheel
(EventHandler<MouseWheelEventArgs> handler) Triggers each time the mouse wheel is turned.onWindowClose
(EventHandler<WindowCloseEventArgs> handler) Triggers exactly once when the user closes the window orclose()
is called.onWindowMove
(EventHandler<WindowMoveEventArgs> handler) Triggers every time the CodeDraw window is moved.void
setAlwaysOnTop
(boolean isAlwaysOnTop) When set to true this CodeDraw window will always be displayed on top of other windows.void
setAntiAliased
(boolean isAntiAliased) Defines whether drawn text, drawn shapes and filled shapes are anti-aliased.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
setInstantDraw
(boolean isInstantDraw) When InstantDraw is disabled CodeDraw will only draw shapes to the window once show is called.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 all the drawn and filled shapes that have been drawn until now.void
show
(long waitMilliseconds) Displays all the drawn and filled shapes that have been drawn until now 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
-
isInstantDraw
public boolean isInstantDraw()When InstantDraw is disabled CodeDraw will only draw shapes to the window once show is called. When InstantDraw is enabled CodeDraw will immediately draw all shapes to the canvas. ImmediateDraw is disabled per default.- Returns:
- whether InstantDraw is enabled.
-
setInstantDraw
public void setInstantDraw(boolean isInstantDraw) When InstantDraw is disabled CodeDraw will only draw shapes to the window once show is called. When InstantDraw is enabled CodeDraw will immediately draw all shapes to the canvas. InstantDraw is disabled per default.- Parameters:
isInstantDraw
- defines whether InstantDraw is enabled.
-
isAlwaysOnTop
public boolean isAlwaysOnTop()- Returns:
- whether the CodeDraw window is always displayed on top of other windows.
-
setAlwaysOnTop
public void setAlwaysOnTop(boolean isAlwaysOnTop) When set to true this CodeDraw window will always be displayed on top of other windows. When set to false this CodeDraw window will disappear behind other windows when CodeDraw loses focus.- Parameters:
isAlwaysOnTop
- defines whether this CodeDraw window is displayed on top of other windows.
-
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. Must be greater than zero.- Returns:
- the lineWidth of this CodeDraw window.
-
setLineWidth
public void setLineWidth(double lineWidth) Defines the width or thickness of drawn shapes and lines. Must be greater than zero.- Parameters:
lineWidth
- Sets the lineWidth of this CodeDraw window.
-
getTextFormat
Defines the styling of drawn text. See alsodrawText(double, double, String)
on how the styling is applied.- Returns:
- the text formatting options of this CodeDraw window.
-
setTextFormat
Defines the styling of drawn text. See alsodrawText(double, double, String)
on how the styling is applied.- 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.
-
setAntiAliased
public void setAntiAliased(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. SeeCorner
for details.- Returns:
- the corner style of this CodeDraw window.
-
setCorner
Defines how the corners of drawn shapes should look. SeeCorner
for details.- 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. UsePalette
for a large selection of colors.- Parameters:
color
- Sets the drawing color of this CodeDraw window.
-
onMouseClick
Triggers once when a mouse button is pressed down and quickly released again. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onMouseMove
Triggers continuously while the mouse is being moved. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onMouseDown
Triggers exactly once when a mouse button is pressed down. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onMouseUp
Triggers when a mouse button is released. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onMouseEnter
Triggers when the mouse enters the canvas. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onMouseLeave
Triggers when the mouse leaves the canvas. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onMouseWheel
Triggers each time the mouse wheel is turned. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onKeyDown
Trigger exactly once when a key is pressed down. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onKeyUp
Trigger exactly once when a key is released. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onKeyPress
Triggers continuously while a key is being held down. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onWindowMove
Triggers every time the CodeDraw window is moved. If you have difficulty with this method try theEventScanner
.- Parameters:
handler
- A lambda or function reference.
-
onWindowClose
Triggers exactly once when the user closes the window orclose()
is called. If you have difficulty with this method try theEventScanner
.- 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. The line width can be changed withsetLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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 Bézier 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. The line width can be changed withsetLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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 Bézier 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. The line width can be changed withsetLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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. The line width can be changed withsetLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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. The line width can be changed withsetLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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. The line width can be changed withsetLineWidth(double)
.- 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. The line width can be changed withsetLineWidth(double)
.- 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 3 o'clock position offset by the startRadians parameter. The total length of the arc is defined by the sweepRadians parameter. The line width can be changed withsetLineWidth(double)
.
You can useMath.toRadians(double)
to specify the angle in degrees.
The line width can be changed withcd.drawArc(200, 200, 50, Math.toRadians(90), Math.toRadians(180));
setLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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 3 o'clock going clockwise.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 3 o'clock position offset by the startRadians parameter. The total length of the arc is defined by the sweepRadians parameter. The line width can be changed withsetLineWidth(double)
.
You can useMath.toRadians(double)
to specify the angle in degrees.
The line width can be changed withcd.drawArc(200, 200, 50, 50, Math.toRadians(90), Math.toRadians(180));
setLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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 3 o'clock going clockwise.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 3 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter. The line width can be changed withsetLineWidth(double)
.
You can useMath.toRadians(double)
to specify the angle in degrees.
The line width can be changed withcd.drawPie(200, 200, 50, Math.toRadians(90), Math.toRadians(180));
setLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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 3 o'clock going clockwise.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 3 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter. The line width can be changed withsetLineWidth(double)
.
You can useMath.toRadians(double)
to specify the angle in degrees.
The line width can be changed withcd.drawPie(200, 200, 50, 50, Math.toRadians(90), Math.toRadians(180));
setLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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 3 o'clock going clockwise.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 3 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.
You can useMath.toRadians(double)
to specify the angle in degrees.cd.fillPie(200, 200, 50, Math.toRadians(90), Math.toRadians(180));
- 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 3 o'clock going clockwise.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 3 o'clock position offset by the startRadians parameter. The total length of the pie is defined by the sweepRadians parameter.
You can useMath.toRadians(double)
to specify the angle in degrees.cd.fillPie(200, 200, 50, 50, Math.toRadians(90), Math.toRadians(180));
- 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 3 o'clock going clockwise.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. The line width can be changed withsetLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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.
The line width can be changed withcd.drawPolygon( 200, 100, 100, 200, 300, 200 );
setLineWidth(double)
. The corners can be changed withsetCorner(Corner)
. For details see theCorner
class.- 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. If you pass only two arguments to fillPolygon nothing will be drawn.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.- 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
- Any image.
-
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.- 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.width
- The width of the image on the canvas.height
- The height of the image on the canvas.image
- Any image.
-
drawImage
public void drawImage(double x, double y, double width, double height, CodeDrawImage image, Interpolation interpolation) Draws an image at the specified (x, y) coordinate. The image will be rescaled to fit within the width and height given as parameters.- 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.width
- The width of the image on the canvas.height
- The height of the image on the canvas.image
- Any image.interpolation
- Defines the way the images is interpolated when scaled. SeeInterpolation
.
-
copyCanvas
Creates a copy of the current buffer (not the displayed image) and returns it as a buffered image. A BufferedImage can be saved to a file with the following code:
Fun Fact: You can copy the currently displayed canvas to your clipboard by pressing Ctrl + C.CodeDraw cd = new CodeDraw(); // some drawing occurs here CodeDrawImage.saveAsPNG(cd.copyCanvas(), "/pathToDirectory/filename.png");
- 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 all the drawn and filled shapes that have been drawn until now. Showing the drawn elements in the CodeDraw window is slow. Calling show frequently will slow down your program. -
show
public void show(long waitMilliseconds) Displays all the drawn and filled shapes that have been drawn until now and then waits for the given amount of milliseconds. Since showing the drawn elements in the CodeDraw window is slow, CodeDraw will subtract the time it takes to show from waitMilliseconds. 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- Parameters:
waitMilliseconds
- Minimum time it takes this function to return.
-
close
public void close()Closes the frame and disposes all created resources associated with this CodeDraw instance.- Specified by:
close
in interfaceAutoCloseable
-
close
public void close(boolean terminateProcess) Closes the frame and disposes all created resources associated with this CodeDraw instance.- Parameters:
terminateProcess
- 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
-