JavaFX形状没有显示?
我需要使用文本文件中的说明创建图像。我几乎完成了,但是我的两个形状没有显示出来。上面的图片是我拥有的,下面的图片是我需要的。如你所见,右上角的一个圆圈没有显示,而构成火箭底座的矩形也没有显示。以下是相关代码:JavaFX形状没有显示?,java,javafx,java.util.scanner,Java,Javafx,Java.util.scanner,我需要使用文本文件中的说明创建图像。我几乎完成了,但是我的两个形状没有显示出来。上面的图片是我拥有的,下面的图片是我需要的。如你所见,右上角的一个圆圈没有显示,而构成火箭底座的矩形也没有显示。以下是相关代码: Group root = new Group(); //creates group for all shapes to go in try { Scanner obj = new Scanner(new File("scene.txt&q
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);
}
}
组根=新组()//为所有要进入的形状创建组
试一试{
Scanner obj=new Scanner(新文件(“scene.txt”);//读取文本文件
布尔值shouldBreak=false;
while(obj.hasNextLine()){
String[]strArray=obj.nextLine().split(“”;//将所有命令拆分为新行
开关(strArray[0]){
大小写“SIZE”://设置窗口的大小
fwidth=Double.parseDouble(strArray[1]);
fheight=Double.parseDouble(strArray[2]);
打破
案例“行”://创建每一行
la=Double.parseDouble(strArray[1]);
lb=Double.parseDouble(strArray[2]);
lc=Double.parseDouble(strArray[3]);
ld=Double.parseDouble(strArray[4]);
直线=新线(la、lb、lc、ld);
行。设置样式(“-fx笔划:rgb(127244,16)”);
root.getChildren().add(行);
打破
案例“圆”://创建每个圆
cx=Double.parseDouble(strArray[1]);
cy=Double.parseDouble(strArray[2]);
cr=Double.parseDouble(strArray[3]);
圆圈=新圆圈(cx、cy、cr);
circle.setFill(null);
圆圈。设置样式(“-fx笔划:rgb(127244,16)”;
root.getChildren().add(圆);
打破
大小写“矩形”://创建每个矩形
rx=Double.parseDouble(strArray[1]);
ry=Double.parseDouble(strArray[2]);
rw=Double.parseDouble(strArray[3]);
rh=Double.parseDouble(strArray[4]);
矩形rect=新矩形(rx、ry、rw、rh);
rect.setFill(null);
矩形设置样式(“-fx笔划:rgb(127244,16)”;
root.getChildren().add(rect);
打破
大小写“TEXT”://创建每个文本字符串
tx=Double.parseDouble(strArray[1]);
ty=Double.parseDouble(strArray[2]);
对于(int i=3;i
我在网上查过了,找不到关于它的任何东西。如果有人知道为什么只有两个形状没有出现,我们将不胜感激
case "//": //ignores comments
obj.nextLine();
break;
您正在使它跳过此处的附加指令行。我想这就是让你跳过某些形状的原因。下面是它应该是什么
case "//": //ignores comments
break;
不要转发问题。。。。。相反,编辑第一个,并澄清为什么它的答案没有帮助