简体   繁体   中英

error in creating my own Robot class in android

I have decided to create my own Java's Robot class in android to take screen capture..i have written the source code of the robot class by my own but the problem is here, the following line in the code is throwing compilation error..saying " The method createRobot(Robot, GraphicsDevice) in the type ComponentFactory is not applicable for the arguments (Robot, GraphicsDevice)"

peer = ((ComponentFactory)toolkit).createRobot(this, screen);

Can anyone suggest me what would be the solution....

thanks..

HERE IS MY FULL SOURCE CODE

package com.example.awt;

import java.awt.AWTException; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.peer. ; import java.awt.image. ; import java.awt.event.*; import java.lang.reflect.InvocationTargetException ;

//import com.sun.media.sound.Toolkit;

import sun.awt.ComponentFactory; //import sun.awt.SunToolkit; import sun.security.util.SecurityConstants; import java.awt.Toolkit;

public class Robot {

 private static final int MAX_DELAY = 60000;
private RobotPeer peer;
  private boolean isAutoWaitForIdle = false;
  private int autoDelay = 0;
  private static final int LEGAL_BUTTON_MASK = 
                       InputEvent.BUTTON1_MASK|
                      InputEvent.BUTTON2_MASK|
                       InputEvent.BUTTON3_MASK;

  private DirectColorModel screenCapCM = null;

  public Robot() throws AWTException   {
          if (GraphicsEnvironment.isHeadless()) {
               throw new AWTException  ("headless environment");
        }
        init(GraphicsEnvironment.getLocalGraphicsEnvironment()
             .getDefaultScreenDevice());

      }
  private void init(GraphicsDevice   screen) throws AWTException   {
         checkRobotAllowed();
          Toolkit toolkit = Toolkit.getDefaultToolkit();
           if (toolkit instanceof ComponentFactory) {
               peer = ((ComponentFactory)toolkit).createRobot(this, screen);
            }
       }
  public Robot(GraphicsDevice   screen) throws AWTException   {
       checkIsScreenDevice(screen);
           init(screen);
   }

  private void checkRobotAllowed() {
     SecurityManager   security = System.getSecurityManager();
       if (security != null) {
           security.checkPermission(SecurityConstants.CREATE_ROBOT_PERMISSION);
        }
        }

  private void checkIsScreenDevice(GraphicsDevice   device) {
           if (device == null || device.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) {
                throw new IllegalArgumentException  ("not a valid screen device");
          }

  }
  public synchronized BufferedImage createScreenCapture(Rectangle   screenRect) {
        checkScreenCaptureAllowed();
       checkValidRect(screenRect);

       BufferedImage image;
        DataBufferInt buffer;
        WritableRaster raster;

        if (screenCapCM == null) {
            /*
   284          * Fix for 4285201 
   285          * Create a DirectColorModel equivalent to the default RGB ColorModel,
   286          * except with no Alpha component.
            */

           screenCapCM = new DirectColorModel(24,
                            /* red mask */ 0x00FF0000,
                           /* green mask */ 0x0000FF00,
                           /* blue mask */ 0x000000FF);
        }

        int pixels[];
       int[] bandmasks = new int[3];

        pixels = peer.getRGBPixels(screenRect);
        buffer = new DataBufferInt(pixels, pixels.length);

        bandmasks[0] = screenCapCM.getRedMask();
       bandmasks[1] = screenCapCM.getGreenMask();
       bandmasks[2] = screenCapCM.getBlueMask();

       raster = Raster.createPackedRaster(buffer, screenRect.width, screenRect.height, screenRect.width, bandmasks, null);

       image = new BufferedImage(screenCapCM, raster, false, null);

       return image;
       }
  private static void checkValidRect(Rectangle   rect) {
        if (rect.width <= 0 || rect.height <= 0) {
            throw new IllegalArgumentException  ("Rectangle width and height must be > 0");
        }
        }
  private static void checkScreenCaptureAllowed() {
       SecurityManager   security = System.getSecurityManager();
       if (security != null) {
           security.checkPermission(
            SecurityConstants.READ_DISPLAY_PIXELS_PERMISSION);
        }
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM