Class Canvas


  • public class Canvas
    extends java.lang.Object
    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)
      Initializes the canvas
      Canvas​(int width, int height, java.lang.String title, CanvasOptions options)
      Initializes the canvas
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static Canvas getGlobalInstance()
      Get the global instance of the Canvas (used internally to access the Canvas)
      int getHeight()
      Get the height of the canvas
      java.awt.Point getMousePos()
      Get the mouse position in the canvas.
      int getWidth()
      Get the width of the canvas
      void render()  
      void setBackgroundColor​(java.awt.Color color)
      Sets the background color of the canvas
      void setTitle​(java.lang.String title)
      Set the title of this canvas
      void sleep()
      Sleeps until all animations finish.
      void sleep​(double seconds)
      Sleeps for the specified amount of seconds
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • globalInstance

        public static Canvas globalInstance
      • startSize

        public final java.awt.Dimension startSize
        the initial size of the Canvas
      • translation

        public final java.awt.geom.Point2D.Float translation
      • elements

        public final java.util.List<Drawable<?>> elements
        the elements that are currently on the canvas
      • animations

        public final java.util.List<Animation> animations
        the list of animations that are currently running
      • panel

        public final CanvasPanel panel
        A CanvasComponent, which handles all the rendering n stuff
      • animationSync

        protected final java.lang.Object animationSync
        Sync with animations: Notifies on animation finish
      • frameSync

        protected final java.lang.Object frameSync
        Sync with frame: Notifies on end of frame
      • drawableSync

        public static final java.lang.Object drawableSync
        Sync with drawables: Use when modifying a drawable
      • frame

        public int frame
        The current frame
      • renderLifecycles

        public java.util.List<RenderLifecycle> renderLifecycles
        The RenderLifecycle: allows you to write code to run before and after a frame is rendered
    • 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)
        Initializes the canvas
        Parameters:
        width - the width of the canvas
        height - the height of the canvas
        title - the title of the canvas
      • Canvas

        public Canvas​(int width,
                      int height,
                      java.lang.String title,
                      CanvasOptions options)
        Initializes the canvas
        Parameters:
        width - the width of the canvas
        height - the height of the canvas
        title - the title of the canvas
        options - options for the canvas
    • Method Detail

      • getGlobalInstance

        public static Canvas getGlobalInstance()
        Get the global instance of the Canvas (used internally to access the Canvas)
        Returns:
        The global instance of Canvas
      • 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
      • 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.
        
         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 seconds
        
         Circle 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