回答編集履歴
3
import忘れ
answer
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
```xml:screen2.fxml
|
|
12
12
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
13
13
|
|
|
14
|
+
<?import application.Shape1?>
|
|
14
15
|
<?import javafx.scene.layout.AnchorPane?>
|
|
15
16
|
<?import javafx.scene.shape.Rectangle?>
|
|
16
17
|
<?import javafx.scene.text.Text?>
|
|
@@ -84,7 +85,7 @@
|
|
|
84
85
|
}
|
|
85
86
|
}
|
|
86
87
|
```
|
|
87
|
-
|
|
88
|
+
|
|
88
89
|
```xml:shape1.fxml
|
|
89
90
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
90
91
|
|
|
@@ -156,6 +157,7 @@
|
|
|
156
157
|
}
|
|
157
158
|
}
|
|
158
159
|
```
|
|
160
|
+
|
|
159
161
|
```java:Shape1.java
|
|
160
162
|
package application;
|
|
161
163
|
|
|
@@ -177,13 +179,10 @@
|
|
|
177
179
|
@Override public void handle(long timestamp) {
|
|
178
180
|
var offset = shiftPressed.get() ? 5 : 2;
|
|
179
181
|
|
|
180
|
-
// ここは匿名クラスなのでthisはAnimationTimer
|
|
181
|
-
// Shape1を使いたい場合はShape1.this.の表記になる
|
|
182
|
-
// [java 匿名クラス this - Google 検索](https://www.google.com/search?q=java+%E5%8C%BF%E5%90%8D%E3%82%AF%E3%83%A9%E3%82%B9+this)
|
|
183
|
-
if (wPressed.get())
|
|
182
|
+
if (wPressed.get()) setLayoutY(getLayoutY() - offset);
|
|
184
|
-
if (sPressed.get())
|
|
183
|
+
if (sPressed.get()) setLayoutY(getLayoutY() + offset);
|
|
185
|
-
if (aPressed.get())
|
|
184
|
+
if (aPressed.get()) setLayoutX(getLayoutX() - offset);
|
|
186
|
-
if (dPressed.get())
|
|
185
|
+
if (dPressed.get()) setLayoutX(getLayoutX() + offset);
|
|
187
186
|
}
|
|
188
187
|
};
|
|
189
188
|
|
|
@@ -215,7 +214,7 @@
|
|
|
215
214
|
}
|
|
216
215
|
}
|
|
217
216
|
```
|
|
218
|
-
|
|
217
|
+
|
|
219
218
|
```java:HelloApplication.java
|
|
220
219
|
package application;
|
|
221
220
|
|
|
@@ -227,9 +226,7 @@
|
|
|
227
226
|
import java.io.IOException;
|
|
228
227
|
|
|
229
228
|
public class HelloApplication extends Application {
|
|
230
|
-
public static void main(String[] args) {
|
|
229
|
+
public static void main(String[] args) { launch(); }
|
|
231
|
-
launch();
|
|
232
|
-
}
|
|
233
230
|
|
|
234
231
|
@Override public void start(Stage stage) throws IOException {
|
|
235
232
|
var fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("screen2.fxml"));
|
2
不要な括弧
answer
CHANGED
|
@@ -149,10 +149,10 @@
|
|
|
149
149
|
}
|
|
150
150
|
});
|
|
151
151
|
|
|
152
|
-
keyPressed.addListener((
|
|
152
|
+
keyPressed.addListener((_, aBoolean, _) -> {
|
|
153
153
|
if (!aBoolean) timer.start();
|
|
154
154
|
else timer.stop();
|
|
155
|
-
})
|
|
155
|
+
});
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
158
|
```
|
|
@@ -208,10 +208,10 @@
|
|
|
208
208
|
}
|
|
209
209
|
});
|
|
210
210
|
|
|
211
|
-
keyPressed.addListener((
|
|
211
|
+
keyPressed.addListener((_, aBoolean, _) -> {
|
|
212
212
|
if (!aBoolean) timer.start();
|
|
213
213
|
else timer.stop();
|
|
214
|
-
})
|
|
214
|
+
});
|
|
215
215
|
}
|
|
216
216
|
}
|
|
217
217
|
```
|
1
Shape1.java
answer
CHANGED
|
@@ -18,7 +18,12 @@
|
|
|
18
18
|
prefHeight="500.0" prefWidth="1000.0"
|
|
19
19
|
xmlns:fx="http://javafx.com/fxml" fx:controller="application.Screen2Controller">
|
|
20
20
|
<children>
|
|
21
|
-
<fx:include fx:id="shape1" source="shape1.fxml"/>
|
|
21
|
+
<!-- <fx:include fx:id="shape1" source="shape1.fxml"/> -->
|
|
22
|
+
<!-- ↑の代わりに継承クラスを使った場合 -->
|
|
23
|
+
<Shape1 fx:id="shape1" arcHeight="5.0" arcWidth="5.0" fill="#ff8c1f" height="100.0" layoutX="450.0"
|
|
24
|
+
layoutY="400.0"
|
|
25
|
+
focusTraversable="true" stroke="BLACK" strokeType="INSIDE" width="100.0"/>
|
|
26
|
+
|
|
22
27
|
<Rectangle fx:id="boundary1" arcHeight="5.0" arcWidth="5.0" fill="TRANSPARENT" height="200.0" layoutX="400.0"
|
|
23
28
|
layoutY="150.0" stroke="BLACK" strokeType="INSIDE" width="200.0"/>
|
|
24
29
|
<Text fx:id="text" layoutX="417.0" layoutY="63.0" text="何にも接触していません。"/>
|
|
@@ -151,7 +156,66 @@
|
|
|
151
156
|
}
|
|
152
157
|
}
|
|
153
158
|
```
|
|
159
|
+
```java:Shape1.java
|
|
160
|
+
package application;
|
|
154
161
|
|
|
162
|
+
import javafx.animation.AnimationTimer;
|
|
163
|
+
import javafx.beans.binding.BooleanBinding;
|
|
164
|
+
import javafx.beans.property.BooleanProperty;
|
|
165
|
+
import javafx.beans.property.SimpleBooleanProperty;
|
|
166
|
+
import javafx.scene.shape.Rectangle;
|
|
167
|
+
|
|
168
|
+
public class Shape1 extends Rectangle {
|
|
169
|
+
private final BooleanProperty wPressed = new SimpleBooleanProperty();
|
|
170
|
+
private final BooleanProperty aPressed = new SimpleBooleanProperty();
|
|
171
|
+
private final BooleanProperty sPressed = new SimpleBooleanProperty();
|
|
172
|
+
private final BooleanProperty dPressed = new SimpleBooleanProperty();
|
|
173
|
+
private final BooleanProperty shiftPressed = new SimpleBooleanProperty();
|
|
174
|
+
private final BooleanBinding keyPressed = wPressed.or(aPressed).or(sPressed).or(dPressed).or(shiftPressed);
|
|
175
|
+
|
|
176
|
+
private final AnimationTimer timer = new AnimationTimer() {
|
|
177
|
+
@Override public void handle(long timestamp) {
|
|
178
|
+
var offset = shiftPressed.get() ? 5 : 2;
|
|
179
|
+
|
|
180
|
+
// ここは匿名クラスなのでthisはAnimationTimer
|
|
181
|
+
// Shape1を使いたい場合はShape1.this.の表記になる
|
|
182
|
+
// [java 匿名クラス this - Google 検索](https://www.google.com/search?q=java+%E5%8C%BF%E5%90%8D%E3%82%AF%E3%83%A9%E3%82%B9+this)
|
|
183
|
+
if (wPressed.get()) Shape1.this.setLayoutY(Shape1.this.getLayoutY() - offset);
|
|
184
|
+
if (sPressed.get()) Shape1.this.setLayoutY(Shape1.this.getLayoutY() + offset);
|
|
185
|
+
if (aPressed.get()) Shape1.this.setLayoutX(Shape1.this.getLayoutX() - offset);
|
|
186
|
+
if (dPressed.get()) Shape1.this.setLayoutX(Shape1.this.getLayoutX() + offset);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
public Shape1() {
|
|
191
|
+
setOnKeyPressed(e -> {
|
|
192
|
+
switch (e.getCode()) {
|
|
193
|
+
case W -> wPressed.set(true);
|
|
194
|
+
case A -> aPressed.set(true);
|
|
195
|
+
case S -> sPressed.set(true);
|
|
196
|
+
case D -> dPressed.set(true);
|
|
197
|
+
case SHIFT -> shiftPressed.set(true);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
setOnKeyReleased(e -> {
|
|
202
|
+
switch (e.getCode()) {
|
|
203
|
+
case W -> wPressed.set(false);
|
|
204
|
+
case A -> aPressed.set(false);
|
|
205
|
+
case S -> sPressed.set(false);
|
|
206
|
+
case D -> dPressed.set(false);
|
|
207
|
+
case SHIFT -> shiftPressed.set(false);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
keyPressed.addListener(((_, aBoolean, _) -> {
|
|
212
|
+
if (!aBoolean) timer.start();
|
|
213
|
+
else timer.stop();
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
155
219
|
```java:HelloApplication.java
|
|
156
220
|
package application;
|
|
157
221
|
|