Java 在android中创建我自己的机器人类时出错
我已经决定在android中创建我自己的Java的Robot类来进行屏幕截图。我已经自己编写了Robot类的源代码,但问题在于,代码中的以下行抛出编译错误。说“方法createRobot(Robot,GraphicsDevice)在类型中,ComponentFactory不适用于参数(Robot、GraphicsDevice)“ peer=((组件工厂)工具箱).createRobot(这个,屏幕) 谁能告诉我解决方案是什么Java 在android中创建我自己的机器人类时出错,java,android,Java,Android,我已经决定在android中创建我自己的Java的Robot类来进行屏幕截图。我已经自己编写了Robot类的源代码,但问题在于,代码中的以下行抛出编译错误。说“方法createRobot(Robot,GraphicsDevice)在类型中,ComponentFactory不适用于参数(Robot、GraphicsDevice)“ peer=((组件工厂)工具箱).createRobot(这个,屏幕) 谁能告诉我解决方案是什么 谢谢。这是我的完整源代码 包com.example.awt 导入jav
谢谢。这是我的完整源代码 包com.example.awt 导入java.awt.AWTException; 导入java.awt.GraphicsDevice; 导入java.awt.GraphicsEnvironment; 导入java.awt.Rectangle; 导入java.awt.peer。; 导入java.awt.image。; 导入java.awt.event.*; 导入java.lang.reflect.InvocationTargetException //导入com.sun.media.sound.Toolkit 进口sun.awt.Component工厂; //导入sun.awt.SunToolkit; 导入sun.security.util.SecurityConstants; 导入java.awt.Toolkit 公共类机器人{
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);
}
}
private static final int MAX_DELAY=60000;
私有机器人对等;
私有布尔值isAutoWaitForIdle=false;
私有int自动延迟=0;
私有静态最终整数合法按钮掩码=
InputEvent.BUTTON1\u掩码|
InputEvent.BUTTON2\u掩码|
InputEvent.BUTTON3\u掩码;
私有DirectColorModel screenCapCM=null;
public Robot()抛出AWTException{
if(GraphicsEnvironment.ishadless()){
抛出新的AWTException(“无头环境”);
}
init(GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice());
}
私有void init(图形设备屏幕)引发AWTException{
checkRobotAllowed();
Toolkit=Toolkit.getDefaultToolkit();
if(组件工厂的工具箱实例){
peer=((组件工厂)工具箱).createRobot(这个,屏幕);
}
}
公共机器人(图形设备屏幕)抛出AWTException{
检查屏幕设备(屏幕);
初始化(屏幕);
}
私有void checkRobotAllowed(){
SecurityManager security=System.getSecurityManager();
if(安全性!=null){
security.checkPermission(SecurityConstants.CREATE\u ROBOT\u权限);
}
}
专用无效支票筛选设备(图形设备){
if(device==null | | | device.getType()!=GraphicsDevice.TYPE(光栅)屏幕){
抛出新的IllegalArgumentException(“不是有效的屏幕设备”);
}
}
公共同步缓冲区映像CreateScreateScreenCapture(矩形screenRect){
checkScreenCaptureAllowed();
checkValidRect(screenRect);
缓冲图像;
数据缓冲区;
可写光栅;
如果(screenCapCM==null){
/*
284*修复4285201
285*创建与默认RGB ColorModel等效的DirectColorModel,
286*除了没有α分量。
*/
screenCapCM=新的DirectColorModel(24,
/*红色遮罩*/0x00FF0000,
/*绿色遮罩*/0x0000FF00,
/*蓝色遮罩*/0x000000FF);
}
整数像素[];
int[]带掩码=新int[3];
像素=peer.getRGBPixels(screenRect);
缓冲区=新的数据缓冲区(像素,像素.长度);
bandmasks[0]=screenCapCM.getRedMask();
bandmasks[1]=screenCapCM.getGreenMask();
bandmasks[2]=screenCapCM.getBlueMask();
raster=raster.createPackedRaster(缓冲区、screenRect.width、screenRect.height、screenRect.width、带掩码、空);
图像=新的缓冲区图像(screenCapCM、光栅、假、空);
返回图像;
}
私有静态void checkValidRect(矩形rect){
如果(rect.width)是它所说的意思,请确保“this”是Robot的一个实例,screen是GraphicsDevice的一个实例。查看我的完整代码的答案这应该是对原始问题的编辑,因为它不是一个答案,而是一个澄清。这并不是说我不知道你在做什么,我的java内件已经过时了。你正在传递你的这个指针指向你的事实从构造函数中删除。在将robot类传递给createRobot()之前,是否确定已完全初始化robot类?