1

How can I make a spinning line with animations in JavaFX? As you know, if this is done in C or C++, it is just looping, goto (x,y), than sleep. But, in JavaFX, I draw a line in a loop, but nothing happens while its running. Can somebody help me?

package javafxapplication1;

import javafx.stage.Stage;
import java.util.*;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.EllipseBuilder;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.scene.shape.*;

public class JavaFXApplication1 extends Application { 

  public static void main(String[] args) {
    launch(args);
  }

  public class Canvas
  {
   private int width;
   private int height;
   private int stride;
   private byte[] im;
   private com.sun.prism.Image pimage;

   public Canvas(int width, int height)
   {
      this.width = width;
      this.height = height;
      this.stride = width*3;
      im = new byte[stride*height];
   } 
}


  @Override
  public void start(Stage primaryStage) {
      Group root = new Group();
      Scene scene = new Scene(root, 800, 600, Color.WHITE);

  Ellipse lingkaran = EllipseBuilder.create()
    .centerX(400)
    .centerY(300)
    .radiusX(210)
    .radiusY(210)
    .strokeWidth(3)
    .build();

  Path path = new Path();      

  MoveTo moveTo = new MoveTo();
  moveTo.setX(400);
  moveTo.setY(300);

  LineTo lineTo = new LineTo();
  lineTo.setX(400);
  lineTo.setY(100);

  path.getElements().add(moveTo);
  path.getElements().add(lineTo);
  path.setStrokeWidth(3);
  path.setStroke(Color.BLACK);

  int x1 = 400;
  int y1 = 300;
  int x2 = 400;
  int y2 = 100;
  double rr = 0;
  double dx, temp1;
  double dy, temp2;

  temp1 = x2;
  temp2 = y2;
  //root.getChildren().add(lingkaran);

  for(int i = 0; i < 500; i++){              
      Line line = new Line();
      Ellipse ellipse = new Ellipse();

      ellipse.setCenterX(400);
      ellipse.setCenterY(300);
      ellipse.setRadiusX(210);
      ellipse.setRadiusY(210);
      ellipse.setStroke(Color.BLACK);
      ellipse.setFill(Color.WHITE);

      line.setStartX(x1);
      line.setStartY(y1);          

      line.setStroke(Color.RED);          

      dx = ((x2-x1)*Math.cos(rr)) - ((y2-y1)*Math.sin(rr)) +x1;
      dy = ((x2-x1)*Math.sin(rr)) + ((y2-y1)*Math.cos(rr)) +y1;

      temp1 = dx;
      temp2 = dy;

      line.setEndX(dx);
      line.setEndY(dy);

      rr = rr + 0.05;

      root.getChildren().add(ellipse); 

      root.getChildren().add(line);  
  }    

   primaryStage.setScene(scene);
   primaryStage.show();


  }
}
Reid Spencer
  • 2,776
  • 28
  • 37
Yohanim
  • 3,319
  • 9
  • 52
  • 92

1 Answers1

1

Use a RotateTransition or Animation Timeline with KeyFrames instead.

See How to draw a clock with JavaFX 2? for instructions and sample code which rotates a line using JavaFX.

Your code doesn't work because you have to give control back to the JavaFX system so that it can perform the actual rendering. Also, directly referencing com.sun classes in your code is ill advised because those classes may change API in future JavaFX versions, compromising the compatibility of your code.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406