-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldCanvas.java
47 lines (39 loc) · 1.19 KB
/
FieldCanvas.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
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
/**
* Provide a graphical view of the field. This is a custom node for the user interface.
*
* @author Jeffery Raphael
* @version 2024.02.03
*/
public class FieldCanvas extends Canvas {
private static final int GRID_VIEW_SCALING_FACTOR = 6;
private int width, height;
private int xScale, yScale;
GraphicsContext gc;
/**
* Create a new FieldView component.
*/
public FieldCanvas(int height, int width) {
super(height, width);
gc = getGraphicsContext2D();
this.height = height;
this.width = width;
}
public void setScale(int gridHeight, int gridWidth) {
xScale = width / gridWidth;
yScale = height / gridHeight;
if (xScale < 1)
xScale = GRID_VIEW_SCALING_FACTOR;
if (yScale < 1)
yScale = GRID_VIEW_SCALING_FACTOR;
}
/**
* Paint a rectangle of the given color on the canvas
*/
public void drawMark(int x, int y, Color color) {
gc.setFill(color);
gc.fillRect(x * xScale, y * yScale, xScale-1, yScale-1);
}
}