Take the 2-minute tour ×

I have a sort of animation going on in which a rectangle is increasing and decreasing in size........what i want to do here is detect the color of a particular location through Robot() class but its not happening..........why?? I also want to know if Robot() can be used outside try or without main() class.

//<applet code=ctry.java width=500 height=500></applet>

import java.awt.*;
import java.applet.Applet;
import java.awt.AWTException;

public class ctry extends Applet implements Runnable{
Thread d=null;
int l=0,t=0,i=250,j=250;
Color color=null;
public void init(){
public void start(){
 d=new Thread(this);
public void run(){
System.out.println("in run");
   Robot robo=new Robot();
  System.out.println("after robo");
      System.out.println("in while");
      System.out.println("Red   = " + color.getRed());

catch(Exception e)
public void paint(Graphics g) 
System.out.println("in paint");
public void destroy()
share|improve this question
Rather than writing empty catch statements (which is a bad practice) add print statements to see if an exception is really thrown –  Extreme Coders May 2 '13 at 9:21
1) Change catch(Exception e) {} to catch(Exception e) { e.printStackTrace(); } and copy/paste the output as an edit to the question. Then notify me (by typing the '@' symbol and my name) & I'll have a look over the output. It is likely throwing an AccessControlException. 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow! –  Andrew Thompson May 2 '13 at 13:32
@ Andrew Thompson.........the exception is of AccessControlException along with few more...........what now?? –  Suraj Pandey May 2 '13 at 16:42
Edit you question with all the exceptions being thrown. That will help diagnose the problem. –  Frecklefoot May 9 '13 at 16:55
I suspect that getPixelColor() may be returning null, but I can't be sure. Have you tried stepping through the code with a debugger? How many of your print statements get called? When the exception is thrown, it should give you the line number it's dying on. –  Frecklefoot May 9 '13 at 18:15

1 Answer 1

You could try to capture an image instead and check the color there. The Robot.getPixelColor() is very slow, it returns a new Color instance every time. Try using a BufferedImage instead:

Color color = Color.black;
int screenWidth = 768;
int screenHeight = 1024;
Rectangle rectangle = new Rectangle(screenWidth, screenHeight);
BufferedImage image = robot.createScreenCapture(rectangle);
for (int y = 0 ; y < screenHeight ; y++) {
    for (int x = 0 ; x < screenWidth ; x++) {
        if(image.getRGB(x, y) == color.getRGB()) {
            return true;

And yes, you can use the Robot everywhere and you can deal with the exceptions, but most on them are runtime exceptions, so you don't have to.

share|improve this answer

Your Answer


