JavaFX的形状未显示?

huangapple go评论116阅读模式
英文:

JavaFX shapes not showing up?

问题

我需要使用文本文件中的指令来创建一张图片。我几乎完成了,但是其中有 2 个形状没有显示出来。链接 https://i.stack.imgur.com/WKxwK.jpg 上面的图片是我现在的情况,下面的图片是我需要的效果。如您所见,右上角的一个圆和构建火箭底座的矩形都没有显示出来。这是相关的代码部分:

  1. Group root = new Group(); //创建一个用于存放所有形状的组
  2. try {
  3. Scanner obj = new Scanner(new File("scene.txt")); //读取文本文件
  4. boolean shouldBreak = false;
  5. while(obj.hasNextLine()){
  6. String[] strArray = obj.nextLine().split(" "); //将每个命令拆分为新行
  7. switch(strArray[0]){
  8. case "SIZE": //设置窗口大小
  9. fwidth = Double.parseDouble(strArray[1]);
  10. fheight = Double.parseDouble(strArray[2]);
  11. break;
  12. case "LINE": //创建线段
  13. la = Double.parseDouble(strArray[1]);
  14. lb = Double.parseDouble(strArray[2]);
  15. lc = Double.parseDouble(strArray[3]);
  16. ld = Double.parseDouble(strArray[4]);
  17. Line line = new Line(la,lb,lc,ld);
  18. line.setStyle("-fx-stroke:rgb(127,244,16)");
  19. root.getChildren().add(line);
  20. break;
  21. case "CIRCLE": //创建圆形
  22. cx = Double.parseDouble(strArray[1]);
  23. cy = Double.parseDouble(strArray[2]);
  24. cr = Double.parseDouble(strArray[3]);
  25. Circle circle = new Circle(cx,cy,cr);
  26. circle.setFill(null);
  27. circle.setStyle("-fx-stroke:rgb(127,244,16)");
  28. root.getChildren().add(circle);
  29. break;
  30. case "RECTANGLE": //创建矩形
  31. rx = Double.parseDouble(strArray[1]);
  32. ry = Double.parseDouble(strArray[2]);
  33. rw = Double.parseDouble(strArray[3]);
  34. rh = Double.parseDouble(strArray[4]);
  35. Rectangle rect = new Rectangle(rx,ry,rw,rh);
  36. rect.setFill(null);
  37. rect.setStyle("-fx-stroke:rgb(127,244,16)");
  38. root.getChildren().add(rect);
  39. break;
  40. case "TEXT": //创建文本
  41. tx = Double.parseDouble(strArray[1]);
  42. ty = Double.parseDouble(strArray[2]);
  43. for(int i = 3; i < strArray.length; i++){
  44. ts = ts + " " + strArray[i];
  45. }
  46. Text text = new Text(tx,ty,ts);
  47. text.setStyle("-fx-stroke:rgb(127,244,16)");
  48. root.getChildren().add(text);
  49. ts = "";
  50. break;
  51. case "//": //忽略注释
  52. obj.nextLine();
  53. break;
  54. case "END": //停止读取文件
  55. shouldBreak = true;
  56. break;
  57. }
  58. if(shouldBreak){break;}
  59. }
  60. Scene scene = new Scene(root, fwidth, fheight, Color.BLACK);
  61. stage.setTitle("graphic");
  62. stage.setScene(scene);
  63. stage.show();
  64. }
  65. catch (FileNotFoundException fileNotFoundException) {
  66. fileNotFoundException.printStackTrace();
  67. }
  68. }
  69. /**
  70. * 主方法
  71. * @param args
  72. */
  73. public static void main(String[] args) {
  74. launch(args);
  75. }
  76. }

我在网上搜索了一下,但没有找到相关信息。如果有人知道为什么只有 2 个形状没有显示出来,我会非常感谢任何帮助。

英文:

