diff --git a/app/src/processing/app/ui/Toolkit.java b/app/src/processing/app/ui/Toolkit.java index fc740353b4..e35cc5c430 100644 --- a/app/src/processing/app/ui/Toolkit.java +++ b/app/src/processing/app/ui/Toolkit.java @@ -30,6 +30,7 @@ import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Image; @@ -907,23 +908,14 @@ static public boolean isRetina() { // A 5-minute search didn't turn up any such event in the Java API. // Also, should we use the Toolkit associated with the editor window? static private boolean checkRetina() { - if (Platform.isMacOS()) { - GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); - GraphicsDevice device = env.getDefaultScreenDevice(); - - try { - Field field = device.getClass().getDeclaredField("scale"); - if (field != null) { - field.setAccessible(true); - Object scale = field.get(device); - - if (scale instanceof Integer && ((Integer)scale).intValue() == 2) { - return true; - } - } - } catch (Exception ignore) { } - } - return false; + GraphicsDevice graphicsDevice = GraphicsEnvironment + .getLocalGraphicsEnvironment() + .getDefaultScreenDevice(); + GraphicsConfiguration graphicsConfig = graphicsDevice + .getDefaultConfiguration(); + + AffineTransform tx = graphicsConfig.getDefaultTransform(); + return Math.round(tx.getScaleX()) == 2; } diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index e4208ca62f..40f38b0bbb 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -30,8 +30,10 @@ // before calling settings() to get displayWidth/Height import java.awt.DisplayMode; // handleSettings() and displayDensity() +import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; +import java.awt.geom.AffineTransform; // used to present the fullScreen() warning about Spaces on OS X import javax.swing.JOptionPane; @@ -1158,59 +1160,14 @@ public int displayDensity() { * @param display the display number to check */ public int displayDensity(int display) { - if (PApplet.platform == PConstants.MACOS) { - // This should probably be reset each time there's a display change. - // A 5-minute search didn't turn up any such event in the Java 7 API. - // Also, should we use the Toolkit associated with the editor window? - final String javaVendor = System.getProperty("java.vendor"); - if (javaVendor.contains("Oracle")) { - GraphicsDevice device; - GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); - - if (display == -1) { - device = env.getDefaultScreenDevice(); - - } else if (display == SPAN) { - throw new RuntimeException("displayDensity() only works with specific display numbers"); - - } else { - GraphicsDevice[] devices = env.getScreenDevices(); - if (display > 0 && display <= devices.length) { - device = devices[display - 1]; - } else { - if (devices.length == 1) { - System.err.println("Only one display is currently known, use displayDensity(1)."); - } else { - System.err.format("Your displays are numbered %d through %d, " + - "pass one of those numbers to displayDensity()%n", 1, devices.length); - } - throw new RuntimeException("Display " + display + " does not exist."); - } - } - - try { - Field field = device.getClass().getDeclaredField("scale"); - if (field != null) { - field.setAccessible(true); - Object scale = field.get(device); - - if (scale instanceof Integer && ((Integer)scale).intValue() == 2) { - return 2; - } - } - } catch (Exception ignore) { } - } - } else if (PApplet.platform == PConstants.WINDOWS || - PApplet.platform == PConstants.LINUX) { - if (suggestedDensity == -1) { - // TODO: detect and return DPI scaling using JNA; Windows has - // a system-wide value, not sure how it works on Linux - return 1; - } else if (suggestedDensity == 1 || suggestedDensity == 2) { - return suggestedDensity; - } - } - return 1; + GraphicsDevice graphicsDevice = GraphicsEnvironment + .getLocalGraphicsEnvironment() + .getDefaultScreenDevice(); + GraphicsConfiguration graphicsConfig = graphicsDevice + .getDefaultConfiguration(); + + AffineTransform tx = graphicsConfig.getDefaultTransform(); + return (int) Math.round(tx.getScaleX()); }