aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/context/GraphicsContext.java
blob: 2c1c72acab76fa75c29695f648d535c83185d23f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package fr.umlv.java.wallj.context;

import fr.umlv.java.wallj.board.TileVec2;
import fr.umlv.zen5.ScreenInfo;
import org.jbox2d.common.Vec2;

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.Objects;

/**
 * A context of the current graphic status of the application.
 *
 * @author Adam NAILI
 */
public final class GraphicsContext {
  private final Graphics2D graphics2D;
  private final ScreenInfo screenInfo;

  /**
   * @param graphics2D the current drawable canvas
   * @param screenInfo the informations about the screen
   */
  public GraphicsContext(Graphics2D graphics2D, ScreenInfo screenInfo) {
    this.graphics2D = Objects.requireNonNull(graphics2D);
    this.screenInfo = Objects.requireNonNull(screenInfo);
  }

  /**
   * @return the drawable canvas
   */
  public Graphics2D getGraphics2D() {
    return graphics2D;
  }

  /**
   * @return the screen informations
   */
  public ScreenInfo getScreenInfo() {
    return screenInfo;
  }

  /**
   * @param color    the color of the circle
   * @param position the upper left corner of the rectangle frame that contains the circle
   * @param size     size of the circle
   */
  public void paintCircle(Color color, Vec2 position, float size) {
    graphics2D.setColor(color);
    graphics2D.fillOval(Math.round(position.x + (TileVec2.TILE_DIM - size) / 2) - 1, Math.round(position.y + (TileVec2.TILE_DIM - size) / 2) - 1, Math.round(size), Math.round(size));
  }

  /**
   * @param color    the color of the rectangle
   * @param position the upper left corner of the rectangle
   * @param width    width of the rectangle
   * @param height   height of the rectangle
   */
  public void paintRectangle(Color color, Vec2 position, float width, float height) {
    graphics2D.setColor(color);
    graphics2D.fillRect(Math.round(position.x), Math.round(position.y), Math.round(width), Math.round(height));
  }

  public void paintString(Color color, Vec2 position, String string){
    graphics2D.setColor(color);
    graphics2D.drawString(string,position.x, position.y);
  }
}