2012-04-08 22 views
0

Ich versuche, ein JavaFX-Steuerelement mit PathTransition zu überführen. In der Ausführung bewegt sich das Steuerelement unregelmäßig (ich habe es für ein Rechteck arbeiten lassen).JavaFX Animation Fehler beim Übergang Steuerelement

Das angehängte Beispiel versucht, ein Label basierend auf den Positionen von zwei anderen Labels von einer Position zu einer anderen zu verschieben. Das Etikett springt sofort zu einem Punkt unter seinem angenommenen Startpunkt und springt dann zu einem anderen Punkt unterhalb seines beabsichtigten Zielorts.

Die Debug-Info zeigt an, dass die Beschriftung bei (50, 50) mit einem Offset von (0, 0) beginnt und auf eine neue Position von (200, 50) abzielt. Wenn die Animation abgeschlossen ist, glaube ich, dass sie an der Position (50, 50) mit einem Offset von (150, 0) sein sollte; in der Tat hat es eine Position von (50, 50) mit einem Offset von (148, 38,5).

Was mache ich falsch?

Danke für die Hilfe.

package fxsort; 

import javafx.animation.PathTransition; 
import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.ArcTo; 
import javafx.scene.shape.MoveTo; 
import javafx.scene.shape.Path; 
import javafx.scene.text.Font; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class Test4 
    extends Application 
{ 
    private final Label statLabel1  = 
     getLabel("Jehosaphat", 50, 50, Color.gray(.85)); 
    private final Label statLabel2  = 
     getLabel("Jehosaphat", 200, 50, Color.gray(.85)); 
    private final Label periLabel = 
     getLabel("Jehosaphat", 50, 50, Color.RED); 

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

@Override 
public void start(Stage stage) throws Exception 
{ 
    Button start    = new Button("Start"); 

    start.relocate(250, 125); 
    start.setOnAction(
     new EventHandler<ActionEvent>() 
     { 
      @Override 
      public void handle(ActionEvent evt) 
      { 
       PathTransition transition = getTransition(); 
       transition.play(); 
      } 
     } 
    ); 

    Group root = 
     new Group(statLabel1, statLabel2, periLabel, start); 
    stage.setScene(new Scene(root, 350, 200)) ; 
    stage.show(); 
} 

private Label 
getLabel(String text, double xco, double yco, Color color) 
{ 
    Label label = new Label(text); 
    label.relocate(xco, yco); 
    label.setTextFill(color); 
    label.setFont(Font.font("Arial", 20)); 

    return label; 
} 
private PathTransition 
getTransition() 
{ 
    Label  from  = statLabel1; 
    Label  too   = statLabel2; 
    Duration duration = Duration.millis(3000); 
    double  startX  = from.getLayoutX();// + 30; 
    double  startY  = from.getLayoutY();// + 30; 
    System.out.println("From Layout: " + startX + ", " + startY); 
    System.out.println("From Offset: " + from.getTranslateX() 
         + ", " + from.getTranslateX()); 

    double  endX  = too.getLayoutX();// + 30; 
    double  endY  = too.getLayoutY();// + 30; 
    System.out.println("To Layout: " + endX + ", " + endY); 
    System.out.println("To Offset: " + too.getTranslateX() 
         + ", " + too.getTranslateX()); 

    MoveTo  start  = new MoveTo(startX, startY); 
    System.out.println("MoveTo: " + start.getX() + ", " + start.getY()); 

    ArcTo  end  = new ArcTo(); 
    end.setX(endX); 
    end.setY(endY); 
    end.setRadiusX(100); 
    end.setRadiusY(100); 
    System.out.println("ArcTo: " + end.getX() + ", " + end.getY()); 

    System.out.println("**********"); 

    Path path = new Path(); 
    path.getElements().addAll(start, end); 

    PathTransition transition = new PathTransition(); 
    transition.setDuration(duration); 
    transition.setPath(path); 
    transition.setNode(periLabel); 

    transition.setOnFinished(
     new EventHandler<ActionEvent>() 
     { 
      public void handle(ActionEvent evt) 
      { 
       System.out.println("Landed: " + periLabel.getLayoutX() 
            + ", " + periLabel.getLayoutY()); 
       System.out.println("Offset: " + periLabel.getTranslateX() 
            + ", " + periLabel.getTranslateY()); 
      } 
     } 
    ); 

    return transition; 
} 
} 

Antwort

1

Es gibt zwei Probleme mit Ihnen Code:

  1. PathTransition bewegt sich Knoten in einem eigenen Koordinatensystem. Wenn Ihr Knoten also layoutX/Y auf 50,50 gesetzt hat, wie es periLabel tut, wird es verschoben.

  2. PathTransition verschiebt Knoten nach Knotenmitte, nicht nach oben links.

Next Code Ihrer Probleme beheben:

private PathTransition getTransition() { 
    Label from = statLabel1; 
    Label too = statLabel2; 
    Duration duration = Duration.millis(3000); 

    // fix problem (1) 
    periLabel.relocate(0, 0); 
    // fix problem (2) 
    double startX = from.getLayoutX() + from.getBoundsInLocal().getWidth()/2; 
    double startY = from.getLayoutY() + from.getBoundsInLocal().getHeight()/2; 

    double endX = too.getLayoutX() + too.getBoundsInLocal().getWidth()/2; 
    double endY = too.getLayoutY() + too.getBoundsInLocal().getHeight()/2; 
+0

Perfect, danke. –

Verwandte Themen