2013-04-22 65 views
0

我有我的代碼了這裏:Java的RGB值碼Robot類

package RGBValues; 

import java.awt.Color; 
import java.awt.MouseInfo; 
import java.awt.Point; 
import java.awt.PointerInfo; 
import java.awt.Robot; 

public class RGBValues { 

    public static void main(String[] args) throws Exception { 
     PointerInfo pointer; 
     pointer = MouseInfo.getPointerInfo(); 
     Point coord = pointer.getLocation(); 

     Robot robot = new Robot(); 
     robot.delay(2000); 

     while(true) { 
      coord = MouseInfo.getPointerInfo().getLocation()… 
      Color color = robot.getPixelColor((int)coord.getX(), (int)coord.getY()); 
      if(color.getGreen() == 255 && 
       color.getBlue() == 255 && 
       color.getRed() == 255 
      ) { 
       System.out.println("WHITE"); 
      } 
      robot.delay(1000); 
     } 
    } 
} 

我卡在如何讓這個無論鼠標在屏幕上指指點點,它會告訴我的RGB指針下面的那個像素的值。有人可以幫助和做什麼?我對Java很新,所以我不知道如何解決這個問題。

+0

這爲我工作。也許你只是不把鼠標放在白色像素上。每次執行循環時只顯示顏色。 – camickr 2013-04-22 04:19:49

回答

1

除了可能讓線程運行有點野,我看不出有任何理由,爲什麼你的代碼將無法正常工作......

enter image description here

import java.awt.AWTException; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.FontMetrics; 
import java.awt.Graphics; 
import java.awt.HeadlessException; 
import java.awt.MouseInfo; 
import java.awt.Point; 
import java.awt.PointerInfo; 
import java.awt.Robot; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class WhatsMyColor { 

    public static void main(String[] args) { 
     new WhatsMyColor(); 
    } 

    public WhatsMyColor() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      Thread thread = new Thread(new BackgroundMonitor()); 
      thread.setDaemon(true); 
      thread.start(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Color color = getBackground(); 
      String text = color.getRed() + "x" + color.getGreen() + "x" + color.getBlue(); 
      FontMetrics fm = g.getFontMetrics(); 
      int x = (getWidth() - fm.stringWidth(text))/2; 
      int y = ((getHeight() - fm.getHeight())/2) + fm.getAscent(); 
      g.drawString(text, x, y); 
     } 

     public class BackgroundMonitor implements Runnable { 

      @Override 
      public void run() { 
       try { 
        Robot robot = new Robot(); 

        Color previous = null; 
        while (true) { 
         Point coord = MouseInfo.getPointerInfo().getLocation(); 
         Color color = robot.getPixelColor((int) coord.getX(), (int) coord.getY()); 
         if (previous == null || !previous.equals(color)) { 
          SwingUtilities.invokeLater(new UpdateBackgroud(color)); 
         } 
         try { 
          Thread.sleep(250); 
         } catch (InterruptedException ex) { 
         } 
        } 
       } catch (AWTException | HeadlessException exp) { 
        exp.printStackTrace(); 
       } 
      } 
     } 

     public class UpdateBackgroud implements Runnable { 

      private Color color; 

      public UpdateBackgroud(Color color) { 
       this.color = color; 
      } 

      @Override 
      public void run() { 
       setBackground(color); 
      } 

     } 
    } 
}