Search in sources :

Example 6 with LinearGradient

use of javafx.scene.paint.LinearGradient in project FXyzLib by Birdasaur.

the class SimpleRayTest method start.

@Override
public void start(Stage primaryStage) throws Exception {
    // add cameraTransform so it doesn't affect all nodes
    rayLight.getScope().add(cameraTransform);
    camera = new PerspectiveCamera(true);
    cameraTransform.setTranslate(0, 0, 0);
    cameraTransform.getChildren().addAll(camera);
    camera.setNearClip(0.1);
    camera.setFarClip(1000000.0);
    camera.setFieldOfView(42);
    camera.setTranslateZ(-5000);
    PointLight light = new PointLight(Color.GAINSBORO);
    PointLight light2 = new PointLight(Color.YELLOW);
    light2.setTranslateY(-2000);
    //create a target
    target1 = new Sphere(100);
    target1.setTranslateX(300);
    target1.setTranslateY(300);
    target1.setTranslateZ(1000);
    target1.setMaterial(red);
    // create another target
    target2 = new Sphere(100);
    target2.setTranslateX(800);
    target2.setTranslateY(-1200);
    target2.setTranslateZ(-500);
    target2.setMaterial(blue);
    root.getChildren().addAll(cameraTransform, target1, target2, light, light2, rayLight);
    root.setAutoSizeChildren(false);
    Scene scene = new Scene((root), 1200, 800, true, SceneAntialiasing.BALANCED);
    scene.setCamera(camera);
    Stop[] stops = new Stop[] { new Stop(0, Color.BLACK), new Stop(0.5, Color.DEEPSKYBLUE.darker()), new Stop(1.0, Color.BLACK) };
    LinearGradient lg = new LinearGradient(0, 0, 0, 1, true, CycleMethod.NO_CYCLE, stops);
    scene.setFill(lg);
    //First person shooter keyboard movement
    scene.setOnKeyPressed(ke -> {
        double change = 10.0;
        if (ke.isShiftDown()) {
            change = 50.0;
        }
        KeyCode keycode = ke.getCode();
        if (keycode == KeyCode.W) {
            camera.setTranslateZ(camera.getTranslateZ() + change);
        }
        if (keycode == KeyCode.S) {
            camera.setTranslateZ(camera.getTranslateZ() - change);
        }
        if (keycode == KeyCode.A) {
            camera.setTranslateX(camera.getTranslateX() - change);
        }
        if (keycode == KeyCode.D) {
            camera.setTranslateX(camera.getTranslateX() + change);
        }
        if (keycode == KeyCode.CONTROL) {
            fireRay = false;
        }
    });
    scene.setOnKeyReleased(ke -> {
        if (ke.getCode().equals(KeyCode.CONTROL)) {
            fireRay = true;
        }
    });
    scene.setOnMousePressed(e -> {
        mousePosX = e.getSceneX();
        mousePosY = e.getSceneY();
        mouseOldX = e.getSceneX();
        mouseOldY = e.getSceneY();
        if (fireRay) {
            Point3D o = e.getPickResult().getIntersectedPoint();
            if (e.isPrimaryButtonDown()) {
                Point3D t = Point3D.ZERO.add(target2.getTranslateX(), target2.getTranslateY(), target2.getTranslateZ()), d = t.subtract(o);
                Ray r = new Ray(o, d);
                double dist = t.distance(o);
                if (target2.getBoundsInParent().contains(r.project(dist))) {
                    animateRayTo(r, target2, Duration.seconds(2));
                    System.out.println("Target Contains Ray!\n" + r + "\nTarget Bounds: " + target2.getBoundsInParent() + "\nDistance: " + dist + "\n");
                }
                e.consume();
            } else if (e.isSecondaryButtonDown()) {
                Point3D tgt = Point3D.ZERO.add(target1.getTranslateX(), target1.getTranslateY(), target1.getTranslateZ()), dir = tgt.subtract(o);
                Ray r = new Ray(o, dir);
                double dist = tgt.distance(o);
                if (target1.getBoundsInParent().contains(r.project(dist))) {
                    animateRayTo(r, target1, Duration.seconds(2));
                    System.out.println("Target Contains Ray: " + target1.getBoundsInParent().contains(r.project(dist)) + "\n" + r + "\nTarget Bounds: " + target1.getBoundsInParent() + "\nDistance: " + dist + "\n");
                }
                e.consume();
            }
        }
    });
    scene.setOnMouseDragged(e -> {
        if (!fireRay) {
            mouseOldX = mousePosX;
            mouseOldY = mousePosY;
            mousePosX = e.getSceneX();
            mousePosY = e.getSceneY();
            mouseDeltaX = (mousePosX - mouseOldX);
            mouseDeltaY = (mousePosY - mouseOldY);
            double modifier = 10.0;
            double modifierFactor = 0.1;
            if (e.isControlDown()) {
                modifier = 0.1;
            }
            if (e.isShiftDown()) {
                modifier = 50.0;
            }
            if (e.isPrimaryButtonDown()) {
                cameraTransform.ry.setAngle(((cameraTransform.ry.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180);
                cameraTransform.rx.setAngle(((cameraTransform.rx.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180);
            } else if (e.isSecondaryButtonDown()) {
                double z = camera.getTranslateZ();
                double newZ = z + mouseDeltaX * modifierFactor * modifier;
                camera.setTranslateZ(newZ);
            } else if (e.isMiddleButtonDown()) {
                cameraTransform.t.setX(cameraTransform.t.getX() + mouseDeltaX * modifierFactor * modifier * 0.3);
                cameraTransform.t.setY(cameraTransform.t.getY() + mouseDeltaY * modifierFactor * modifier * 0.3);
            }
        }
    });
    primaryStage.setTitle("Hello Ray! Animated Visual of a Ray casting");
    primaryStage.setScene(scene);
    primaryStage.show();
}
Also used : Sphere(javafx.scene.shape.Sphere) LinearGradient(javafx.scene.paint.LinearGradient) Stop(javafx.scene.paint.Stop) Point3D(javafx.geometry.Point3D) KeyCode(javafx.scene.input.KeyCode) PerspectiveCamera(javafx.scene.PerspectiveCamera) Ray(org.fxyz.geometry.Ray) PointLight(javafx.scene.PointLight) Scene(javafx.scene.Scene)

Example 7 with LinearGradient

use of javafx.scene.paint.LinearGradient in project FXyzLib by Birdasaur.

the class Patterns method createCarbonKevlarPattern.

public static final ImagePattern createCarbonKevlarPattern() {
    final double WIDTH = 12;
    final double HEIGHT = 12;
    final Canvas CANVAS = new Canvas(WIDTH, HEIGHT);
    final GraphicsContext CTX = CANVAS.getGraphicsContext2D();
    double offsetY = 0;
    /// 1= border=yellow=dark========================================================
    CTX.beginPath();
    CTX.rect(0, 0, WIDTH * 0.5, HEIGHT * 0.5);
    CTX.closePath();
    CTX.setFill(new LinearGradient(0, offsetY * HEIGHT, 0, 0.5 * HEIGHT + offsetY * HEIGHT, false, CycleMethod.NO_CYCLE, new Stop(0, Color.rgb(105, 105, 0)), new Stop(1, Color.rgb(98, 98, 0))));
    CTX.fill();
    //  2=body=yellow==============================
    CTX.beginPath();
    CTX.rect(WIDTH * 0.083333, 0, WIDTH * 0.333333, HEIGHT * 0.416666);
    CTX.closePath();
    offsetY = 0;
    CTX.setFill(new LinearGradient(0, offsetY * HEIGHT, 0, 0.416666 * HEIGHT + offsetY * HEIGHT, false, CycleMethod.NO_CYCLE, new Stop(0, Color.rgb(138, 138, 0)), new Stop(1, Color.rgb(130, 130, 0))));
    CTX.fill();
    //  3=border=yellow=dark=============================
    CTX.beginPath();
    CTX.rect(WIDTH * 0.5, HEIGHT * 0.5, WIDTH * 0.5, HEIGHT * 0.5);
    CTX.closePath();
    offsetY = 0.5;
    CTX.setFill(new LinearGradient(0, offsetY * HEIGHT, 0, 0.5 * HEIGHT + offsetY * HEIGHT, false, CycleMethod.NO_CYCLE, new Stop(0, Color.rgb(105, 105, 0)), new Stop(1, Color.rgb(98, 98, 0))));
    CTX.fill();
    //  4=body=yellow============================================================
    CTX.beginPath();
    CTX.rect(WIDTH * 0.583333, HEIGHT * 0.5, WIDTH * 0.333333, HEIGHT * 0.416666);
    CTX.closePath();
    offsetY = 0.5;
    CTX.setFill(new LinearGradient(0, offsetY * HEIGHT, 0, 0.416666 * HEIGHT + offsetY * HEIGHT, false, CycleMethod.NO_CYCLE, new Stop(0, Color.rgb(138, 138, 0)), new Stop(1, Color.rgb(130, 130, 0))));
    CTX.fill();
    //  5= border=gray=dark============================
    CTX.beginPath();
    CTX.rect(WIDTH * 0.5, 0, WIDTH * 0.5, HEIGHT * 0.5);
    CTX.closePath();
    offsetY = 0;
    CTX.setFill(new LinearGradient(0, offsetY * HEIGHT, 0, 0.5 * HEIGHT + offsetY * HEIGHT, false, CycleMethod.NO_CYCLE, new Stop(0, Color.rgb(48, 48, 48)), new Stop(1, Color.rgb(30, 30, 30))));
    CTX.fill();
    //  6=body=gray=============================
    CTX.beginPath();
    CTX.rect(WIDTH * 0.583333, HEIGHT * 0.083333, WIDTH * 0.333333, HEIGHT * 0.416666);
    CTX.closePath();
    offsetY = 0.083333;
    CTX.setFill(new LinearGradient(0, offsetY * HEIGHT, 0, 0.416666 * HEIGHT + offsetY * HEIGHT, false, CycleMethod.NO_CYCLE, new Stop(0, Color.rgb(53, 53, 53)), new Stop(1, Color.rgb(45, 45, 45))));
    CTX.fill();
    //  7= border=gray=dark=============================
    CTX.beginPath();
    CTX.rect(0, HEIGHT * 0.5, WIDTH * 0.5, HEIGHT * 0.5);
    CTX.closePath();
    offsetY = 0.5;
    CTX.setFill(new LinearGradient(0, offsetY * HEIGHT, 0, 0.5 * HEIGHT + offsetY * HEIGHT, false, CycleMethod.NO_CYCLE, new Stop(0, Color.rgb(48, 48, 48)), new Stop(1, Color.rgb(40, 40, 40))));
    CTX.fill();
    //  8= body=gray=light==============================
    CTX.beginPath();
    CTX.rect(WIDTH * 0.083333, HEIGHT * 0.583333, WIDTH * 0.333333, HEIGHT * 0.416666);
    CTX.closePath();
    offsetY = 0.583333;
    CTX.setFill(new LinearGradient(0, offsetY * HEIGHT, 0, 0.416666 * HEIGHT + offsetY * HEIGHT, false, CycleMethod.NO_CYCLE, new Stop(0, Color.rgb(53, 53, 53)), new Stop(1, Color.rgb(45, 45, 45))));
    CTX.fill();
    final Image PATTERN_IMAGE = CANVAS.snapshot(new SnapshotParameters(), null);
    final ImagePattern PATTERN = new ImagePattern(PATTERN_IMAGE, 0, 0, WIDTH, HEIGHT, false);
    return PATTERN;
}
Also used : LinearGradient(javafx.scene.paint.LinearGradient) SnapshotParameters(javafx.scene.SnapshotParameters) GraphicsContext(javafx.scene.canvas.GraphicsContext) Stop(javafx.scene.paint.Stop) Canvas(javafx.scene.canvas.Canvas) ImagePattern(javafx.scene.paint.ImagePattern) Image(javafx.scene.image.Image)

Example 8 with LinearGradient

use of javafx.scene.paint.LinearGradient in project fxexperience2 by EricCanull.

the class PaintPickerController method onLinearButtonAction.

@FXML
void onLinearButtonAction(ActionEvent event) {
    final ToggleButton tb = (ToggleButton) event.getTarget();
    assert tb == linearToggleButton;
    final Paint value = gradientPicker.getValue(Mode.LINEAR);
    assert value instanceof LinearGradient;
    // Update UI
    setMode(value);
    gradientPicker.setMode(value);
    gradientPicker.updatePreview(value);
    // Update model
    setPaintProperty(value);
    event.consume();
}
Also used : LinearGradient(javafx.scene.paint.LinearGradient) ToggleButton(javafx.scene.control.ToggleButton) Paint(javafx.scene.paint.Paint) FXML(javafx.fxml.FXML)

Example 9 with LinearGradient

use of javafx.scene.paint.LinearGradient in project fxexperience2 by EricCanull.

the class PaintPickerController method getMode.

public Mode getMode() {
    final Mode mode;
    final Paint value = getPaintProperty();
    if (value instanceof Color) {
        mode = Mode.COLOR;
    } else if (value instanceof LinearGradient) {
        mode = Mode.LINEAR;
    } else {
        assert value instanceof RadialGradient;
        mode = Mode.RADIAL;
    }
    return mode;
}
Also used : LinearGradient(javafx.scene.paint.LinearGradient) Mode(com.fxexperience.javafx.scene.control.paintpicker.PaintPicker.Mode) Color(javafx.scene.paint.Color) RadialGradient(javafx.scene.paint.RadialGradient) Paint(javafx.scene.paint.Paint)

Aggregations

LinearGradient (javafx.scene.paint.LinearGradient)9 Stop (javafx.scene.paint.Stop)6 Paint (javafx.scene.paint.Paint)4 SnapshotParameters (javafx.scene.SnapshotParameters)3 Canvas (javafx.scene.canvas.Canvas)3 GraphicsContext (javafx.scene.canvas.GraphicsContext)3 Image (javafx.scene.image.Image)3 ImagePattern (javafx.scene.paint.ImagePattern)3 Mode (com.fxexperience.javafx.scene.control.paintpicker.PaintPicker.Mode)2 FXML (javafx.fxml.FXML)2 PerspectiveCamera (javafx.scene.PerspectiveCamera)2 PointLight (javafx.scene.PointLight)2 Scene (javafx.scene.Scene)2 KeyCode (javafx.scene.input.KeyCode)2 MouseEvent (javafx.scene.input.MouseEvent)2 StackPane (javafx.scene.layout.StackPane)2 Color (javafx.scene.paint.Color)2 RadialGradient (javafx.scene.paint.RadialGradient)2 GradientPicker (com.fxexperience.javafx.scene.control.gradientpicker.GradientPicker)1 GradientPickerStop (com.fxexperience.javafx.scene.control.gradientpicker.GradientPickerStop)1