Class Draw

java.lang.Object
buildengine.engine.graphics.Draw

public class Draw extends Object
Utility class for java graphics drawing.

This class has the unit object, used to translate and scale the Draws. The screen therefore consists of units. The amount of units depends on the aspect ratio, and the standard unit product. Every time you use methods from this class it expects unit coordinates, with the exception of the convert() methods, witch are used to convert units to pixels on the screen, and vise versa.

The class also contains a Transform, to apply to the drawing. Use the methods Draw.translate(), Draw.scale() and Draw.rotate() before drawing to apply transformations.

  • Field Details

    • UNIT

      public static final float UNIT
      See Also:
    • ASPECT_RATIO

      public static final Vector2f ASPECT_RATIO
    • DRAW_POINT_SIZE_MUL

      public static final float DRAW_POINT_SIZE_MUL
      See Also:
    • DEFAULT_COLOR

      public static final Color DEFAULT_COLOR
  • Method Details

    • prepare

      public static void prepare(Graphics2D graphics2D)
      Sets a new Graphics2D object for the Draw to use. Always call this first in the render method of the Draw.
      Parameters:
      graphics2D - up-to-date Graphics2D object.
      See Also:
      • g
    • applyTransform

      public static void applyTransform(Transform transform)
      Transform the Draw. After altering applies to all Draw calls.
      Parameters:
      transform - the transform to apply
    • translate

      public static void translate(Vector2f vector2f)
      Translate the transform of Draw. After altering applies to all Draw calls.
      Parameters:
      vector2f - translation position in units
    • scale

      public static void scale(Vector2f size)
      Scale the transform of Draw. After altering applies to all Draw calls.
      Parameters:
      size - translation scale in units
    • rotate

      public static void rotate(float theta)
      Rotate the transform of Draw. After altering applies to all Draw calls.
      Parameters:
      theta - the rotation in radians
    • resetTransform

      public static void resetTransform()
      Resets the transform of draw. After calling applies to all Draw calls.
    • drawImage

      public static void drawImage(BufferedImage image, Vector2f position, float width, float height)
      DrawImage method. This is the main method for rendering graphics/images to the screen. This method uses java.awt.Graphics2D.
      Parameters:
      image - image to draw.
      position - the position to draw in units.
      width - the width stretch in units.
      height - the height stretch in units.
    • drawImage

      public static void drawImage(BufferedImage image, Vector2f position, float width, float height, double theta, Vector2f center)
      DrawImage method. This is the main method for rendering graphics/images to the screen. This method uses java.awt.Graphics2D.
      Parameters:
      image - image to draw.
      position - the position to draw in units.
      width - the width stretch in units.
      height - the height stretch in units.
      theta - the rotation
      center - center, around witch the image should be rotated.
    • drawString

      public static void drawString(String string, Vector2f position)
      Draws a specified string to a specified position on the screen using the specified color, using the default font. This method uses java.awt.Graphics2D.
      Parameters:
      string - the string to draw.
      position - position of the string in units.
    • drawString

      public static void drawString(String string, Vector2f position, Color color)
      Draws a specified string to a specified position on the screen using the specified color, using the default font. This method uses java.awt.Graphics2D.
      Parameters:
      string - the string to draw.
      position - position of the string in units.
      color - color to draw in.
    • drawString

      public static void drawString(String string, Vector2f position, Color color, Font font)
      Draws a string to the screen, with specified position, color and font. This method uses java.awt.Graphics2D.
      Parameters:
      string - the string to draw.
      position - position of the string in units.
      color - color to draw in.
      font - font to use.
    • drawCenteredString

      public static void drawCenteredString(String string, Vector2f position)
      Draws a centered string to the screen. This method uses java.awt.Graphics2D.
      Parameters:
      string - the string to draw.
      position - position of the string in units(CENTERED).
      See Also:
    • drawCenteredString

      public static void drawCenteredString(String string, Vector2f position, Color color)
      Draws a centered string to the screen. This method uses java.awt.Graphics2D.
      Parameters:
      string - the string to draw.
      position - position of the string in units(CENTERED).
      color - color to draw in.
      See Also:
    • drawCenteredString

      public static void drawCenteredString(String string, Vector2f position, Color color, Font font)
      Draws a centered string to the screen. This method uses java.awt.Graphics2D.
      Parameters:
      string - the string to draw.
      position - position of the string in units(CENTERED).
      color - color to draw in.
      font - font to use.
      See Also:
    • drawRect

      public static void drawRect(Rectangle rectangle, boolean fill)
      Draws a rectangular shape to the screen. This method uses buildengine.math.shape.Rectangle.
      Parameters:
      rectangle - The rectangle object to draw.
      fill - Whether the rectangle should be filled or not.
    • drawRect

      public static void drawRect(Rectangle rectangle, Color color, boolean fill)
      Draws a rectangular shape to the screen. This method uses buildengine.math.shape.Rectangle.
      Parameters:
      rectangle - The rectangle object to draw.
      color - The color of the rectangle.
      fill - Whether the rectangle should be filled or not.
    • drawRect

      public static void drawRect(Rectangle rectangle, Vector2f position, Color color, boolean fill)
      Draws a rectangular shape to the screen. This method uses buildengine.math.shape.Rectangle.
      Parameters:
      rectangle - The rectangle object to draw.
      position - Position to draw the rectangle.
      color - The color of the rectangle.
      fill - Whether the rectangle should be filled or not.
    • drawRect

      public static void drawRect(Vector2f position, float width, float height, boolean fill)
      Draws a rectangular shape to the screen. This method uses java.awt.Graphics2D.
      Parameters:
      position - Position in units.
      width - Width in units.
      height - Height in units.
      fill - Whether the rectangle should be filled or not.
    • drawRect

      public static void drawRect(Vector2f position, float width, float height, Color color, boolean fill)
      Draws a rectangular shape to the screen. This method uses java.awt.Graphics2D.
      Parameters:
      position - Position in units.
      width - Width in units.
      height - Height in units.
      color - Color of the rectangle.
      fill - Whether the rectangle should be filled or not.
    • drawRect

      public static void drawRect(Vector2f position, float width, float height, boolean fill, double theta, Vector2f center)
      Draws a rectangular shape to the screen. This method uses java.awt.Graphics2D.
      Parameters:
      position - the position to draw in units.
      width - the width stretch in units.
      height - the height stretch in units.
      fill - whether the rectangle should be filled or not.
      theta - the rotation
      center - if you're working with rotations, different center point
    • drawRect

      public static void drawRect(Vector2f position, float width, float height, Color color, boolean fill, double theta, Vector2f center)
      Draws a rectangular shape to the screen. This method uses java.awt.Graphics2D.
      Parameters:
      position - the position to draw in units.
      width - the width stretch in units.
      height - the height stretch in units.
      color - the color of the rectangle.
      fill - whether the rectangle should be filled or not.
      theta - the rotation
      center - if you're working with rotations, different center point
    • drawLine

      public static void drawLine(Vector2f a, Vector2f b)
      Draws a line to the screen, between two specified points. This method uses java.awt.Graphics2D.
      Parameters:
      a - starting point of the line.
      b - end point of the line.
    • drawLine

      public static void drawLine(Vector2f a, Vector2f b, Color color)
      Draws a line to the screen, between two specified points. This method uses java.awt.Graphics2D.
      Parameters:
      a - starting point of the line.
      b - end point of the line.
      color - color of the line
    • drawLine

      public static void drawLine(Line line, Color color)
      Draws a line to the screen, between two specified points. This method uses java.awt.Graphics2D.
      Parameters:
      line - Line to draw.
      color - color of the line
    • drawPoint

      public static void drawPoint(Vector2f point)
      Draws a point to the screen, on a specified point.
      Parameters:
      point - The point to draw
    • drawPoint

      public static void drawPoint(Vector2f point, Color color)
      Draws a point to the screen, on a specified point.
      Parameters:
      point - The point to draw
      color - The color to draw it in.
    • resize

      public static void resize(Dimension newSize)
      Recalculates the unit relative to the #size and the aspectRatio.
    • convertPositionToPixel

      public static Vector2i convertPositionToPixel(Vector2f vector2F)
      Converts unit coordinates to screen pixel coordinates and applies the draw transform.
      Parameters:
      vector2F - unit coordinate
      Returns:
      pixel coordinate
    • convertSizeToPixel

      public static Vector2i convertSizeToPixel(float width, float height)
      Converts unit sizes to screen pixel coordinates and applies the draw transform.
      Parameters:
      width - Width in units
      height - Height in units
      Returns:
      Pixel coordinate
    • convertToUnit

      public static Vector2f convertToUnit(Vector2i vector2i)
      Converts pixel coordinates to draw unit coordinates and takes the draw transform into account.
      Parameters:
      vector2i - pixel coordinate
      Returns:
      unit coordinate
    • isStretch

      public static boolean isStretch()
      Check if horizontal stretching is active. The screen will resize horizontally too with this enabled.
      Returns:
      true if vStretch is enabled
    • setStretch

      public static void setStretch(boolean stretch)
      Allows you to enable horizontal stretching. The screen will resize horizontal too with this enabled.
      Parameters:
      stretch - true - enables vStretch false(default) - disables vStretch; only resizes vertically
    • setUnit

      public static void setUnit(Vector2f newUnit)
      Changes the unit
      Parameters:
      newUnit - new unit
      See Also:
      • unit
    • getUnit

      public static Vector2f getUnit()
      Returns:
      the unit object.
      See Also:
      • unit
    • getTransform

      public static Transform getTransform()
      Gets the current transformation of the Draw
      Returns:
      a copy of the current Transform object
    • getWidth

      public static float getWidth()
      Returns:
      the screen width in units
    • getHeight

      public static float getHeight()
      Returns:
      the screen height in units
    • getCenter

      public static Vector2f getCenter()
      Returns:
      the screen center in units