Issue
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://imgur.com/a/KptnvOD 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.
Solution
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;
Answered By - wyit