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