0

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(){
 setBackground(Color.red);
                    }
public void start(){
 d=new Thread(this);
 d.start();
                        }
public void run(){
System.out.println("in run");
try{
   Robot robo=new Robot();
  System.out.println("after robo");
  while(true){
      System.out.println("in while");
      repaint();
      color=robo.getPixelColor(i,j);
      System.out.println("Red   = " + color.getRed());
      l+=10;
      t+=10;
      d.sleep(100);    
      if(t>=225)
       {t=0;}
      if(l>=225)
       {l=0;}
                  }

     }
catch(Exception e)
       {}
                         }
public void paint(Graphics g) 
{
System.out.println("in paint");
g.setColor(Color.blue);
g.fill3DRect(225,225,l*2,t*2,true);
}
public void destroy()
{}
}
eltabo
  • 3,749
  • 1
  • 21
  • 33
Suraj Pandey
  • 31
  • 1
  • 7
  • 1
    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 02 '13 at 09:21
  • 1
    1) Change `catch(Exception e) {}` to `catch(Exception e) { e.printStackTrace(); }` and copy/paste the output as an [edit to the question](http://stackoverflow.com/posts/16334064/edit). 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 02 '13 at 13:32
  • @ Andrew Thompson.........the exception is of AccessControlException along with few more...........what now?? – Suraj Pandey May 02 '13 at 16:42
  • Edit you question with all the exceptions being thrown. That will help diagnose the problem. – Frecklefoot May 09 '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 09 '13 at 18:15

1 Answers1

0

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.

wolfgarnet
  • 68
  • 1
  • 6