1

So I am developing an application using JavaFX that needs to display LateX formulas on a canvas. I managed to do just that using the revised version of this thread: Running swing application in javaFX. So my next move was to draw a line under the formula when the mouse moves, which should be easy, but the line simply does not show! I was finally able to spot the problem:

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

When I scale my FXGraphics2D object, I cannot draw anything on canvas except the formula. If I comment the code I can draw the line, but unfortunately the formula becomes very, very tiny. Does anyone have an idea to fix that? Here is the code to display the line:

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);
        }
});

Here is where I set the canvas:

 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();
    }  

And finally where I draw the formula:

private void draw() { 
        double width = getWidth(); 
        double height = getHeight();
        getGraphicsContext2D().clearRect(0, 0, width, height);
        this.box.draw(g2, 1, 5);
    }
Community
  • 1
  • 1
  • Can you post the part where FXGraphics2D is implemented? It might also be that your GraphicsContext is so large (scale by 20, 20 is humongous), that the line may be out of bounds of the canvas. What are you trying to achieve by scaling? – Steven Sep 28 '16 at 03:00
  • @steven you are right! If I scale by 2 instead of 20 the line appears, but very large, while the formula is very small. I am still trying to get around that. – Danilo Cardoso Sep 28 '16 at 13:31
  • My solution would be to NOT scale the canvas, but somehow change the size of the TeX object independently. – Steven Sep 28 '16 at 18:23

0 Answers0