Package edu.macalester.graphics
Class CanvasWindow
java.lang.Object
edu.macalester.graphics.CanvasWindow
A window frame that can contain graphical objects.
A CanvasWindow will not immediate draw GraphicsObjects you add to it. Instead, it waits until one of the following things happens:
- Your main() method exits.
- An event listener completes.
- You explicitly call the CanvasWindow's draw() method.
You can use draw() to create animations using loops.
-
Constructor Summary
ConstructorDescriptionCanvasWindow
(String title, int windowWidth, int windowHeight) Opens a new window for drawing. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(GraphicsObject gObject) Adds the given graphics object to the objects that will be drawn on the canvas.void
add
(GraphicsObject gObject, double x, double y) Adds the graphical object to the list of objects drawn on the canvas at the position x, y.void
Call the given callback repeatedly forever, up to 60 times per second.void
animate
(DoubleConsumer animation) Call the given callback repeatedly forever, up to 60 times per second, passing the time delta since the last time it was called.void
Closes the canvas window.void
draw()
Immediately draws the currently added graphics objects to the screen.Returns the center of the window’s visible content area.getElementAt
(double x, double y) Returns the topmost graphical object underneath position x, y.getElementAt
(Point position) Returns the topmost graphical object underneath position x, y.int
Returns the height of the window's visible content area, not including any title bar and border.Returns all keys currently pressed on the keyboard.int
getWidth()
Returns the width of the window's visible content area, not including any title bar and border.Deprecated.Do not mix Swing and kilt-graphics APIsvoid
onCharacterTyped
(Consumer<Character> handler) Adds a listener that will be notified when the use types a combination of keys that produce a character.void
onClick
(MouseButtonEventHandler handler) Adds a listener that will receive an event when the mouse button goes down and then up inside the window without moving significantly (i.e.void
onDrag
(MouseMotionEventHandler handler) Adds a listener that will receive an event when the mouse moves over the window with the button down.void
onKeyDown
(KeyboardEventHandler handler) Adds a listener that will receive an event when a key on the keyboard is pressed.void
onKeyUp
(KeyboardEventHandler handler) Adds a listener that will receive an event when a key on the keyboard is released.void
onMouseDown
(MouseButtonEventHandler handler) Adds a listener that will receive an event when the mouse button goes down inside the window.void
onMouseMove
(MouseMotionEventHandler handler) Adds a listener that will receive an event when the mouse moves over the window with the button up.void
onMouseUp
(MouseButtonEventHandler handler) Adds a listener that will receive an event when the mouse button goes up inside the window.void
pause
(double milliseconds) Pauses the program for the given time in milliseconds.void
pause
(long milliseconds) Pauses the program for the given time in milliseconds.void
performEventAction
(Runnable action) For internal use.void
remove
(GraphicsObject gObject) Removes the object from being drawnvoid
Removes all of the objects currently drawn on the canvas.Captures a screenshot of the currently drawn canvas' contents to an image.void
screenShot
(String filename) Saves a screenshot of the currently drawn canvas' contents to the filename specified as a parametervoid
setBackground
(Paint color) Changes the background color of the window.
-
Constructor Details
-
CanvasWindow
Opens a new window for drawing.- Parameters:
title
- The user-visible title of the windowwindowWidth
- The width of the window's content areawindowHeight
- The height of the window's content area
-
-
Method Details
-
getWidth
public int getWidth()Returns the width of the window's visible content area, not including any title bar and border. -
getHeight
public int getHeight()Returns the height of the window's visible content area, not including any title bar and border. -
getCenter
Returns the center of the window’s visible content area. -
setBackground
Changes the background color of the window. -
add
Adds the given graphics object to the objects that will be drawn on the canvas. -
add
Adds the graphical object to the list of objects drawn on the canvas at the position x, y.- Parameters:
gObject
- the graphical object to be drawnx
- the x position of graphical objecty
- the y position of graphical object
-
remove
Removes the object from being drawn- Throws:
NoSuchElementException
- if gObject has not been added to the canvas
-
removeAll
public void removeAll()Removes all of the objects currently drawn on the canvas. Does not change the background color. -
draw
public void draw()Immediately draws the currently added graphics objects to the screen. This method is synchronous: it waits for the drawing to complete before proceeding. -
pause
public void pause(long milliseconds) Pauses the program for the given time in milliseconds. -
pause
public void pause(double milliseconds) Pauses the program for the given time in milliseconds. -
getElementAt
Returns the topmost graphical object underneath position x, y. If no such object exists it returns null. -
getElementAt
Returns the topmost graphical object underneath position x, y. If no such object exists it returns null. -
screenShot
Captures a screenshot of the currently drawn canvas' contents to an image. -
screenShot
Saves a screenshot of the currently drawn canvas' contents to the filename specified as a parameter- Parameters:
filename
- The filename for where you would like to save.
-
getWindowFrame
Deprecated.Do not mix Swing and kilt-graphics APIs -
closeWindow
public void closeWindow()Closes the canvas window. -
onMouseDown
Adds a listener that will receive an event when the mouse button goes down inside the window. -
onMouseUp
Adds a listener that will receive an event when the mouse button goes up inside the window. -
onClick
Adds a listener that will receive an event when the mouse button goes down and then up inside the window without moving significantly (i.e. not a drag). -
onMouseMove
Adds a listener that will receive an event when the mouse moves over the window with the button up. -
onDrag
Adds a listener that will receive an event when the mouse moves over the window with the button down. -
onKeyDown
Adds a listener that will receive an event when a key on the keyboard is pressed. Note that this reports actual keys on the keyboard, not which characters they produce. SeeonKeyUp()
for more information. If you want to take some continuous action as long as a key is pressed, considergetKeysPressed()
instead. -
onKeyUp
Adds a listener that will receive an event when a key on the keyboard is released. Note that this reports actual keys on the keyboard, not any characters they produce. For example, if the user types a plus sign, you will receive the following events:Example key event sequence 1. keyDown Key.SHIFT modifiers=[ModifierKey.SHIFT] 2. keyDown Key.EQUALS modifiers=[ModifierKey.SHIFT] 3. keyUp Key.EQUALS modifiers=[ModifierKey.SHIFT] 4. characterTyped '+'
5. keyUp Key.SHIFT modifiers=[] onCharacterTyped()
instead. If you want to take some continuous action as long as a key is pressed, considergetKeysPressed()
instead. -
onCharacterTyped
Adds a listener that will be notified when the use types a combination of keys that produce a character. Note that this only reports key combinations that produce characters; it does not report special keys such as arrow keys, backspace, modifiers, etc. SeeonKeyUp()
for more information. -
getKeysPressed
Returns all keys currently pressed on the keyboard. You can poll this repeatedly (e.g. in an animate() callback) to continuously take some action as long as a key is held down. If instead you want to do something only at the moment a key goes down or comes back up, useonKeyDown
andonKeyUp()
instead. -
animate
Call the given callback repeatedly forever, up to 60 times per second. Automatically draws the canvas after each time the callback runs. -
animate
Call the given callback repeatedly forever, up to 60 times per second, passing the time delta since the last time it was called. Automatically draws the canvas after each time the callback runs. -
performEventAction
For internal use.
-