Thursday, 7 March 2019

Different results when running java awt code on Windows and Linux




I have some code (unfortunately not written by me, or else I would know where to start looking for solutions) that draws some line graphs. When I run the code on my Windows machine and on the Linux machine, I get different results: on the Linux machine, the border around the legend is drawn with less height, resulting in some ugly problems when there are more than 3 items in the legend.



Is there some known difference when using



int getHeight(Graphics2D g, String text) {
Rectangle2D bounds = g.getFont().getStringBounds(text, g.getFontMetrics().getFontRenderContext());
return bounds.getHeight();
}



or where could the problem be?



Windows:



C:\>java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)



Linux:



$ java -version
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.10) (6b20-1.9.10-0ubuntu1~10.04.3)
OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)


UPDATE:




It was a rounding bug that seemingly only occurred with linux fonts, not with windows ones. Strange.


Answer



Your method is based on getFontMetrics(), that return the font metric of the current font. I assume you're using default font in your program, and they are different in the different OS, so the result height might be different.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...