自習
EXE3501.exeに関して
Button transLine = new Button("太");
//int hoge;//線の太さ //here? NO!
hoge = getLineWidth();
transLine.setOnAction((ActionEvent event)->{
hoge = hoge+2;
gc.setLineWidth(hoge);
//System.out.println("hoge is"+ hoge);
});
この部分ですね。
最初、この部分で変数の宣言をすればいいかなーと思ったらがっつりエラーがでましたね?
→
なぜか。
①transLine.setOnAction((ActionEvent event)->{
の中は「別世界」
②別世界なので、ここで使われる変数はおそらく「一番上」で宣言してないと使えないっぽい
ですよ
--->先生に教えてもらったURLの分を動くように当てはめることに成功した
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class EXE9001 extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
View v = new View();
Scene scene = new Scene(v, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
class View extends Group{
public View() {
Rectangle rect1 = new Rectangle(100, 120, 100, 100);
Rectangle rect2 = new Rectangle(70, 140, 100, 100);
rect1.setStroke(Color.RED);
rect1.setFill(null);
rect2.setStroke(Color.BLUE);
rect2.setFill(null);
getChildren().add(rect1);
getChildren().add(rect2);
Text text = new Text("当たっていません。");
text.setFont(new Font(20));
//矩形同士の当たり判定
if ( rect1.intersects(rect2.getBoundsInLocal()) ) {
text.setText("当たっています。");
text.setFill(Color.RED);
}
text.setY(100);
text.setX(200 - text.getBoundsInLocal().getWidth() / 2);
getChildren().add(text);
}
}
}
こういうのはコピペでなく「インポート」すればよいのだ。