javafx: How to add an appropriate listener to an ensemble demo?

烂漫一生 提交于 2019-12-12 01:45:41

问题


I'm new to javafx and browsed through the demos provided by oracle, especially I found this:

package ensemble.samples.graphics2d.images.imageoperator;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ImageOperationApp extends Application {
private SimpleDoubleProperty gridSize = new SimpleDoubleProperty(3.0);
private SimpleDoubleProperty hueFactor = new SimpleDoubleProperty(12.0);
private SimpleDoubleProperty hueOffset = new SimpleDoubleProperty(240.0);
private static void renderImage(WritableImage img, double gridSize, double hueFactor, double hueOffset) {
    PixelWriter pw = img.getPixelWriter();
    double w = img.getWidth();
    double h = img.getHeight();
    double xRatio = 0.0;
    double yRatio = 0.0;
    double hue = 0.0;

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            xRatio = x/w;
            yRatio = y/h;
            hue = Math.sin(yRatio*(gridSize*Math.PI))*Math.sin(xRatio*(gridSize*Math.PI))*Math.tan(hueFactor/20.0)*360.0 + hueOffset;
            Color c = Color.hsb(hue, 1.0, 1.0);
            pw.setColor(x, y, c);
        }
    }
}
public Parent createContent() {
     StackPane root = new StackPane();
    final WritableImage img = new WritableImage(200, 200);
    gridSize.addListener((Observable observable) -> {
        renderImage(img, gridSize.doubleValue(), hueFactor.doubleValue(), hueOffset.doubleValue());
     });
    hueFactor.addListener((Observable observable) -> {
        renderImage(img, gridSize.doubleValue(), hueFactor.doubleValue(), hueOffset.doubleValue());
     });
    hueOffset.addListener((Observable observable) -> {
        renderImage(img, gridSize.doubleValue(), hueFactor.doubleValue(), hueOffset.doubleValue());
     });
    renderImage(img, 3.0, 12.0, 240.0);

    ImageView view = new ImageView(img);

     root.getChildren().add(view);

    return root;
}

@Override public void start(Stage primaryStage) throws Exception {
    primaryStage.setScene(new Scene(createContent()));
    primaryStage.show();
}

/** Java main for when running without JavaFX launcher
 * @param args command line arguments
 */
public static void main(String[] args) { launch(args); }
}

1. This is selfcontained and runable.
2. In opposite to the container application for the demos, "ensemle.jar", which provides a "playground" with some sliders for the three SimpleDoubleProperties, here are no sliders cf. the screenshot of ensemble .
3. In order to get an idea how the event-handling with FX works (and to enjoy this nice application) I would like to add appropriate keylisteners to imitate the sliders.

I have no idea where to add the listeners and where to process the events fired by the keyboard, but guess, that there are missing only some lines of code.

Edit: I would be happy if I had a hint, where (and how) to insert a keylistener, so that typing "Y" would give me a "HelloWorld" in the console. I'm confident to do the rest myself.


回答1:


Adjust your start method like this and you will get a message whenever you press the Y-key.

    @Override public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(createContent());
        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if(event.getCode()== KeyCode.Y){
                    System.out.println("got a Y");
                }
            }
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    }

If you want to do sth else, I would suggest to look into all the setOn...-methods applicable for scene in the javadocs.



来源:https://stackoverflow.com/questions/41912445/javafx-how-to-add-an-appropriate-listener-to-an-ensemble-demo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!