Last active
December 17, 2015 09:59
-
-
Save mfr/5591127 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.swing.*; | |
| import java.awt.*; | |
| import java.awt.image.BufferedImage; | |
| public class pxs extends JFrame { | |
| private static final long SLEEP_DELAY = 500L; | |
| private static JLabel label; | |
| public static void main(String[] args) { | |
| JFrame frame = new pxs(); | |
| final JPanel panel = new JPanel(); | |
| frame.setAlwaysOnTop(true); | |
| panel.setLayout(new BorderLayout()); | |
| frame.setResizable(false); | |
| Dimension screenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize()); | |
| label = new JLabel("Hello World"); | |
| label.setOpaque(true); | |
| frame.getContentPane().add(label); | |
| label.setBackground(Color.black); | |
| label.setForeground(Color.gray); | |
| label.setHorizontalAlignment(SwingConstants.RIGHT); | |
| frame.setDefaultCloseOperation(EXIT_ON_CLOSE); | |
| frame.setUndecorated(true); | |
| frame.setLocation(screenSize.width-80,screenSize.height-label.getHeight()-60); | |
| frame.pack(); | |
| frame.setVisible(true); | |
| pix(); | |
| } | |
| private static void pix() { | |
| Point coord; | |
| Robot robot = null; | |
| try { | |
| robot = new Robot(); | |
| } catch (AWTException e) { | |
| e.printStackTrace(); | |
| System.exit(-1); | |
| } | |
| Color pixColor = null; | |
| Color color = null; | |
| int _w = 0; | |
| int _h = 0; | |
| while (true) { | |
| try { | |
| coord = MouseInfo.getPointerInfo().getLocation(); | |
| Rectangle screenRect = new Rectangle(0, 0, Toolkit.getDefaultToolkit() | |
| .getScreenSize().width, Toolkit.getDefaultToolkit() | |
| .getScreenSize().height); | |
| BufferedImage grid = robot.createScreenCapture(screenRect); | |
| int x = coord.x; | |
| int y = coord.y; | |
| int x0 = x; | |
| int y0 = y; | |
| int rgb = grid.getRGB(x0, y0); | |
| //System.out.println(rgb); | |
| int w = 0; | |
| int h = 0; | |
| x = x0 - 1; | |
| do { | |
| x--; | |
| w++; | |
| } while (grid.getRGB(x, y) == rgb || x <= 0); | |
| x = x0; | |
| do { | |
| x++; | |
| w++; | |
| } while (grid.getRGB(x, y) == rgb || x >= screenRect.width); | |
| x = x0; | |
| y = y0 - 1; | |
| do { | |
| y--; | |
| h++; | |
| } while (grid.getRGB(x, y) == rgb || y <= 0); | |
| y = y0; | |
| do { | |
| y++; | |
| h++; | |
| } while (grid.getRGB(x, y) == rgb || y >= screenRect.height); | |
| if ((_w != w) || (_h != h)) { | |
| //System.out.println(w + " " + h); | |
| label.setText(w + " x " + h); | |
| } | |
| _w = w; | |
| _h = h; | |
| } catch (Exception e) { | |
| } | |
| try { | |
| Thread.sleep(SLEEP_DELAY); | |
| } catch (InterruptedException e) { | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment