Class CanvasWindow

java.lang.Object
edu.macalester.graphics.CanvasWindow

public class CanvasWindow extends Object
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:

  1. Your main() method exits.
  2. An event listener completes.
  3. You explicitly call the CanvasWindow's draw() method.

You can use draw() to create animations using loops.

  • Constructor Summary

    Constructors
    Constructor
    Description
    CanvasWindow(String title, int windowWidth, int windowHeight)
    Opens a new window for drawing.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    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
    animate(Runnable animation)
    Call the given callback repeatedly forever, up to 60 times per second.
    void
    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
    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
    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 APIs
    void
    Adds a listener that will be notified when the use types a combination of keys that produce a character.
    void
    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
    Adds a listener that will receive an event when the mouse moves over the window with the button down.
    void
    Adds a listener that will receive an event when a key on the keyboard is pressed.
    void
    Adds a listener that will receive an event when a key on the keyboard is released.
    void
    Adds a listener that will receive an event when the mouse button goes down inside the window.
    void
    Adds a listener that will receive an event when the mouse moves over the window with the button up.
    void
    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
    For internal use.
    void
    Removes the object from being drawn
    void
    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 parameter
    void
    Changes the background color of the window.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CanvasWindow

      public CanvasWindow(String title, int windowWidth, int windowHeight)
      Opens a new window for drawing.
      Parameters:
      title - The user-visible title of the window
      windowWidth - The width of the window's content area
      windowHeight - 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

      public Point getCenter()
      Returns the center of the window’s visible content area.
    • setBackground

      public void setBackground(Paint color)
      Changes the background color of the window.
    • add

      public void add(GraphicsObject gObject)
      Adds the given graphics object to the objects that will be drawn on the canvas.
    • add

      public 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.
      Parameters:
      gObject - the graphical object to be drawn
      x - the x position of graphical object
      y - the y position of graphical object
    • remove

      public void remove(GraphicsObject gObject)
      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

      public GraphicsObject getElementAt(double x, double y)
      Returns the topmost graphical object underneath position x, y. If no such object exists it returns null.
    • getElementAt

      public GraphicsObject getElementAt(Point position)
      Returns the topmost graphical object underneath position x, y. If no such object exists it returns null.
    • screenShot

      public BufferedImage screenShot()
      Captures a screenshot of the currently drawn canvas' contents to an image.
    • screenShot

      public void screenShot(String filename)
      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 public JFrame getWindowFrame()
      Deprecated.
      Do not mix Swing and kilt-graphics APIs
    • closeWindow

      public void closeWindow()
      Closes the canvas window.
    • onMouseDown

      public void onMouseDown(MouseButtonEventHandler handler)
      Adds a listener that will receive an event when the mouse button goes down inside the window.
    • onMouseUp

      public void onMouseUp(MouseButtonEventHandler handler)
      Adds a listener that will receive an event when the mouse button goes up inside the window.
    • onClick

      public 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. not a drag).
    • onMouseMove

      public void onMouseMove(MouseMotionEventHandler handler)
      Adds a listener that will receive an event when the mouse moves over the window with the button up.
    • onDrag

      public void onDrag(MouseMotionEventHandler handler)
      Adds a listener that will receive an event when the mouse moves over the window with the button down.
    • onKeyDown

      public void onKeyDown(KeyboardEventHandler handler)
      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. See onKeyUp() for more information. If you want to take some continuous action as long as a key is pressed, consider getKeysPressed() instead.
      See Also:
    • onKeyUp

      public void onKeyUp(KeyboardEventHandler handler)
      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.keyDownKey.SHIFTmodifiers=[ModifierKey.SHIFT]
      2.keyDownKey.EQUALSmodifiers=[ModifierKey.SHIFT]
      3.keyUpKey.EQUALSmodifiers=[ModifierKey.SHIFT]
      4.characterTyped'+'
      5.keyUpKey.SHIFTmodifiers=[]
      If you want to know what character the user typed, taking keyboard layout and modifier keys into account (e.g. 'a' vs 'A'), consider onCharacterTyped() instead. If you want to take some continuous action as long as a key is pressed, consider getKeysPressed() instead.
      See Also:
    • onCharacterTyped

      public void onCharacterTyped(Consumer<Character> handler)
      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. See onKeyUp() for more information.
      See Also:
    • getKeysPressed

      public Set<Key> 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, use onKeyDown and onKeyUp() instead.
    • animate

      public void animate(Runnable animation)
      Call the given callback repeatedly forever, up to 60 times per second. Automatically draws the canvas after each time the callback runs.
    • animate

      public 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. Automatically draws the canvas after each time the callback runs.
    • performEventAction

      public void performEventAction(Runnable action)
      For internal use.