Hi and thanks for making FXGraphics2D, it's really great!
I'm using AWT's TextLayout#draw to draw into a FXGraphics2D.
However, the result is not very good - a long way from when I draw text directly with JavaFX.
Anything I can do to improve rendering quality?
Thanks!
Font rendering quality
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: Font rendering quality
Hmm, I see this too. I'm not aware of a way to improve this, but I'll see if I can find anything.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: Font rendering quality
One thing I've found is that it is important to call getGraphicsContext2D().clearRect(0, 0, width, height) each time the canvas is redrawn. This clears the drawing queue, and improves the text quality for me (it seems as though the same string was being drawn over and over which makes it look bad). Try the following demo and see how it looks for you:
Code: Select all
/* =================
* FXGraphics2DDemo2
* =================
*
* Copyright (c) 2014, Object Refinery Limited.
* All rights reserved.
*
* http://www.object-refinery.com/fxgraphics2d/index.html
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the Object Refinery Limited nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package org.jfree.fx.demo;
import static javafx.application.Application.launch;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.font.TextAttribute;
import org.jfree.fx.FXGraphics2D;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.text.AttributedString;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* http://www.jfree.org/forum/viewtopic.php?f=34&t=116969
*/
public class FXGraphics2DDemo2 extends Application {
static class MyCanvas extends Canvas {
TextLayout textLayout;
private FXGraphics2D g2;
public MyCanvas() {
this.g2 = new FXGraphics2D(getGraphicsContext2D());
AttributedString as = new AttributedString("Hello World!");
as.addAttribute(TextAttribute.FONT, new Font("serif", Font.PLAIN, 24));
this.textLayout = new TextLayout(as.getIterator(), this.g2.getFontRenderContext());
// Redraw canvas when size changes.
widthProperty().addListener(evt -> draw());
heightProperty().addListener(evt -> draw());
}
private void draw() {
double width = getWidth();
double height = getHeight();
getGraphicsContext2D().clearRect(0, 0, width, height);
this.textLayout.draw(this.g2, 50, 50);
Rectangle2D r = new Rectangle2D.Double(60, 60, 60, 70);
this.g2.setStroke(new BasicStroke(0.5f));
this.g2.setPaint(Color.BLACK);
this.g2.draw(r);
}
@Override
public boolean isResizable() {
return true;
}
@Override
public double prefWidth(double height) { return getWidth(); }
@Override
public double prefHeight(double width) { return getHeight(); }
}
@Override
public void start(Stage stage) throws Exception {
MyCanvas canvas = new MyCanvas();
StackPane stackPane = new StackPane();
stackPane.getChildren().add(canvas);
// Bind canvas size to stack pane size.
canvas.widthProperty().bind( stackPane.widthProperty());
canvas.heightProperty().bind( stackPane.heightProperty());
stage.setScene(new Scene(stackPane));
stage.setTitle("FXGraphics2DDemo2.java");
stage.setWidth(700);
stage.setHeight(390);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: Font rendering quality
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader

