Class Canvas


  • public class Canvas
    extends java.lang.Object
    Represents the resolution of the application graphics independently from the device's screen resolution. The canvas is initially created with a preferred width and height. When the screen or window is resized, the canvas will also be resized while keeping its original aspect ratio.

    Having a consistent canvas size across different devices ensures a consistent user experience in situations where the same application is used across a variety of devices with different screen sizes. This class is then used to translate between the two coordinate systems.

    Alternatively, a flexible canvas can be created. In this case, the canvas follows the screen resolution without considering the requested width/height or aspect ratio.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static Canvas fixed​(int preferredWidth, int preferredHeight)
      Creates a canvas with fixed dimensions.
      static Canvas flexible​(int initialWidth, int initialHeight)
      Creates a canvas that resizes itself to match the screen size, always keeping a zoom level of 1.0.
      Rect getBounds()  
      Point getCenter()  
      int getHeight()  
      int getWidth()  
      float getZoomLevel()  
      void offsetScreen​(int offsetX, int offsetY)
      Offset the canvas position on the screen.
      void resizeScreen​(int screenWidth, int screenHeight)
      Sets the screen dimensions to the specified values.
      static Canvas responsive​(int initialWidth, int initialHeight, float minZoom, float maxZoom)
      Creates a canvas that resizes itself to match the screen size, as long as the zoom level stays within the specified bounds.
      float toCanvasX​(int screenX)  
      float toCanvasY​(int screenY)  
      float toScreenX​(float canvasX)  
      float toScreenY​(float canvasY)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • resizeScreen

        public void resizeScreen​(int screenWidth,
                                 int screenHeight)
        Sets the screen dimensions to the specified values. This method should be called by the renderer when the application window is first created, and whenever the window is resized.
      • offsetScreen

        public void offsetScreen​(int offsetX,
                                 int offsetY)
        Offset the canvas position on the screen. This is mainly needed for when the screen contains system UI such as a status bar or window title where no application graphics can be displayed.
      • getWidth

        public int getWidth()
      • getHeight

        public int getHeight()
      • getBounds

        public Rect getBounds()
      • getCenter

        public Point getCenter()
      • getZoomLevel

        public float getZoomLevel()
      • toCanvasX

        public float toCanvasX​(int screenX)
      • toCanvasY

        public float toCanvasY​(int screenY)
      • toScreenX

        public float toScreenX​(float canvasX)
      • toScreenY

        public float toScreenY​(float canvasY)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • fixed

        public static Canvas fixed​(int preferredWidth,
                                   int preferredHeight)
        Creates a canvas with fixed dimensions. The canvas will be scaled to match the current screen size. Note that it might not be possible to retain the preferred canvas dimensions in all cases, as the screen might have a different aspect ratio than the canvas.
      • flexible

        public static Canvas flexible​(int initialWidth,
                                      int initialHeight)
        Creates a canvas that resizes itself to match the screen size, always keeping a zoom level of 1.0. The provided width and height are only used to initialize the canvas but will not be used afterwards.
      • responsive

        public static Canvas responsive​(int initialWidth,
                                        int initialHeight,
                                        float minZoom,
                                        float maxZoom)
        Creates a canvas that resizes itself to match the screen size, as long as the zoom level stays within the specified bounds.