JavaFX:将屏幕上的像素映射到2D矩阵

时间:2016-09-02 11:24:20

标签: matrix mapping javafx-8 pixel java-canvas

我正在制作动画,我需要在屏幕上跟踪探索/未探测的像素。最初屏幕是黑色的,然后当节点(圆圈)移动(在定义的路径上)时,探索的像素是为执行此任务(颜色更改),我使用 Canvas 类JavaFX作为背景,并使用 的对象绘制路径GraphicsContext 类(参见 createPathAnimation 方法),现在我想将int 2D矩阵更新为0-unexplored,1-explored。 如何在 createPathAnimation 中使用 changed() 功能更新矩阵,因为该函数正在更新像素颜色为白色,我需要在矩阵中将同一组探测像素更新为1? sample translation

我正在尝试使用内置函数,因为即使我知道初始和最终像素坐标,也不容易确定当圆圈在它们之间移动时将设置哪些像素(对于前一个对角线)因为圆圈就像一个像素级别的小方块。 我的动机是在对角线平移后找到数字的白色像素。

public void start(Stage primaryStage)throws Exception{
    Pane root=new Pane();
    Path path1=createPath();

    canvas=new Canvas(800,600);

    root.getChildren().addAll(path1,canvas);

    primaryStage.setScene(new Scene(root,800,600,Color.BLACK));
    primaryStage.show();

    Animation animation1=createPathAnimation(path1,Duration.seconds(10));

    pt.getChildren().addAll(animation1);
    pt.play();
}


private Path createPath(){
    Path path=new Path();
    path.setStroke(Color.BLACK);
    path.setStrokeWidth(10);
    path.getElements().add(new MoveTo(400,300));
    path.getElements().add(new LineTo(600,500));
    return path;
}
public int a,b;
private Animation createPathAnimation(Path path,Duration duration){
    GraphicsContext gc=canvas.getGraphicsContext2D();
    Circle pen=new Circle(0,0,10);
    PathTransition pathTransition=new PathTransition(duration,path,pen);
    pathTransition.currentTimeProperty().addListener(new ChangeListener<Duration>(){

        Location oldLocation = null;

        /**
         * Draw a line from the old location to the new location
         */
        @Override
        public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {


            if( oldValue == Duration.ZERO)
                return;

            // get current location
            double x = pen.getTranslateX();
            double y = pen.getTranslateY();

            // initialize the location
            if( oldLocation == null) {
                oldLocation = new Location();
                oldLocation.x = x;
                oldLocation.y = y;
                return;
            }

            // draw line
            gc.setStroke(Color.WHITE);
            gc.setLineWidth(30);
            gc.strokeLine(oldLocation.x, oldLocation.y, x, y);

            // update old location with current one
            oldLocation.x = x;
            oldLocation.y = y;
        }
    });

    return pathTransition;
}

public static class Location {
    double x;
    double y;

0 个答案:

没有答案