Search code examples
javafxpolyline

Is there a way to shift something in a method without explicitly editing the method's code?


For instance, I have a class in which I wrote a method that creates a Polyline object. Notice how I have solution.setTranslateY(315), which shifts the Polyline down in my javafx window.

public Polyline getSolution()
{
    Polyline solution = new Polyline();
    solution.setStrokeWidth(3);

    for (int i = 0; i < coordinates; i = i + 2)
        solution.getPoints().addAll(puzzle[i], puzzle[i + 1]);      

    solution.setTranslateY(315);

    //translates this solution farther down the display window

    return solution;
}    

As you can see, I implement this "getSolution()" method on a created object by putting it in a Group and then adding that Group to the scene of my program.

 public void start(Stage primaryStage) throws FileNotFoundException
 {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the name of the input file: ");
    String fileName = input.nextLine();                        
    ConnectTheDotsPuzzle heart = new ConnectTheDotsPuzzle(fileName);
    // When the user inputs the file, it gets sent to the constructor

    Line blueLine = new Line(0, 300, 500, 300); 
    //line between puzzle and solution
    blueLine.setStroke(Color.BLUE);
    blueLine.setStrokeWidth(3);

    Text solutionText = new Text(20, 335, "Solution:");
    solutionText.setFont(Font.font("Times", FontWeight.BOLD, 24));

    Group group = new Group(heart.getPuzzle(), heart.getSolution(),
            blueLine, solutionText);
    // grouping everything together to put in the scene

    Scene scene = new Scene(group, 500, 
            300);                                    
    primaryStage.setTitle("Connect the Dots");
    primaryStage.setScene(scene);
    primaryStage.show();        
    primaryStage.setOnCloseRequest(e -> System.exit(0));
 }

My question is: Is there a way so that I don't have to translate my Polyline object in the initial method I created? How can I shift it down in my start method? If I wanted to create multiple objects of this and didn't always want it to be 315 down, I was wondering how I would be able to change it in my start method as opposed to having it be a constant shift in my method.


Solution

  • One way you could make it easier to customize would be to take in the downshift as an argument for the getSolution() method. That or, above the Group declaration, you could say

    Polyline solution = heart.getSolution();
    solution.setTranslateY(315);
    

    Then replace getSolution() with solution in the Group declaration.