Skip to content

Method: draw()

1: /*
2: * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel24-VierConnects-gui.
5: *
6: * ipspiel24-VierConnects-gui is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * ipspiel24-VierConnects-gui is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * ipspiel24-VierConnects-gui. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel24.VierConnects.gui.impl;
20:
21: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsFieldState;
22: import javafx.beans.property.ObjectProperty;
23: import javafx.beans.property.SimpleObjectProperty;
24: import javafx.beans.value.ObservableValue;
25: import javafx.scene.canvas.Canvas;
26: import javafx.scene.canvas.GraphicsContext;
27: import javafx.scene.layout.Region;
28: import javafx.scene.paint.Color;
29: import javafx.util.Duration;
30:
31: /**
32: * Displays a Vier Connects field.
33: */
34: final class VierConnectsFieldView extends Region {
35:
36: /**
37: * The field state.
38: */
39: private VierConnectsFieldState fieldState;
40:
41: /**
42: * The canvas.
43: */
44: private final Canvas canvas;
45:
46: /**
47: * The duration of a move.
48: */
49: private final ObjectProperty<Duration> moveDuration;
50:
51: /**
52: * {@code true} if the field is highlighted, else {@code false}.
53: */
54: private boolean highlighted;
55:
56: /**
57: * Creates an {@link VierConnectsFieldView}.
58: *
59: * @param fieldState The field state.
60: */
61: VierConnectsFieldView(final VierConnectsFieldState fieldState) {
62: this.fieldState = fieldState;
63: this.moveDuration = new SimpleObjectProperty<>(Duration.millis(500.0));
64: this.highlighted = false;
65:
66: this.canvas = new Canvas() {
67:
68: @Override
69: public boolean isResizable() {
70: return true;
71: }
72:
73: @Override
74: public double prefWidth(final double height) {
75: return 0.0;
76: }
77:
78: @Override
79: public double prefHeight(final double width) {
80: return 0.0;
81: }
82:
83: @Override
84: public double maxWidth(final double height) {
85: return Double.POSITIVE_INFINITY;
86: }
87:
88: @Override
89: public double maxHeight(final double width) {
90: return Double.POSITIVE_INFINITY;
91: }
92: };
93:
94: this.canvas.widthProperty().bind(this.widthProperty());
95: this.canvas.heightProperty().bind(this.heightProperty());
96: this.getChildren().add(this.canvas);
97:
98: this.widthProperty().addListener(
99: (final ObservableValue<? extends Number> observable, final Number oldValue, final Number newValue) -> {
100: this.draw();
101: });
102: this.heightProperty().addListener(
103: (final ObservableValue<? extends Number> observable, final Number oldValue, final Number newValue) -> {
104: this.draw();
105: });
106:
107: this.setMinSize(50.0, 50.0);
108: this.setMaxSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
109: }
110:
111: /**
112: * Returns the move duration property.
113: */
114: ObjectProperty<Duration> moveDurationProperty() {
115: return this.moveDuration;
116: }
117:
118: /**
119: * Updates the state of the field control.
120: *
121: * @param fieldState The field state.
122: */
123: void setFieldState(final VierConnectsFieldState fieldState) {
124: this.fieldState = fieldState;
125: this.draw();
126: }
127:
128: /**
129: * Updates the highlight state of the field control.
130: *
131: * @param highlighted {@code true} if the field is highlighted, else {@code false}.
132: */
133: void setHighlighted(final boolean highlighted) {
134: this.highlighted = highlighted;
135: this.draw();
136: }
137:
138: /**
139: * Draws the field.
140: */
141: private void draw() {
142: final double height = this.getHeight();
143: final double width = this.getWidth();
144:
145: final GraphicsContext gc = this.canvas.getGraphicsContext2D();
146:• gc.setFill(this.highlighted ? Color.BLUE : Color.DARKBLUE);
147: gc.fillRect(0.0, 0.0, width, height);
148:
149: gc.setFill(Color.BLACK);
150: gc.beginPath();
151: gc.moveTo(0.0, 0.0);
152: gc.lineTo(width, 0.0);
153: gc.lineTo(width, height);
154: gc.lineTo(0.0, height);
155: gc.lineTo(0.0, 0.0);
156: gc.closePath();
157:
158: final double marginheight = height * 0.1;
159: final double marginwidth = width * 0.1;
160:
161: gc.setFill(Color.WHITE);
162: gc.fillOval(marginwidth, marginheight, width - 2 * marginwidth, height - 2 * marginheight);
163: gc.stroke();
164:
165:
166:
167:
168:• switch (this.fieldState) {
169: case EMPTY:
170: // nothing to do
171: break;
172: case CROSS:
173: gc.setFill(Color.RED);
174: gc.fillOval(marginwidth, marginheight, width - 2 * marginwidth, height - 2 * marginheight);
175: break;
176: case NOUGHT:
177: gc.setFill(Color.YELLOW);
178: gc.fillOval(marginwidth, marginheight, width - 2 * marginwidth, height - 2 * marginheight);
179: break;
180: default:
181: throw new UnsupportedOperationException(
182: String.format("Unknown Vier Connects field state '%s'.", this.fieldState));
183: }
184: }
185: }