Как Сравнить Два Цвета?

10.12.2013
9
0
#1
Всем привет. Подскажите начинающему програмировать как можна сравнить два цвета один и котрых задан ро умолчанию, а второй должен браться методом getPixelColor(х, у).
У меня выбивает такое:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at myFrame$1.actionPerformed(Faktorial.java:28)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


Пример моей программы:

import java.awt.Color;
import java.awt.Container;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class myFrame extends JFrame
{

Robot robot;
myFrame()
{
setTitle("Test");
JPanel panel = new JPanel();
JButton bt1 = new JButton("Start");
panel.add(bt1);
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

for(int i=0;i<1;i++)
{

robot.mouseMove(312, 390);
System.out.print(robot.getPixelColor(312, 390));
Color c1=robot.getPixelColor(312, 390);
Color col=new Color(252, 251, 256);
if(c1.equals(col)){
System.out.print("Yes");}
}
}
});
Container pane = getContentPane();
pane.add(panel);
pack();
}
}
public class Faktorial {

public static void main(String[] args) {
myFrame frame = new myFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();

}
}
 

LuMee

Well-known member
02.05.2006
477
0
#2
У вас переменная robot не инициализирована, вот и вылетает NPE.
 
10.12.2013
9
0
#3
Я уже все исправил, все работает. Но только почемуто не читает цвет пикселей у Flash плэере. Даже если делаю принтскрин, все ровно все пиксели выдает как черные. Может для него нужно какойто особый метод?