質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
JavaFX

JavaFXとは、Java仮想マシン上で動作するリッチインターネットアプリケーション (RIA) のGUIライブラリです。Swingとは異なり、FXMLと呼ばれる XMLとCSSを併用してデザインを記述します。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

2回答

300閲覧

AnimationTimer()による画面の分岐

nununuso

総合スコア9

JavaFX

JavaFXとは、Java仮想マシン上で動作するリッチインターネットアプリケーション (RIA) のGUIライブラリです。Swingとは異なり、FXMLと呼ばれる XMLとCSSを併用してデザインを記述します。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2017/06/22 14:09

編集2017/06/22 17:59
  • 前提・実現したいこと

AnimationTimer()でサイクルをループさせたい

1.1つ目のボタンの表示
2.ボタン1(画像)が5回押されたら3に移行
(ボタン1が押された時のみ画像が変化
3.ボタン2(画像)が5回押されたら1に戻る
(ボタン2が押された時のみ画像が変化

またゲームなどの画面切り替えの際にsceneを複数用意して条件によって切り替えるのか
scene1つだけ作りsceneの中身を条件によって変更していくのは
どちらが一般的な方法でしょうか?
贅沢な質問申し訳ありません

  • 対応するコード

java

1package novelgame; 2 3import javafx.animation.AnimationTimer; 4import javafx.application.Application; 5import javafx.event.EventHandler; 6import javafx.scene.Group; 7import javafx.scene.Scene; 8import javafx.scene.canvas.Canvas; 9import javafx.scene.canvas.GraphicsContext; 10import javafx.scene.image.Image; 11import javafx.scene.input.MouseEvent; 12import javafx.scene.media.AudioClip; 13import javafx.stage.Stage; 14 15public class test2 extends Application 16{ 17 int num = 1; 18 int count = 1; 19 double pw,pv= 0; 20 Scene theScene1; 21 Stage theStage1; 22 GraphicsContext graphicsContext; 23 Image image; 24 Image image2; 25 Image image3; 26 Image image4; 27 AudioClip plonkSound; 28 static int width = 512; 29 static int heigh = 256; 30 31 public static void main(String[] args) 32 { 33 launch(args); 34 } 35 36 @Override public void start(Stage theStage) 37 { 38 this.theStage1 = theStage; 39 40 new AnimationTimer() 41 { 42 @Override 43 public void handle(long now) 44 { 45 switch (num) 46 { 47 case 1: 48 System.out.println(1); 49 set(); 50 num = num + 1; 51 case 2: 52 System.out.println(2); 53 botten1(); 54 break; 55 case 3: 56 System.out.println(3); 57 botten2(); 58 break; 59 } 60 } 61 };//;を追記 62 63 theScene1.setOnMousePressed( 64 new EventHandler<MouseEvent>() 65 { 66 public void handle(MouseEvent pe) 67 { 68 pw=pe.getX(); 69 pv=pe.getY(); 70 71 System.out.println("Push X=" + pw + " Y=" + pv); 72 } 73 }); 74 75 } 76 77 void set(){ 78 theStage1.setTitle("てすと"); 79 Image image = new Image(getClass().getResource("kimi.png").toExternalForm()); 80 Image image2 = new Image(getClass().getResource("kimi2.png").toExternalForm()); 81 Image image3 = new Image(getClass().getResource("manzi.png").toExternalForm()); 82 Image image4 = new Image(getClass().getResource("manzi2.png").toExternalForm()); 83 //画像サイズは全て100*150 84 85 AudioClip plonkSound = new AudioClip(getClass().getResource("kimi.mp3").toExternalForm()); 86 87 Group root = new Group(); 88 Canvas canvas = new Canvas(width, heigh); 89 GraphicsContext graphicsContext = canvas.getGraphicsContext2D(); 90 graphicsContext.drawImage(image, 100 , 100); 91 root.getChildren().add(canvas); 92 Scene theScene = new Scene(root); 93 this.theScene1 = theScene; 94 this.image = image; 95 this.image2 = image2; 96 this.image3 = image3; 97 this.image4 = image4; 98 this.graphicsContext = graphicsContext; 99 this.plonkSound = plonkSound; 100 theStage1.setWidth(280); 101 theStage1.setHeight(280); 102 theStage1.setScene(theScene1); 103 theStage1.show(); 104 } 105 106 void botten1(){ 107 while(true){ 108 graphicsContext.clearRect(0, 0, width, heigh); 109 if(pw>100 && pw<250 && pv>100 && pv<200){ 110 System.out.println("ボタン1が押されたよ"); 111 graphicsContext.drawImage(image2, 100 , 100); 112 plonkSound.play(); 113 count = count + 1; 114 }else{ 115 graphicsContext.drawImage(image, 100 , 100); 116 } 117 if(count == 5){ 118 num = 3; 119 count = 1; 120 break; 121 } 122 123 } 124 } 125 126 void botten2(){ 127 while(true){ 128 graphicsContext.clearRect(0, 0, width, heigh); 129 if(pw>0 && pw<150 && pv>0 && pv<100){ 130 System.out.println("ボタン2が押されたよ"); 131 graphicsContext.drawImage(image4, 0 , 0); 132 System.out.println("音楽スタート"); 133 count = count + 1; 134 }else{ 135 graphicsContext.drawImage(image3, 0 , 0); 136 } 137 138 if(count == 5){ 139 count = 1; 140 num = 1; 141 break; 142 } 143 } 144 } 145}
  • 追記エラーメッセージ

run:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at novelgame.test2.start(test2.java:64)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application novelgame.test2
/Users/username/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
ビルド失敗(合計時間: 2秒)

  • 補足情報(言語/FW/ツール等のバージョンなど)

macのNetBeans 8.2環境

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

コンパイルは通るのですが

コンパイルエラーが残っているプログラムを動かすことは普通しないだろうと誰もが思うはずなので一々かかなくても大丈夫なのですが・・・

実際はコードにコンパイルエラーがあります。セミコロンが1か所抜けています。

java

1new AnimationTimer() { 2 @Override 3 public void handle(long now) { 4 switch (num) { 5 case 1: 6 System.out.println(1); 7 set(); 8 num = num + 1; // <==多分ここにbreakが抜けている気がします 9 case 2: 10 ... 11 } 12 } 13} // <====ここにセミコロンが抜けている

セミコロンを補ったコードで実行したのかも知れませんが、「質問文には最新のコード」を張り付けるように気を付けてください。さもないと発生している現象とコードが不一致になってしまうので読者を混乱させてしまいます。

実行するとエラーが起きてしまう

「エラーが起きてしまう」とだけしか書いていない質問は基本的にNGだと思います。最低でも何が起こっているか読者に伝わるように、エラーメッセージ(どの例外かが分かるもの)とスタックトレースを張り付けてください。

質問するときのヒントをよくご覧ください。


なお、コンパイルエラーを取り去った上で動かしたとすると、theScene1.setOnMousePressed(...)のところでNullPointerExceptionが発生すると思います。変数theScene1にSceneインスタンスが設定されていないからです。

その他にもおかしな点(バグ)が多数あるようですが、数が多いので今回指摘するのは控えます。

投稿2017/06/22 15:19

KSwordOfHaste

総合スコア18392

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nununuso

2017/06/22 17:42

具体性がなく困難を招いてしまって申し訳ありません 質問者として当然のマナーが欠けていました それでも回答して頂きありがとうございます しばらく考えてから出直そうと思います
guest

0

自己解決

java

1package novelgame; 2 3import javafx.animation.AnimationTimer; 4import javafx.application.Application; 5import javafx.event.EventHandler; 6import javafx.scene.Group; 7import javafx.scene.Scene; 8import javafx.scene.canvas.Canvas; 9import javafx.scene.canvas.GraphicsContext; 10import javafx.scene.image.Image; 11import javafx.scene.input.MouseEvent; 12import javafx.scene.media.AudioClip; 13import javafx.stage.Stage; 14 15public class test5 extends Application 16{ 17 int num = 1; 18 int count = 0; 19 double px,py= 0; 20 21 Stage theStage1; 22 Image image; 23 Image image2; 24 Image image3; 25 Image image4; 26 AudioClip plonkSound; 27 28 static int width = 512; 29 static int heigh = 256; 30 31 public static void main(String[] args) 32 { 33 launch(args); 34 } 35 36 @Override public void start(Stage theStage) 37 { 38 Group root1 = new Group(); 39 Group root2 = new Group(); 40 Group root3 = new Group(); 41 Group root4 = new Group(); 42 43 Scene theScene1 = new Scene(root1); 44 Scene theScene2 = new Scene(root2); 45 Scene theScene3 = new Scene(root3); 46 Scene theScene4 = new Scene(root4); 47 48 this.theStage1 = theStage; 49 theStage1.setWidth(280); 50 theStage1.setHeight(280); 51 theStage1.show(); 52 53 new AnimationTimer() 54 { 55 @Override 56 public void handle(long now) 57 { 58 switch (num) 59 { 60 case 1: 61 set(root1,root2,root3,root4); 62 break; 63 64 case 2: 65 botten1(theScene1,theScene2); 66 break; 67 68 case 3: 69 botten2(theScene3,theScene4); 70 break; 71 } 72 73 } 74 }.start(); 75 76 theScene1.setOnMousePressed( 77 new EventHandler<MouseEvent>() 78 { 79 public void handle(MouseEvent pe) 80 { 81 px=pe.getX(); 82 py=pe.getY(); 83 84 System.out.println("Push X=" + px + " Y=" + py); 85 } 86 }); 87 88 theScene3.setOnMousePressed( 89 new EventHandler<MouseEvent>() 90 { 91 public void handle(MouseEvent pe) 92 { 93 px=pe.getX(); 94 py=pe.getY(); 95 96 System.out.println("Push X=" + px + " Y=" + py); 97 } 98 }); 99 } 100 101 void set(Group root1,Group root2,Group root3,Group root4){ 102 num = num + 1; 103 theStage1.setTitle("Sceneテスト"); 104 Image image = new Image(getClass().getResource("kimi.png").toExternalForm()); 105 Image image2 = new Image(getClass().getResource("kimi2.png").toExternalForm()); 106 Image image3 = new Image(getClass().getResource("manzi.png").toExternalForm()); 107 Image image4 = new Image(getClass().getResource("manzi2.png").toExternalForm()); 108 this.image = image; 109 this.image2 = image2; 110 this.image3 = image3; 111 this.image4 = image4; 112 //画像サイズは全て100*150 113 114 AudioClip plonkSound = new AudioClip(getClass().getResource("kimi.mp3").toExternalForm()); 115 this.plonkSound = plonkSound; 116 117 Canvas canvas1 = new Canvas(width, heigh); 118 GraphicsContext graphicsContext1 = canvas1.getGraphicsContext2D(); 119 graphicsContext1.drawImage(image, 100 , 100); 120 root1.getChildren().add(canvas1); 121 122 Canvas canvas2 = new Canvas(width, heigh); 123 GraphicsContext graphicsContext2 = canvas2.getGraphicsContext2D(); 124 graphicsContext2.drawImage(image2, 100 , 100); 125 root2.getChildren().add(canvas2); 126 127 Canvas canvas3 = new Canvas(width, heigh); 128 GraphicsContext graphicsContext3 = canvas3.getGraphicsContext2D(); 129 graphicsContext3.drawImage(image3, 0 , 0); 130 root3.getChildren().add(canvas3); 131 132 Canvas canvas4 = new Canvas(width, heigh); 133 GraphicsContext graphicsContext4 = canvas4.getGraphicsContext2D(); 134 graphicsContext4.drawImage(image4, 0 , 0); 135 root4.getChildren().add(canvas4); 136 } 137 138 void botten1(Scene theScene1,Scene theScene2){ 139// pw = 110; 140// pv = 110; 141 if(px>100 && px<250 && py>100 && py<200){ 142 px = 0; 143 py = 0; 144 System.out.println("ボタン1が押されたよ"); 145 theStage1.setScene(theScene2); 146 count = count + 1; 147 }else{ 148 theStage1.setScene(theScene1); 149 } 150 151 if(count == 3){ 152 count = 0; 153 num = 3; 154 System.out.println("ボタン2へ移動"); 155 } 156 } 157 158 void botten2(Scene theScene3,Scene theScene4){ 159// pw = 10; 160// pv = 10; 161 if(px>0 && px<150 && py>0 && py<100){ 162 px = 0; 163 py = 0; 164 System.out.println("ボタン2が押されたよ"); 165 System.out.println("音楽スタート"); 166 plonkSound.play(); 167 theStage1.setScene(theScene4); 168 count = count + 1; 169 }else{ 170 theStage1.setScene(theScene3); 171 } 172 173 if(count == 3){ 174 count = 0; 175 num = 2; 176 System.out.println("ボタン1へ移動"); 177 } 178 } 179 180}

投稿2017/06/23 12:04

編集2017/06/23 16:08
nununuso

総合スコア9

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問