diff src/viewer_swing/java/com/glavsoft/viewer/Viewer.java @ 456:34277b436cac

Using retina scale to share screnn if minus postion
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Wed, 22 Jun 2016 19:11:37 +0900
parents 6f3d3da40940
children effba48b76f3
line wrap: on
line diff
--- a/src/viewer_swing/java/com/glavsoft/viewer/Viewer.java	Wed Jun 22 12:29:57 2016 +0900
+++ b/src/viewer_swing/java/com/glavsoft/viewer/Viewer.java	Wed Jun 22 19:11:37 2016 +0900
@@ -40,6 +40,7 @@
 import java.awt.event.WindowListener;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Field;
 import java.net.Socket;
 import java.util.ArrayList;
 import java.util.jar.Attributes;
@@ -487,9 +488,12 @@
         GraphicsDevice[] gs = ge.getScreenDevices();
 
         for (GraphicsDevice gd : gs) {
+            int screenNumber = 0;
             for (GraphicsConfiguration r : gd.getConfigurations()) {
                 Rectangle rect = r.getBounds();
+                //System.out.println("screen "+ screenNumber +":"+rect);
                 rectangles.add(rect);
+                screenNumber++;
             }
         }
         return rectangles;
@@ -500,13 +504,38 @@
         // compute offset to make them positive
         int offsetx = 0;
         int offsety = 0;
+        int screenNumber = 0;
         for (Rectangle rect : rectangles) {
-            if (rect.x < offsetx) offsetx = rect.x;
-            if (rect.y < offsety) offsety = rect.y;
+            int scale = retinaScale(screenNumber);
+            int x = rect.x < 0 ? rect.x * scale : rect.x;
+            int y = rect.y < 0 ? rect.y * scale : rect.y;
+            if (x < offsetx) offsetx = x;
+            if (y < offsety) offsety = y;
+            screenNumber++;
         }
         return new Point(-offsetx,-offsety);
     }
 
+    public int retinaScale(int shareScreenNumber) {
+        int scale = 1;
+        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
+        final GraphicsDevice[] devices = env.getScreenDevices();
+
+        try {
+            Field field = devices[shareScreenNumber].getClass().getDeclaredField("scale");
+
+            if (field != null) {
+                field.setAccessible(true);
+                Object retinaScale = field.get(devices[shareScreenNumber]);
+
+                if (retinaScale instanceof Integer) {
+                    scale = (Integer) retinaScale;
+                    return scale;
+                }
+            }
+        } catch (Exception ignore) {}
+        return scale;
+    }
 
     @Override
     public void setFitScreen() {