I need to create an image using instructions from a text file. I have almost finished, but 2 of my shapes are not showing up. https://i.stack.imgur.com/WKxwK.jpg The top pic is what I have, and the bottom is what I need. As you can see, one of the circles in the top right is not showing and the rectangle building the base of the rocket isn't either. this is the relevant code:

  1. Group root = new Group(); //creates group for all shapes to go in
  2. try {
  3. Scanner obj = new Scanner(new File(&quot;scene.txt&quot;)); //reads text file
  4. boolean shouldBreak = false;
  5. while(obj.hasNextLine()){
  6. String[] strArray = obj.nextLine().split(&quot; &quot;); //splits all commands to new line
  7. switch(strArray[0]){
  8. case &quot;SIZE&quot;: //sets size of window
  9. fwidth = Double.parseDouble(strArray[1]);
  10. fheight = Double.parseDouble(strArray[2]);
  11. break;
  12. case &quot;LINE&quot;: //creates each line
  13. la = Double.parseDouble(strArray[1]);
  14. lb = Double.parseDouble(strArray[2]);
  15. lc = Double.parseDouble(strArray[3]);
  16. ld = Double.parseDouble(strArray[4]);
  17. Line line = new Line(la,lb,lc,ld);
  18. line.setStyle(&quot;-fx-stroke:rgb(127,244,16)&quot;);
  19. root.getChildren().add(line);
  20. break;
  21. case &quot;CIRCLE&quot;: //creates each circle
  22. cx = Double.parseDouble(strArray[1]);
  23. cy = Double.parseDouble(strArray[2]);
  24. cr = Double.parseDouble(strArray[3]);
  25. Circle circle = new Circle(cx,cy,cr);
  26. circle.setFill(null);
  27. circle.setStyle(&quot;-fx-stroke:rgb(127,244,16)&quot;);
  28. root.getChildren().add(circle);
  29. break;
  30. case &quot;RECTANGLE&quot;: //creates each rectangle
  31. rx = Double.parseDouble(strArray[1]);
  32. ry = Double.parseDouble(strArray[2]);
  33. rw = Double.parseDouble(strArray[3]);
  34. rh = Double.parseDouble(strArray[4]);
  35. Rectangle rect = new Rectangle(rx,ry,rw,rh);
  36. rect.setFill(null);
  37. rect.setStyle(&quot;-fx-stroke:rgb(127,244,16)&quot;);
  38. root.getChildren().add(rect);
  39. break;
  40. case &quot;TEXT&quot;: //creates each string of text
  41. tx = Double.parseDouble(strArray[1]);
  42. ty = Double.parseDouble(strArray[2]);
  43. for(int i = 3; i &lt; strArray.length; i++){
  44. ts = ts + &quot; &quot; + strArray[i];
  45. }
  46. Text text = new Text(tx,ty,ts);
  47. text.setStyle(&quot;-fx-stroke:rgb(127,244,16)&quot;);
  48. root.getChildren().add(text);
  49. ts = &quot;&quot;;
  50. break;
  51. case &quot;//&quot;: //ignores comments
  52. obj.nextLine();
  53. break;
  54. case &quot;END&quot;: //stops reading file
  55. shouldBreak = true;
  56. break;
  57. }
  58. if(shouldBreak){break;}
  59. }
  60. Scene scene = new Scene(root, fwidth, fheight, Color.BLACK);
  61. stage.setTitle(&quot;graphic&quot;);
  62. stage.setScene(scene);
  63. stage.show();
  64. }
  65. catch (FileNotFoundException fileNotFoundException) {
  66. fileNotFoundException.printStackTrace();
  67. }
  68. }
  69. /**
  70. * The main method
  71. * @param args
  72. */
  73. public static void main(String[] args) {
  74. launch(args);
  75. }
  76. }

i've looked online and can't find anything about it. if anyone knows why just 2 of the shapes aren't appearing, any help is appreciated.

答案1

得分: 1

以下是翻译好的内容:

  1. case "//": //忽略注释
  2. obj.nextLine();
  3. break;
  1. case "//": //忽略注释
  2. break;
英文:
  1. case &quot;//&quot;: //ignores comments
  2. obj.nextLine();
  3. break;

You are causing it to skip an additional instruction line here. I'm assuming this is whats making you skip certain shapes. Below is what it should be.

  1. case &quot;//&quot;: //ignores comments
  2. break;

huangapple
  • 本文由 发表于 2020年10月18日 22:35:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64414536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定