英文:
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("scene.txt")); //reads text file
boolean shouldBreak = false;
while(obj.hasNextLine()){
String[] strArray = obj.nextLine().split(" "); //splits all commands to new line
switch(strArray[0]){
case "SIZE": //sets size of window
fwidth = Double.parseDouble(strArray[1]);
fheight = Double.parseDouble(strArray[2]);
break;
case "LINE": //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("-fx-stroke:rgb(127,244,16)");
root.getChildren().add(line);
break;
case "CIRCLE": //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("-fx-stroke:rgb(127,244,16)");
root.getChildren().add(circle);
break;
case "RECTANGLE": //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("-fx-stroke:rgb(127,244,16)");
root.getChildren().add(rect);
break;
case "TEXT": //creates each string of 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 "//": //ignores comments
obj.nextLine();
break;
case "END": //stops reading file
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();
}
}
/**
* 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 "//": //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 "//": //ignores comments
break;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论