FXGraphics2D缩放和JavaFX中的画布绘制

时间:2016-09-27 23:34:50

标签: canvas javafx latex graphicscontext

所以我正在使用JavaFX开发一个需要在画布上显示LateX公式的应用程序。我设法使用此线程的修订版本Running swing application in javaFX。 所以我的下一步是在鼠标移动时在公式下绘制一条线,这应该很容易,但线条根本不显示!我终于能够发现问题了:

this.g2 = new FXGraphics2D(canvas.getGraphicsContext2D());
this.g2.scale(20, 20);

当我缩放我的FXGraphics2D对象时,除了公式之外,我无法在画布上绘制任何东西。如果我评论代码我可以绘制线,但不幸的是公式变得非常非常小。有没有人有想法解决这个问题? 以下是显示该行的代码:

canvas.addEventHandler(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent t) {
            GraphicsContext gc = canvas.getGraphicsContext2D();
            gc.setFill(Color.GREEN);
            gc.setStroke(Color.BLUE);
            gc.setLineWidth(5);
            gc.strokeLine(20, 80, 50, 80);
        }
});

这是我设置画布的地方:

 public void initializeCanvas() {
        this.g2 = new FXGraphics2D(canvas.getGraphicsContext2D());
        this.g2.scale(20, 20);

        // create a formula
        TeXFormula formula = new TeXFormula("x=\\frac{-b \\pm \\sqrt {b^2-4ac}}{2a}");
        TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);

        // the 'Box' seems to be the thing we can draw directly to Graphics2D
        this.box = icon.getBox();
    }  

最后我画出公式:

private void draw() { 
        double width = getWidth(); 
        double height = getHeight();
        getGraphicsContext2D().clearRect(0, 0, width, height);
        this.box.draw(g2, 1, 5);
    }

0 个答案:

没有答案
相关问题