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
ConstructorsConstructorDescriptionCanvasWindow(String title, int windowWidth, int windowHeight) Opens a new window for drawing. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(GraphicsObject gObject) Adds the given graphics object to the objects that will be drawn on the canvas.voidadd(GraphicsObject gObject, double x, double y) Adds the graphical object to the list of objects drawn on the canvas at the position x, y.voidCall the given callback repeatedly forever, up to 60 times per second.voidanimate(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.voidCloses the canvas window.voiddraw()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.intReturns the height of the window's visible content area, not including any title bar and border.Returns all keys currently pressed on the keyboard.intgetWidth()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 APIsvoidonCharacterTyped(Consumer<Character> handler) Adds a listener that will be notified when the use types a combination of keys that produce a character.voidonClick(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.voidonDrag(MouseMotionEventHandler handler) Adds a listener that will receive an event when the mouse moves over the window with the button down.voidonKeyDown(KeyboardEventHandler handler) Adds a listener that will receive an event when a key on the keyboard is pressed.voidonKeyUp(KeyboardEventHandler handler) Adds a listener that will receive an event when a key on the keyboard is released.voidonMouseDown(MouseButtonEventHandler handler) Adds a listener that will receive an event when the mouse button goes down inside the window.voidonMouseMove(MouseMotionEventHandler handler) Adds a listener that will receive an event when the mouse moves over the window with the button up.voidonMouseUp(MouseButtonEventHandler handler) Adds a listener that will receive an event when the mouse button goes up inside the window.voidpause(double milliseconds) Pauses the program for the given time in milliseconds.voidpause(long milliseconds) Pauses the program for the given time in milliseconds.voidperformEventAction(Runnable action) For internal use.voidremove(GraphicsObject gObject) Removes the object from being drawnvoidRemoves all of the objects currently drawn on the canvas.Captures a screenshot of the currently drawn canvas' contents to an image.voidscreenShot(String filename) Saves a screenshot of the currently drawn canvas' contents to the filename specified as a parametervoidsetBackground(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:
If you want to know what character the user typed, taking keyboard layout and modifier keys into account (e.g. 'a' vs 'A'), considerExample 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, useonKeyDownandonKeyUp()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.
-