JavaFX 形状没有显示?
JavaFX shapes not showing up?
我需要使用文本文件中的说明创建图像。我快完成了,但是我的两个形状没有出现。 https://imgur.com/a/KptnvOD 上图是我有的,下图是我需要的。如您所见,右上角的一个圆圈没有显示,构建火箭底部的矩形也没有显示。这是相关代码:
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);
}
}
我在网上看过,找不到任何相关信息。如果有人知道为什么只有 2 个形状没有出现,我们将不胜感激。
case "//": //ignores comments
obj.nextLine();
break;
你让它在这里跳过了一个额外的指令行。我假设这是让你跳过某些形状的原因。下面是它应该是什么。
case "//": //ignores comments
break;
我需要使用文本文件中的说明创建图像。我快完成了,但是我的两个形状没有出现。 https://imgur.com/a/KptnvOD 上图是我有的,下图是我需要的。如您所见,右上角的一个圆圈没有显示,构建火箭底部的矩形也没有显示。这是相关代码:
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);
}
}
我在网上看过,找不到任何相关信息。如果有人知道为什么只有 2 个形状没有出现,我们将不胜感激。
case "//": //ignores comments
obj.nextLine();
break;
你让它在这里跳过了一个额外的指令行。我假设这是让你跳过某些形状的原因。下面是它应该是什么。
case "//": //ignores comments
break;