2017-09-05 1 views
1

Die Pfeile im ausführbaren Beispiel unten sollten jeweils mit einem roten Alpha-Gradienten gefüllt werden, der von der Basis des Pfeils zum Kopf führt.Alpha-Gradient für gedrehtes Objekt - JavaFX Canvas

import javafx.application.Application; 
import javafx.geometry.Point2D; 
import javafx.scene.Scene; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.canvas.GraphicsContext; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.paint.Color; 
import javafx.scene.paint.CycleMethod; 
import javafx.scene.paint.LinearGradient; 
import javafx.scene.paint.Stop; 
import javafx.stage.Stage; 

public class JavaFXAlphaGradient extends Application { 

    public static void main(String... args) { 
     Application.launch(JavaFXAlphaGradient.class, args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     double size = 600.0; 
     BorderPane pane = new BorderPane(); 
     Canvas canvas = new Canvas(); 
     pane.getChildren().add(canvas); 
     canvas.setHeight(size); 
     canvas.setWidth(size); 
     Point2D midPoint = new Point2D(canvas.getWidth()/2, canvas.getHeight()/2); 
     Point2D topLeft = new Point2D(0.0, 0.0); 
     Point2D topRight = new Point2D(canvas.getWidth(), 0.0); 
     Point2D bottomLeft = new Point2D(0.0, canvas.getHeight()); 
     Point2D bottomRight = new Point2D(canvas.getWidth(), canvas.getHeight()); 
     drawArrow(canvas, midPoint, topLeft); 
     drawArrow(canvas, midPoint, topRight); 
     drawArrow(canvas, midPoint, bottomLeft); 
     drawArrow(canvas, midPoint, bottomRight); 
     Scene scene = new Scene(pane, size, size); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private void drawArrow(Canvas canvas, Point2D from, Point2D to) { 
     GraphicsContext gc = canvas.getGraphicsContext2D(); 
     double distance = from.distance(to); 
     double arrowWidth = 30.0; 
     double arrowheadSide = 30.0; 
     gc.save(); 
     Point2D rotationPoint = new Point2D((from.getX() + to.getX())/2, (from.getY() + to.getY())/2); 
     gc.translate(rotationPoint.getX(), rotationPoint.getY()); 
     double theta = Math.atan2(from.getY() - to.getY(), from.getX() - to.getX()); 
     gc.rotate(180 + Math.toDegrees(theta)); 
     gc.beginPath(); 
     gc.moveTo(-distance/2, arrowWidth/2); 
     gc.lineTo(distance/2 - arrowWidth, arrowWidth/2); 
     gc.lineTo(distance/2 - arrowWidth, arrowWidth/2); 
     gc.lineTo(distance/2, 0.0); 
     gc.lineTo(distance/2 - arrowWidth, -arrowheadSide/2); 
     gc.lineTo(distance/2 - arrowWidth, -arrowWidth/2); 
     gc.lineTo(-distance/2, -arrowWidth/2); 
     gc.closePath(); 
     Color red03 = new Color(1.0, 0.0, 0.0, 0.3); 
     Color red08 = new Color(1.0, 0.0, 0.0, 0.8); 
     // I want an alpha gradient for each arrow from "from" to "to" - this doesn't work though 
     gc.setFill(new LinearGradient(from.getX(), from.getY(), to.getX(), to.getY(), true, 
       CycleMethod.NO_CYCLE, new Stop(0.0, red03), new Stop(1.0, red08))); 
     gc.fill(); 
     gc.restore(); 
    } 

} 

Ich muss etwas offensichtlich fehlen !?

Vielen Dank für den Blick!

Stackoverflow möchte, dass ich mehr Details wegen "meist Code" hinzufüge, aber ich weiß wirklich nicht, was ich hinzufügen soll. :-)

Antwort

1

Ich habe es herausgefunden - ich habe die Dokumentation über die proportionale Variable komplett falsch gelesen, sorry.

gc.setFill(new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, 
    new Stop(0.0, red03), new Stop(1.0, red08)));