Guest User

MultiMonTest

a guest
Nov 2nd, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.awt.GraphicsDevice;
  2. import java.awt.GraphicsEnvironment;
  3. import java.awt.Rectangle;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6. import javax.swing.JFrame;
  7.  
  8. public class MultiMonTest {
  9.  
  10.     public static void main(String[] args) {
  11.         new MultiMonTest();
  12.     }
  13.  
  14.     public MultiMonTest() {
  15.         JFrame f = new JFrame();
  16.         f.addKeyListener(new KeyListener() {
  17.             @Override
  18.             public void keyTyped(KeyEvent e) {}
  19.  
  20.             @Override
  21.             public void keyPressed(KeyEvent e) {
  22.                 if(e.getKeyChar() == ' ') {
  23.                     prevMonitor(f);
  24.                 } else if(e.getKeyChar() == '\n') {
  25.                     nextMonitor(f);
  26.                 }
  27.             }
  28.  
  29.             @Override
  30.             public void keyReleased(KeyEvent e) {}
  31.         });
  32.         f.setSize(256, 256);
  33.         new Thread(() -> {
  34.             while(true) {
  35.                 try {
  36.                     Thread.sleep(1000);
  37.                     GraphicsDevice device = f.getGraphicsConfiguration().getDevice();
  38.                     GraphicsDevice[] screenArray = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  39.                     int index = findDeviceIndex(device);
  40.                     for(int i = 0; i < screenArray.length; i++) {
  41.                         if(screenArray[i].equals(device)) {
  42.                             index = i;
  43.                             break;
  44.                         }
  45.                     }
  46.                     System.out.println(device + " " + (index+1) + "/" + screenArray.length + ": " + f.getX() + ", " + f.getY());
  47.                 } catch (Exception e) {
  48.                     e.printStackTrace();
  49.                 }
  50.             }
  51.         }).start();
  52.         f.setVisible(true);
  53.     }
  54.  
  55.     private int findDeviceIndex(GraphicsDevice device) {
  56.         GraphicsDevice[] screenArray = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  57.         for(int i = 0; i < screenArray.length; i++) {
  58.             if(screenArray[i].equals(device)) {
  59.                 return i;
  60.             }
  61.         }
  62.         return -1;
  63.     }
  64.  
  65.     private void nextMonitor(JFrame f) {
  66.         GraphicsDevice[] screenArray = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  67.         GraphicsDevice device = f.getGraphicsConfiguration().getDevice();
  68.         GraphicsDevice newDevice = null;
  69.         int index = findDeviceIndex(device);
  70.         if(index == screenArray.length - 1) {
  71.             newDevice = screenArray[0];
  72.         } else {
  73.             newDevice = screenArray[++index];
  74.         }
  75.  
  76.         Rectangle r = newDevice.getDefaultConfiguration().getBounds();
  77.         f.setLocation(r.x, r.y);
  78.     }
  79.  
  80.     private void prevMonitor(JFrame f) {
  81.         GraphicsDevice[] screenArray = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  82.         GraphicsDevice device = f.getGraphicsConfiguration().getDevice();
  83.         GraphicsDevice newDevice = null;
  84.         int index = findDeviceIndex(device);
  85.         if(index == 0) {
  86.             newDevice = screenArray[screenArray.length - 1];
  87.         } else {
  88.             newDevice = screenArray[--index];
  89.         }
  90.  
  91.         Rectangle r = newDevice.getDefaultConfiguration().getBounds();
  92.         f.setLocation(r.x, r.y);
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment