-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquareTile.java
112 lines (103 loc) · 3.55 KB
/
SquareTile.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package lab1;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
/**
* A square tile manages painting of a square
* in a specified area of the screen.
*
* Whenever the object should paint itself,
* it is told what size and position that
* should be used to paint it.
*/
public class SquareTile extends GameTile {
/** The color of the square */
private final Color strokeColor;
private final Color fillColor;
private final Stroke stroke;
private final double scale;
/**
* Creates a square game tile.
*
* @param fillColor
* the color of the interior of the square.
*/
public SquareTile(final Color fillColor) {
this(fillColor, fillColor);
}
/**
* Creates a square game tile with a stroke around it.
*
* @param strokeColor
* the color of the stroke around the square.
* @param fillColor
* the color of the interior of the square.
*/
public SquareTile(final Color strokeColor, final Color fillColor) {
this(strokeColor, fillColor, 1.0);
}
/**
* Creates a square game tile with a stroke around it.
*
* @param strokeColor
* the color of the stroke around the square.
* @param fillColor
* the color of the interior of the square.
* @param thickness
* the thickness of the stroke.
*/
public SquareTile(final Color strokeColor, final Color fillColor,
final double thickness) {
this(strokeColor, fillColor, thickness, 1.0);
}
/**
* Creates a circular game tile with a stroke around it.
*
* @param strokeColor
* the color of the stroke around the square.
* @param fillColor
* the color of the interior of the square.
* @param thickness
* the thickness of the stroke.
* @param scale
* size of the sqaure relative to the tile size.
*/
public SquareTile(final Color strokeColor, final Color fillColor,
final double thickness, final double scale) {
this.strokeColor = strokeColor;
this.fillColor = fillColor;
this.stroke = new BasicStroke((float) thickness);
this.scale = scale;
}
/**
* Draws itself in a given graphics context, position and size.
*
* @param g
* graphics context to draw on.
* @param x
* pixel x coordinate of the tile to be drawn.
* @param y
* pixel y coordinate of the tile to be drawn.
* @param d
* size of this object in pixels.
*/
@Override
public void draw(final Graphics g, final int x, final int y,
final Dimension d) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(this.fillColor);
double xOffset = (d.width * (1.0 - this.scale)) / 2.0;
double yOffset = (d.height * (1.0 - this.scale)) / 2.0;
g2.fillRect((int) (x + xOffset), (int) (y + yOffset),
(int) (d.width - xOffset * 2),
(int) (d.height - yOffset * 2));
g2.setStroke(this.stroke);
g2.setColor(this.strokeColor);
g2.drawRect((int) (x + xOffset), (int) (y + yOffset),
(int) (d.width - xOffset * 2),
(int) (d.height - yOffset * 2));
}
}