Javafx2.2画布放大和缩小失真问题 [英] Javafx2.2 canvas zoom in and out distortion problem

查看:258
本文介绍了Javafx2.2画布放大和缩小失真问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用javafx开发可扩展的图形程序。考虑到性能,我们使用javafx canvas绘制了很多矩形框。矩形框表示容器的位置。我们发现矩形会出现粗细线条,模糊等现象。我们的图形需要缩放。计算出的矩形框架的宽度和坐标将具有浮点数。我们无法使用以下帖子解决问题。
如何绘制清晰, JavaFX 2.2中的不透明发线?

We are using javafx to develop a scalable graphics program. Considering the performance, we use javafx canvas to draw a lot of rectangular boxes. The rectangular box represents the position of the container. We found that the rectangle will appear thick and thin lines, blurred and so on. Our graphics need to be scaled. The calculated width and coordinates of the rectangular frame will have floating point numbers. We can't solve the problem by using the following post. How to draw a crisp, opaque hairline in JavaFX 2.2?

这是我的例子。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class LineScaleTest extends Application {
public static void main(String[] args) { launch(args); }

@Override public void start(Stage stage) {
    AnchorPane anchorPane=new AnchorPane();
    Canvas canvas=new Canvas(500,300);
    canvas.relocate(300,300);
    GraphicsContext gc=canvas.getGraphicsContext2D();
    for(int i=0;i<10;i++){
        gc.strokeRoundRect(10.5+35*i,5.5,30,30,0,0);
    }

    for(int i=0;i<10;i++){
        gc.strokeRoundRect(10.5+35*i,65.5,30,30,0,0);
    }

    for(int i=0;i<10;i++){
        gc.strokeRoundRect(10.5+35*i,115.5,30,30,0,0);
    }

    anchorPane.setOnScroll(new EventHandler<ScrollEvent>() {
        @Override
        public void handle(ScrollEvent e) {
            if (e.getDeltaY() == 0) {
                return;
            }
            double scaleFactor = (e.getDeltaY() > 0) ? 1.1
                    : 1 / 1.1;
            anchorPane.setScaleX(anchorPane.getScaleX() * scaleFactor);
            anchorPane.setScaleY(anchorPane.getScaleY() * scaleFactor);
        }
    });
    anchorPane.getChildren().add(canvas);
    Scene scene = new Scene( anchorPane,1200, 700);
    stage.setScene(scene);
    stage.show();
}
}

example2 ########## ################################################## #

example2#############################################################

公共类LineScaleTest扩展Application {
public static void main(String [] args){launch(args); }

public class LineScaleTest extends Application { public static void main(String[] args) { launch(args); }

@Override public void start(Stage stage) {
    AnchorPane anchorPane=new AnchorPane();
    Canvas canvas=new Canvas(500,300);
    canvas.relocate(300,300);
    GraphicsContext gc=canvas.getGraphicsContext2D();
    for(int i=0;i<10;i++){
        gc.strokeRoundRect(10.5+35*i,5.5,30,30,0,0);
    }

    for(int i=0;i<10;i++){
        gc.strokeRoundRect(10.5+35*i,65.5,30,30,0,0);
    }

    for(int i=0;i<10;i++){
        gc.strokeRoundRect(10.5+35*i,115.5,30,30,0,0);
    }

    anchorPane.setOnScroll(new EventHandler<ScrollEvent>() {
        @Override
        public void handle(ScrollEvent e) {
            if (e.getDeltaY() == 0) {
                return;
            }
            double scaleFactor = (e.getDeltaY() > 0) ? 1.1
                    : 1 / 1.1;
            Scale scale=new Scale(anchorPane.getScaleX() * scaleFactor,anchorPane.getScaleY() * scaleFactor);
            Affine reflectTransform = new Affine();
            reflectTransform.append(scale);
            canvas.getTransforms().add(reflectTransform);
        }
    });
    anchorPane.getChildren().add(canvas);
    Scene scene = new Scene( anchorPane,1200, 700);
    stage.setScene(scene);
    stage.show();
}

}

推荐答案

我认为你不能依靠JavaFX内置的任何东西来产生你想要的结果。您的应用程序需要自行管理,以产生所需的结果。因此,您不需要调用缩放,而是需要自己增加画布宽度和高度,然后根据新尺寸执行重绘。然后,您的渲染代码可以确保使用的坐标不会导致模糊或模糊。

I don't think you can rely on anything built into JavaFX to produce exactly the result you want. Your application will need to manage this itself in order to produce the desired result. So instead of calling "scale" you will need to increase the canvas width and height yourself and then perform a redrawing based on the new dimensions. Your rendering code can then ensure that the coordinates used will not cause fuzziness or blur.

这篇关于Javafx2.2画布放大和缩小失真问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