java - 旋转对象的 Alpha 渐变 - JavaFX Canvas

标签 java javafx rotation gradient

下面的可运行示例中的箭头应该用从箭头底部到箭头头部的红色 alpha 渐变填充。

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();
    }

}

我一定错过了一些明显的东西!?

感谢您的关注!

Stackoverflow 希望我添加更多详细信息,因为“主要是代码”,但我真的不知道要添加什么。 :-)

最佳答案

弄清楚了 - 我完全误读了有关比例变量的文档,抱歉。

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

关于java - 旋转对象的 Alpha 渐变 - JavaFX Canvas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46056291/

相关文章:

java - 是否可以在使用 Java 9 的 Eclipse IDE 中使用没有类和主要方法的 sysout?

java - 检测所有小部件的 Android 实例?

php - 如果旋转正在移动则停止视频

javascript - 使用 ekko-lightbox 旋转图像

java - 如何在 Java 中旋转 BufferedImage?

java - Spring 一号的 BOF

java - 将字符串日期转换为 ISO 格式日期

JavaFX:如何将 Button 属性绑定(bind)到线程状态?

java - 如何获取List子类的REAL类型参数?

JavaFx Group 内容位置在转换后发生变化