Class Canvas

All Implemented Interfaces:
ActionListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible

public class Canvas extends JPanel implements ActionListener
This class represents a basic canvas, it has some features that may seem useless... they are...

Within the canvas there are 2 types of GraphicE, fixed and mobile. fixed elements are only redrawn when the canvas is resized or when specifically told to.
Author:
Federico Vera <[email protected]>
See Also:
  • Constructor Details

    • Canvas

      public Canvas()
  • Method Details

    • setDrawableSize

      public void setDrawableSize(int xSize, int ySize)
      Sets the canvas size.
      This value doesn't affect the panel's size, just the drawable area, this means that the canvas area will be of the given size.
             +------+----+
             |      |    |
             |canvas|    |
             |      |    |
             +------+    |
             |   panel   |
             +-----------+
      Parameters:
      xSize - horizontal size
      ySize - vertical size
      Throws:
      InvalidArgumentException - if either size is less than 1
    • setCenterBounds

      public void setCenterBounds(boolean centerBounds)
      Tells the canvas to center (or not) the drawable area in the panel.
             +--------------+
             |              |
             |  +--------+  |
             |  |        |  |
             |  | canvas |  |
             |  |        |  |
             |  +--------+  |
             |     panel    |
             +--------------+
      Parameters:
      centerBounds - true to center the bounds an false otherwise
    • centerBounds

      public boolean centerBounds()
      Tells if the canvas is being centered.
      Note: the default value is false
      Returns:
      true if the canvas is being centered and false otherwise
    • useFullArea

      public boolean useFullArea()
      Tells if the drawable area will be automatically set to the canvas size.
      Note: the default value is false
      Returns:
      true if the drawable area is the canvas size, and false otherwise.
    • setUseFullArea

      public void setUseFullArea(boolean fullArea)
      Tells the canvas to set the drawable area size to the size of the canvas.
      Parameters:
      fullArea - true for the canvas and drawable area to have the same size, and false to set it manually.
    • getXSize

      public int getXSize()
      Retrieves the horizontal size of the drawable area
      Returns:
      horizontal size in px
    • getYSize

      public int getYSize()
      Retrieves the vertical size of the drawable area
      Returns:
      vertical size in px
    • sendToBottom

      public void sendToBottom(GraphicE element)
      Sends the selected GraphicE to the bottom of the canvas, so other elements will be painted on top
      Parameters:
      element - GraphicE to be send to the bottom
    • sendToFront

      public void sendToFront(GraphicE element)
      Sends the selected GraphicE to the top of the canvas, so it will be painted on top of all the other elements.
      Note: if a new element is added it will land on top of this one.
      Parameters:
      element - GraphicE to be send to the front
    • add

      public void add(GraphicE element)
      Adds a GraphicE to the canvas.
      This method doesn't check if the element is already contained on the canvas, this mean, that you can add elements twice (With no particular gain).
      Parameters:
      element - element that will be added
      See Also:
    • contains

      public boolean contains(GraphicE element)
      Tells if a GraphicE is already being painted on the canvas.
      Parameters:
      element - element to test
      Returns:
      true if the element is contained and false otherwise
    • remove

      public boolean remove(GraphicE element)
      Removes a given GraphicE from the canvas
      Parameters:
      element - element to remove
      Returns:
      true if the element was contained and false otherwise
    • removeAll

      public void removeAll()
      Overrides:
      removeAll in class Container
    • addFixed

      public void addFixed(GraphicE element)
      Adds a new fixed GraphicE to the canvas
      Parameters:
      element - element that you want to add
    • removeFixed

      public boolean removeFixed(GraphicE element)
      Removes a given fixed GraphicE from the canvas
      Parameters:
      element - element to remove
      Returns:
      true if the element was contained and false otherwise
    • setBackground

      public void setBackground(Color bg)
      Overrides:
      setBackground in class JComponent
    • getDrawableAreaPaint

      public Paint getDrawableAreaPaint()
      Retrieves the Paint used as background on the drawable area of the canvas
      Returns:
      drawable area background paint
    • setDrawableAreaPaint

      public void setDrawableAreaPaint(Paint paint)
      Sets the background Paint of the drawable area of the canvas
      Parameters:
      paint - new paint
      Throws:
      IllegalArgumentException - if the color is null
    • getDrawableBorderPaint

      public Paint getDrawableBorderPaint()
      Retrieves the Paint of the border of the drawable area
      Returns:
      drawable area border paint
    • setDrawableBorderPaint

      public void setDrawableBorderPaint(Paint paint)
      Sets the border Paint of the drawable area of the canvas
      Parameters:
      paint - new paint
      Throws:
      IllegalArgumentException - if the paint is null
    • setInvertYAxis

      public void setInvertYAxis(boolean invert)
      Tells the canvas to invert the Y axis.
      This method comes in handy when plotting functions or working with physics since it gives a more natural way of drawing. But should be ignored most of the time, since it might break other transforms.

      WARNING: this method does its magic using an AffineTransform, so this basically means that it draws in the conventional way and then invert the image, so text and images will appear inverted.
      Parameters:
      invert - true if you wish to invert the Y axis and false otherwise
    • invertYAxis

      public boolean invertYAxis()
      Tells if the canvas is inverting the Y axis
      Returns:
      true if the canvas is inverting the Y axis and false otherwise
    • moveOrigin

      public void moveOrigin(int x, int y)
      Moves the origin of coordinates to the given position, this method is very useful when working with mathematical functions since it gives a more natural way of drawing things.
      Parameters:
      x - new X coordinate of the origin
      y - new Y coordinate of the origin
    • repaintWBackground

      public void repaintWBackground()
      This method redraws the background and then repaints the canvas
    • setCenterOrigin

      public void setCenterOrigin(boolean center)
      Tells the canvas to center the origin of coordinates within the drawable area.
      Parameters:
      center - true if you want to center the origin and false otherwise
    • useAntiAliasing

      public boolean useAntiAliasing()
      Tells if the canvas is rendering using antialiasing.
      Note: the default value is true
      Returns:
      true if antialiasing is on and false otherwise
    • setUseAntiAliasing

      public void setUseAntiAliasing(boolean useAntiAliasing)
      Turns anti-aliasing on or off
      Parameters:
      useAntiAliasing - true to turn antialiasing on, and false to turn it off.
    • setAutoRepaint

      public void setAutoRepaint(boolean repaint)
      Tells the canvas to repaint itself automatically
      Parameters:
      repaint - true if the canvas should repaint itself and false otherwise
      See Also:
    • autoRepaint

      public boolean autoRepaint()
      Tells if the canvas is repainting itself automatically
      Returns:
      repaint true if the canvas is repainting itself and false otherwise
      See Also:
    • setRepaintDelay

      public void setRepaintDelay(int delay)
      The time in ms of the repaint interval.
      This will not guaranty that each is repaint is done every n ms, it will only call the repaint method every n ms.
      Parameters:
      delay - time in ms
      Throws:
      InvalidArgumentException - if the delay is less than 1
      See Also:
    • repaintDelay

      public long repaintDelay()
      Retrieves the time in ms for the canvas to repaint itself
      Returns:
      time in ms
      See Also:
    • centerOrigin

      public boolean centerOrigin()
      Tells if the canvas is centering automatically the origin of coordinates.
              not centered           centered
             +------------+       +------------+
             |*(0,0)      |       |            |
             |            |       |            |
             |            |       |     *(0,0) |
             |            |       |            |
             |   canvas   |       |   canvas   |
             +------------+       +------------+
      Returns:
      true if the origin is centered and false otherwise
    • setShowFPS

      public void setShowFPS(boolean show)
      This method tells the canvas to print the current FPS value on the screen (it will be painted in the upper left corner above all other elements).
      Note: if showFPS is set to true it will most likely have a small impact on performance (usually we draw hundreds of thousands of GraphicE, but in small applications with very high FPS the impact is quite noticeable).
      Parameters:
      show - true if you want to show the FPS and false otherwise
    • paintDrawableArea

      public void paintDrawableArea(Graphics2D g2d, boolean back)
      Paints the canvas drawable area on the given graphics
      Parameters:
      g2d - Where to paint
      back - true if you want to paint the background and false otherwise (false is needed when painting with transparent components.
      Throws:
      IllegalArgumentException - if the g2d is null
    • paintComponent

      public void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
    • actionPerformed

      public void actionPerformed(ActionEvent ae)
      Specified by:
      actionPerformed in interface ActionListener
    • inDrawingArea

      public boolean inDrawingArea(int x, int y)
      Tells if a given point is contained in the drawing area.
      Parameters:
      x - X coordinate of the point
      y - Y coordinate of the point
      Returns:
      true if the point is contained in the drawing area, and false otherwise
    • getXOff

      public int getXOff()
      Retrieves the horizontal offset of the drawing area in the canvas.
      Returns:
      horizontal offset in px
    • getYOff

      public int getYOff()
      Retrieves the vertical offset of the drawing area in the canvas.
      Returns:
      vertical offset in px