0

I want to change the size of a line using a button so later I can make the line look like it is rotating... Here is the code I have so far:

package JavaFXApplication14;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class JavaFXApplication14 extends Application 
{
    public static void main(String[] args) 
    {
        launch(args);
    }

    int x = 200;

    @Override public void start(Stage primaryStage) throws Exception 
    {
        final GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER); 
        grid.setHgap(100);
        grid.setVgap(100);
        grid.setPadding(new Insets(10, 10, 10, 10));

        Scene scene = new Scene(grid, 600, 400); //Color.BLACK ?
        primaryStage.setScene(scene);
        primaryStage.setTitle("4D");
        primaryStage.show();

        Line ln = new Line(100, 200, x, 200); 
        ln.setStroke(Color.BLUE);
        ln.setStrokeWidth(5);
        grid.add(ln, 0, 0);

        Button btn = new Button("X-Y");
        grid.setHalignment(btn, HPos.CENTER);
        btn.setOnAction(e -> btn_Click());
        grid.add(btn, 0, 1);
    }

    public void btn_Click()
    {
        x = x + 50;
    }
}

Also, sometimes when I use the following line of code the color of the background does not change.

Scene scene = new Scene(grid, 600, 400, Color.BLACK);

What is the reason for that?

  • If you want a rotation it is not the size that changes but rather the position (`x1,y1`) or (`x2,y2`) the first one represents the `centerX `& `centerY` and the other the `circumference` ! – Bo Halim Dec 24 '16 at 16:14
  • I know but the button does not work :( – random dude Dec 24 '16 at 16:38

1 Answers1

0

The button works very well you can test it, but here you only set a new value to x and nothing else, in other words you don't update the position x2 of your Line.You can dot that by making your variable ln accessible and updating its value EndX:

//before the method start
Line ln;
//Inside your btn_click() method add
ln.setEndX(x);

But it will only increase the size of your line (Horizontally) and not rotate it. with a little research explained here and following what I told you in the comment you can do this easily based on the axis of rotation (startX & startY) and the points to be rotated (endX & endY):

public class Launcher extends Application{

private Pane root = new Pane();
private Scene scene;
private Button btn = new Button("Test");
private Line line;

@Override
public void start(Stage stage) throws Exception {

    line = new Line(200,200,200,100);
    line.setStroke(Color.BLACK);
    line.setStrokeWidth(5);  
    btn.setOnAction(e -> rotate()); 
    root.getChildren().addAll(btn,line);
    scene = new Scene(root,500,500);
    stage.setScene(scene);
    stage.show();

}


private void rotate(){


    double x1 = line.getEndX() - line.getStartX();
    double y1 = line.getEndY() - line.getStartY();

    //The more you reduce the angle the longer the rotation
    double x2 = x1 * Math.cos(0.1) - y1 * Math.sin(0.1); 
    double y2 = x1 * Math.sin(0.1) + y1 * Math.cos(0.1);

    line.setEndX(x2+ line.getStartX());
    line.setEndY(y2+ line.getStartY()); 

}


  public static void main(String[] args) {

    launch(args); 

  }
}

Note: In your example you use a GridPane, so you will be restricted to its functioning, Good luck !

Community
  • 1
  • 1
Bo Halim
  • 1,716
  • 2
  • 17
  • 23