Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect whether a Mac has a Retina display from Java?

Tags:

java

macos

I've found a Swing image rendering bug which afflicts only Macs with Retina displays (TYPE_INT_ARGB_PRE BufferedImages fail to draw). I have an effective workaround (use TYPE_INT_ARGB images instead), but in order to apply it, I need to be able to detect whether the host machine is a Mac with a Retina display. Detecting whether the machine is a Mac is no problem, but how can I determine the display type?

like image 265
uckelman Avatar asked Feb 05 '13 20:02

uckelman


2 Answers

Toolkit.getDefaultToolkit().getDesktopProperty("apple.awt.contentScaleFactor")

The above line should return 2.0 on retina displays. On more recent non-retina macs it returns 1.0 and on all other platforms you get back null. I can't find any official documentation, but I ran across it in this mailing list post while trying to solve a similar problem.

like image 112
job Avatar answered Sep 22 '22 00:09

job


I found this solution which uses:

Graphics2D g = (Graphics2D) this.getGraphics ();
boolean retinaTest = g.getFontRenderContext ().getTransform ()
    .equals (AffineTransform.getScaleInstance (2.0, 2.0));
like image 26
dmolony Avatar answered Sep 20 '22 00:09

dmolony