JavaFX的形状未显示?

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

JavaFX shapes not showing up?

问题

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

Group root = new Group();  //创建一个用于存放所有形状的组
try {
    Scanner obj = new Scanner(new File("scene.txt"));  //读取文本文件
    boolean shouldBreak = false;
    while(obj.hasNextLine()){
        String[] strArray = obj.nextLine().split(" ");  //将每个命令拆分为新行

        switch(strArray[0]){
            case "SIZE":                                      //设置窗口大小
                fwidth = Double.parseDouble(strArray[1]);
                fheight = Double.parseDouble(strArray[2]);
                break;

            case "LINE":                                      //创建线段
                la = Double.parseDouble(strArray[1]);
                lb = Double.parseDouble(strArray[2]);
                lc = Double.parseDouble(strArray[3]);
                ld = Double.parseDouble(strArray[4]);
                Line line = new Line(la,lb,lc,ld);
                line.setStyle("-fx-stroke:rgb(127,244,16)");
                root.getChildren().add(line);
                break;

            case "CIRCLE":                                    //创建圆形
                cx = Double.parseDouble(strArray[1]);
                cy = Double.parseDouble(strArray[2]);
                cr = Double.parseDouble(strArray[3]);
                Circle circle = new Circle(cx,cy,cr);
                circle.setFill(null);
                circle.setStyle("-fx-stroke:rgb(127,244,16)");
                root.getChildren().add(circle);
                break;

            case "RECTANGLE":                                 //创建矩形
                rx = Double.parseDouble(strArray[1]);
                ry = Double.parseDouble(strArray[2]);
                rw = Double.parseDouble(strArray[3]);
                rh = Double.parseDouble(strArray[4]);
                Rectangle rect = new Rectangle(rx,ry,rw,rh);
                rect.setFill(null);
                rect.setStyle("-fx-stroke:rgb(127,244,16)");
                root.getChildren().add(rect);
                break;

            case "TEXT":                                      //创建文本
                tx = Double.parseDouble(strArray[1]);
                ty = Double.parseDouble(strArray[2]);
                for(int i = 3; i < strArray.length; i++){
                    ts = ts + " " + strArray[i];
                }
                Text text = new Text(tx,ty,ts);
                text.setStyle("-fx-stroke:rgb(127,244,16)");
                root.getChildren().add(text);
                ts = "";
                break;

            case "//":                                        //忽略注释
                obj.nextLine();
                break;

            case "END":                                       //停止读取文件
                shouldBreak = true;
                break;
        }
        if(shouldBreak){break;}
    }

    Scene scene = new Scene(root, fwidth, fheight, Color.BLACK);
    stage.setTitle("graphic");
    stage.setScene(scene);
    stage.show();
}
catch (FileNotFoundException fileNotFoundException) {
    fileNotFoundException.printStackTrace();
}
}

/**
 * 主方法
 * @param args
 */

public static void main(String[] args) {
    launch(args);
}
}

我在网上搜索了一下,但没有找到相关信息。如果有人知道为什么只有 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:

Group root = new Group();  //creates group for all shapes to go in
try {
Scanner obj = new Scanner(new File(&quot;scene.txt&quot;));  //reads text file
boolean shouldBreak = false;
while(obj.hasNextLine()){
String[] strArray = obj.nextLine().split(&quot; &quot;);  //splits all commands to new line
switch(strArray[0]){
case &quot;SIZE&quot;:                                      //sets size of window
fwidth = Double.parseDouble(strArray[1]);
fheight = Double.parseDouble(strArray[2]);
break;
case &quot;LINE&quot;:                                      //creates each line
la = Double.parseDouble(strArray[1]);
lb = Double.parseDouble(strArray[2]);
lc = Double.parseDouble(strArray[3]);
ld = Double.parseDouble(strArray[4]);
Line line = new Line(la,lb,lc,ld);
line.setStyle(&quot;-fx-stroke:rgb(127,244,16)&quot;);
root.getChildren().add(line);
break;
case &quot;CIRCLE&quot;:                                    //creates each circle
cx = Double.parseDouble(strArray[1]);
cy = Double.parseDouble(strArray[2]);
cr = Double.parseDouble(strArray[3]);
Circle circle = new Circle(cx,cy,cr);
circle.setFill(null);
circle.setStyle(&quot;-fx-stroke:rgb(127,244,16)&quot;);
root.getChildren().add(circle);
break;
case &quot;RECTANGLE&quot;:                                 //creates each rectangle
rx = Double.parseDouble(strArray[1]);
ry = Double.parseDouble(strArray[2]);
rw = Double.parseDouble(strArray[3]);
rh = Double.parseDouble(strArray[4]);
Rectangle rect = new Rectangle(rx,ry,rw,rh);
rect.setFill(null);
rect.setStyle(&quot;-fx-stroke:rgb(127,244,16)&quot;);
root.getChildren().add(rect);
break;
case &quot;TEXT&quot;:                                      //creates each string of text
tx = Double.parseDouble(strArray[1]);
ty = Double.parseDouble(strArray[2]);
for(int i = 3; i &lt; strArray.length; i++){
ts = ts + &quot; &quot; + strArray[i];
}
Text text = new Text(tx,ty,ts);
text.setStyle(&quot;-fx-stroke:rgb(127,244,16)&quot;);
root.getChildren().add(text);
ts = &quot;&quot;;
break;
case &quot;//&quot;:                                        //ignores comments
obj.nextLine();
break;
case &quot;END&quot;:                                       //stops reading file
shouldBreak = true;
break;
}
if(shouldBreak){break;}
}
Scene scene = new Scene(root, fwidth, fheight, Color.BLACK);
stage.setTitle(&quot;graphic&quot;);
stage.setScene(scene);
stage.show();
}
catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
}
/**
* The main method
* @param args
*/
public static void main(String[] args) {
launch(args);
}
}

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

以下是翻译好的内容:

                        case "//":            //忽略注释
                        obj.nextLine();
                        break;
                        case "//":            //忽略注释
                        break;
英文:
                    case &quot;//&quot;:            //ignores comments
obj.nextLine();
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.

                    case &quot;//&quot;:            //ignores comments
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:

确定