Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import java.awt.Rectangle;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.JFrame;
- public class MultiMonTest {
- public static void main(String[] args) {
- new MultiMonTest();
- }
- public MultiMonTest() {
- JFrame f = new JFrame();
- f.addKeyListener(new KeyListener() {
- @Override
- public void keyTyped(KeyEvent e) {}
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.getKeyChar() == ' ') {
- prevMonitor(f);
- } else if(e.getKeyChar() == '\n') {
- nextMonitor(f);
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {}
- });
- f.setSize(256, 256);
- new Thread(() -> {
- while(true) {
- try {
- Thread.sleep(1000);
- GraphicsDevice device = f.getGraphicsConfiguration().getDevice();
- GraphicsDevice[] screenArray = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
- int index = findDeviceIndex(device);
- for(int i = 0; i < screenArray.length; i++) {
- if(screenArray[i].equals(device)) {
- index = i;
- break;
- }
- }
- System.out.println(device + " " + (index+1) + "/" + screenArray.length + ": " + f.getX() + ", " + f.getY());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }).start();
- f.setVisible(true);
- }
- private int findDeviceIndex(GraphicsDevice device) {
- GraphicsDevice[] screenArray = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
- for(int i = 0; i < screenArray.length; i++) {
- if(screenArray[i].equals(device)) {
- return i;
- }
- }
- return -1;
- }
- private void nextMonitor(JFrame f) {
- GraphicsDevice[] screenArray = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
- GraphicsDevice device = f.getGraphicsConfiguration().getDevice();
- GraphicsDevice newDevice = null;
- int index = findDeviceIndex(device);
- if(index == screenArray.length - 1) {
- newDevice = screenArray[0];
- } else {
- newDevice = screenArray[++index];
- }
- Rectangle r = newDevice.getDefaultConfiguration().getBounds();
- f.setLocation(r.x, r.y);
- }
- private void prevMonitor(JFrame f) {
- GraphicsDevice[] screenArray = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
- GraphicsDevice device = f.getGraphicsConfiguration().getDevice();
- GraphicsDevice newDevice = null;
- int index = findDeviceIndex(device);
- if(index == 0) {
- newDevice = screenArray[screenArray.length - 1];
- } else {
- newDevice = screenArray[--index];
- }
- Rectangle r = newDevice.getDefaultConfiguration().getBounds();
- f.setLocation(r.x, r.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment