java.lang.Object
org.variantsync.diffdetective.show.engine.Texture
All Implemented Interfaces:
Serializable

public class Texture extends Object implements Serializable
This class is used to represent, load and redraw the Images of the entities and the world. The upper left corner of the Texture has the coordinates (0/0). The lower right corner of the Texture has the coordinates (getWidth()/getHeight()).
Author:
Paul Maximilian Bittner
See Also:
  • Field Details

  • Constructor Details

    • Texture

      public Texture(int width, int height)
      Creates a new Texture of the given size in pixels.
    • Texture

      public Texture(String path)
      Creates a new Texture from the given path.
    • Texture

      public Texture(File file)
      Creates a new Texture from the given file.
    • Texture

      public Texture(Texture texture)
      Creates a copy of the given texture.
    • Texture

      public Texture(BufferedImage image)
      Creates a new Texture from a BufferedImage.
    • Texture

      public Texture(String text, int height, Color foreground, Color background)
      Creates a new Texture, that is only text.
      Parameters:
      text - the String that should be drawn as this Texture
      height - the height of the text in pixels
      foreground - the color of the text
      background - the color behind the text
    • Texture

      public Texture(String text, int height, Color foreground, Color background, Font font)
      Creates a new Texture, that is only text.
      Parameters:
      text - the String that should be drawn as this Texture
      height - the height of the text in pixels
      foreground - the color of the text
      background - the color behind the text
      font - the Font for the text
  • Method Details

    • CreateFromFile

      public static Texture CreateFromFile(String path)
      Creates a new Texture from the given path.
    • init

      private void init()
      Initializes the values.
    • copy

      private BufferedImage copy(Texture texture)
    • copy

      private BufferedImage copy(BufferedImage image)
    • setTransparency

      public void setTransparency(int value)
      This method makes images becoming more or less transparent.
      Parameters:
      value - The intensity of transparency. It has to be between 0 and 255 (including both). (It represents the alpha value of a RGB-color.)
    • scale

      public void scale(double factor)
      The Texture will be resized by the given factor, that means, that the width and height of the Texture will be multiplied with factor:
      Parameters:
      factor - The factor the width and the height of the texture will be multiplied with. If factor is smaller than 1 the Texture will be shrinked. If factor is larger than 1 the Texture will grow. If factor is 1 it won't change.
    • scale

      public void scale(int width, int height)
      The Texture will be resized to the given width and height.
      Parameters:
      width - the new width of the texture
      height - the new height of the texture
    • mirrorHorizontally

      public void mirrorHorizontally()
      The Texture will be mirrored by a horizontal axis, that goes through the center of the Texture.
    • mirrorVertically

      public void mirrorVertically()
      The Texture will be mirrored by a vertical axis, that goes through the center of the Texture.
    • rotate

      public Texture rotate(double degrees)
      The size of the returned Texture may be different to the original size, to be able to display the whole rotated image, and not only a part of it.
      Parameters:
      degrees - The amount of degrees, the Texture should be rotated to the right.
      Returns:
      A Texture, that is this texture, rotated by the given degrees.
    • cut

      public Texture cut(int x, int y, int width, int height)
      Cuts out the given rectangle, meaning, that this rectangle will be filled transparent. NOT IMPLEMENTED
    • drawLine

      public void drawLine(int x1, int y1, int x2, int y2)
      Draws a line onto the Texture from P(x1/y1) to Q(x2/y2) with the current drawing color.
      Parameters:
      x1 - the x coordinate of the first point
      y1 - the y coordinate of the first point
      x2 - the x coordinate of the second point
      y2 - the y coordinate of the second point
    • drawLine

      public void drawLine(Point pointA, Point pointB)
      Draws a line onto the Texture from pointA to pointB with the current drawing color.
      Parameters:
      pointA - The start point of the line.
      pointB - The end point of the line.
    • drawRect

      public void drawRect(int x, int y, int width, int height)
      Draws a rectangle with the current drawing color onto the Texture.
      Parameters:
      x - the x coordinate of the upper left corner of the rectangle
      y - the y coordinate of the upper left corner of the rectangle
      width - the width of the rectangle
      height - the height of the rectangle
    • drawOval

      public void drawOval(int x, int y, int width, int height)
      Draws an Oval with the current drawing color onto the Texture.
      Parameters:
      x - the x coordinate of the upper left corner of the oval
      y - the y coordinate of the upper left corner of the oval
      width - the width of the oval
      height - the height of the oval
    • drawPolygon

      public void drawPolygon(int[] xPoints, int[] yPoints)
      Draws a closed polygon defined by arrays of x and y coordinates. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.
      Parameters:
      xPoints - the x coordinates of the corner points
      yPoints - the y coordinates of the corner points
    • drawPolygon

      public void drawPolygon(Point[] points)
      Draws a closed polygon defined by the given array of points. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.
      Parameters:
      points - The points of the polygon in the correct order.
    • drawTexture

      public void drawTexture(Texture texture, int x, int y)
      Draws the given Texture onto this Texture at the given location. The coordinate describes the upper left corner of texture.
      Parameters:
      texture - the Texture, that should be painted onto this one
      x - the x coordinate of the upper left corner of texture in this Texture
      y - the y coordinate of the upper left corner of texture in this Texture
    • drawString

      public void drawString(String text, int x, int y)
      Draws a String onto this image at the location P(x/y) with the given Font and color.
      Parameters:
      text - the String, that should be written onto this texture
      x - the x coordinate of the lower left corner of text
      y - the y coordinate of the lower left corner of text
    • fill

      public void fill()
      Fills the Texture with the current drawing color.
    • fillRect

      public void fillRect(int x, int y, int width, int height)
      Draws a Rectangle onto the Texture and fills it with the current drawing color.
      Parameters:
      x - the x coordinate of the upper left corner of the rectangle
      y - the y coordinate of the upper left corner of the rectangle
      width - the width of the rectangle
      height - the height of the rectangle
    • fillOval

      public void fillOval(int x, int y, int width, int height)
      Draws an Oval with the current drawing color onto the Texture and fills it.
      Parameters:
      x - the x coordinate of the upper left corner of the oval
      y - the y coordinate of the upper left corner of the oval
      width - the width of the oval
      height - the height of the oval
    • fillPolygon

      public void fillPolygon(int[] xPoints, int[] yPoints)
      Fills a closed polygon defined by arrays of x and y coordinates. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.
      Parameters:
      xPoints - the x coordinates of the corner points
      yPoints - the y coordinates of the corner points
    • fillPolygon

      public void fillPolygon(Point[] points)
      Fills a closed polygon defined by the given array of points. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.
      Parameters:
      points - The points of the polygon in the correct order.
    • setColor

      public void setColor(Color color)
      Parameters:
      color - the color used to draw
    • setColorAt

      public void setColorAt(int x, int y, Color color)
      Paints the pixel at the location p(x/y) with color.
      Parameters:
      x - the x coordinate of the pixel
      y - the y coordinate of the pixel
      color - the color for the pixel
    • setFont

      public void setFont(Font font)
      Sets the current Font for drawing Strings.
      Parameters:
      font - the font for drawing strings
    • setAwtImage

      public void setAwtImage(BufferedImage image)
    • set

      public void set(Texture other)
    • getColorAt

      public Color getColorAt(int x, int y)
      Returns:
      the color of the pixel at p(x/y)
    • getWidth

      public int getWidth()
      Returns:
      the width of the Texture in pixels
    • getHeight

      public int getHeight()
      Returns:
      the height of the Texture in pixels
    • getAwtImage

      public BufferedImage getAwtImage()
      Returns:
      the BufferedImage, that represents this Texture
    • getFont

      public Font getFont()
      Returns:
      the current Font for drawing Strings
    • getColor

      public Color getColor()
      Returns:
      the color used to draw
    • contains

      public boolean contains(int x, int y)
    • redPart

      public Texture redPart()
      Returns:
      a Texture, that represents the red part of this Texture of the RGB spectrum
    • greenPart

      public Texture greenPart()
      Returns:
      a Texture, that represents the green part of this Texture of the RGB spectrum
    • bluePart

      public Texture bluePart()
      Returns:
      a Texture, that represents the blue part of this Texture of the RGB spectrum
    • contrast

      public Texture contrast(int blackTop)
      Parameters:
      blackTop - It's used to distinguish between black and white. It has to be between 0 and 255 (including both). As higher blackTop is as darker the Texture gets.
      Returns:
      a Texture, that is the contrast of this Texture
    • blackAndWhite

      public Texture blackAndWhite()
      Returns:
      a new Texture, that is this Texture filtered to black and white
    • negative

      public Texture negative()
      Returns:
      a new Texture, that is the negative of this Texture
    • pixelize

      public Texture pixelize(int pixelLength)
      Parameters:
      pixelLength - the length of a pixel in the finished image in pixels
      Returns:
      a new Texture, that is this Texture pixeled by the given parameter.
    • deleteColor

      public Texture deleteColor(Color color)
      Returns:
      A Texture, which colors equal to the parameter are changed to transparent.
    • changeColor

      public Texture changeColor(Color from, Color to)
      Returns:
      A Texture, which colors equal to the parameter are changed to the other one.
    • loadImage

      private static BufferedImage loadImage(File file)
      This method loads an image from the given file.
      Returns:
      a java.awt.image.BufferdImage representing the image in file.
    • loadFont

      public static Font loadFont(String filename)
      Parameters:
      filename - The directory of the font. It should be placed in "Resources/Fonts/". Otherwise it can't be found.
      Returns:
      the font from the file, null if it couldn't be found or loaded
    • loadSpriteSheet

      public static Texture[] loadSpriteSheet(String filename, int xOffset, int yOffset)
      Parameters:
      filename - The directory of the font. It should be placed in "Resources/Fonts/". Otherwise it can't be found.
      xOffset - The width of one image on the sprite sheet.
      yOffset - The height of one image on the sprite sheet.
      Returns:
      All Texture in the sprite sheet in a Texture array.
    • saveAsPng

      public void saveAsPng(File file) throws IOException
      The texture will be saved with the given name as a png-file into the screenshots folder.
      Parameters:
      file - the file, in which the texture is saved
      Throws:
      IOException
    • inHeight

      public boolean inHeight(int y)
    • inWidth

      public boolean inWidth(int x)