Package paintingcanvas.canvas
Class Canvas
- java.lang.Object
-
- paintingcanvas.canvas.Canvas
-
- All Implemented Interfaces:
Interactable
public class Canvas extends java.lang.Object implements Interactable
The internal canvas component that is used to draw to the screen
-
-
Constructor Summary
Constructors Constructor Description Canvas()
Initializes the canvas with a default size of 900x600 and a title of "Canvas"Canvas(int width, int height, java.lang.String title)
Initialize the canvasCanvas(int width, int height, java.lang.String title, CanvasOptions options)
Initialize the canvas with more options
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
atomic(java.lang.Runnable r)
Runs the specified code all on the same frame.boolean
clicked()
Check if the element has been clicked by the mouse.int
getFrame()
Get the current frame countint
getHeight()
Get the height of the canvasjava.awt.Point
getMousePos()
Get the mouse position in the canvas.CanvasOptions
getOptions()
Get the current configured optionsint
getWidth()
Get the width of the canvasboolean
hovered()
Check if the element is currently being hovered over by the mouse.boolean
intersects(java.awt.Point pos)
Check if the point intersects with the element.boolean
keyDown(char c)
Check if a key is currently held down using the character representation of the key.boolean
keyDown(int keyCode)
Check if a key is currently held down using the key code, the definitions of which can be found inKeyEvent
.void
render()
void
setBackgroundColor(java.awt.Color color)
Sets the background color of the canvasvoid
setTitle(java.lang.String title)
Set the title of this canvasvoid
sleep()
Sleeps until all animations finish, or, if there are no animations, sleep until the next frame.void
sleep(double seconds)
Sleeps for the specified amount of seconds
-
-
-
Constructor Detail
-
Canvas
public Canvas()
Initializes the canvas with a default size of 900x600 and a title of "Canvas"
-
Canvas
public Canvas(int width, int height, java.lang.String title)
Initialize the canvas- Parameters:
width
- the width of the canvasheight
- the height of the canvastitle
- the title of the canvas
-
Canvas
public Canvas(int width, int height, java.lang.String title, CanvasOptions options)
Initialize the canvas with more options- Parameters:
width
- the width of the canvasheight
- the height of the canvastitle
- the title of the canvasoptions
- options for the canvas
-
-
Method Detail
-
setBackgroundColor
public void setBackgroundColor(java.awt.Color color)
Sets the background color of the canvas- Parameters:
color
- the new background color
-
getWidth
public int getWidth()
Get the width of the canvas- Returns:
- The width of the canvas
-
getHeight
public int getHeight()
Get the height of the canvas- Returns:
- The height of the canvas
-
getFrame
public int getFrame()
Get the current frame count
-
getOptions
public CanvasOptions getOptions()
Get the current configured options
-
setTitle
public void setTitle(java.lang.String title)
Set the title of this canvas- Parameters:
title
- the new title
-
sleep
public void sleep()
Sleeps until all animations finish, or, if there are no animations, sleep until the next frame.Circle c = new Circle(200, 200, 50); c.animate().with(Animation.colorTo(Color.RED), 2) .with(Animation.moveBy(100, 0), 2); canvas.sleep() c.setColor(Color.BLUE);
-
sleep
public void sleep(double seconds)
Sleeps for the specified amount of secondsCircle c = new Circle(200, 200, 50); canvas.sleep(1); // Sleeps for 1 second c.setColor(Color.RED);
- Parameters:
seconds
- The number of seconds to sleep for
-
render
public void render()
-
getMousePos
public java.awt.Point getMousePos()
Get the mouse position in the canvas.Point mousePos = canvas.getMousePos(); if (mousePos != null) { int x = mousePos.x; int y = mousePos.y; }
- Returns:
- The mouse position, or
null
if the mouse is not hovering over the canvas
-
atomic
public void atomic(java.lang.Runnable r)
Runs the specified code all on the same frame. Without it, because the rendering is decoupled from your code, many operations can be randomly split across frames.Canvas canvas = new Canvas(); Polygon[] shapes = ...; canvas.atomic(() -> { for (Polygon c : shapes) c.move(10, 0); });
- Parameters:
r
- The code to run
-
intersects
public boolean intersects(java.awt.Point pos)
Description copied from interface:Interactable
Check if the point intersects with the element.- Specified by:
intersects
in interfaceInteractable
- Returns:
- true if the point intersects with the element, false otherwise.
-
hovered
public boolean hovered()
Description copied from interface:Interactable
Check if the element is currently being hovered over by the mouse.- Specified by:
hovered
in interfaceInteractable
- Returns:
- true if the element is hovered, false otherwise.
-
clicked
public boolean clicked()
Description copied from interface:Interactable
Check if the element has been clicked by the mouse.- Specified by:
clicked
in interfaceInteractable
- Returns:
- true if the element is clicked, false otherwise.
-
keyDown
public boolean keyDown(int keyCode)
Check if a key is currently held down using the key code, the definitions of which can be found in
KeyEvent
.canvas.keyDown(KeyEvent.VK_A); // true if the 'A' key is currently held down
- Parameters:
keyCode
- the key code to check (e.g.KeyEvent.VK_A
for the 'A' key)- Returns:
- true if the key is currently held down, false otherwise
-
keyDown
public boolean keyDown(char c)
Check if a key is currently held down using the character representation of the key.
canvas.keyDown('A'); // true if the 'A' key is currently held down
- Parameters:
c
- the character representation of the key (e.g. 'A' for the 'A' key)- Returns:
- true if the key is currently held down, false otherwise
-
-