图像被绘制在错误的位置

时间:2012-12-23 21:11:39

标签: java jframe bufferedimage

enter image description here

这是我的线程的最终输出,它截取屏幕截图并将它们存储在矢量中 java.awt.Robot用于获取屏幕截图,屏幕截图基本上是栅格,包含光标位置。作为解决方法,我使用MouseInfo类获取PointerInfo,然后获得Point。然后在那一点画一幅图像 如果录制区域设置为全屏分辨率,则一切都很酷。但是,如果我更改录制区域,光标将被绘制在错误的位置 这个黑色光标应该位于Eclipse IDE的 Play 按钮的顶部。但它处于错误的位置。

如何在正确的位置绘制它?

代码:

package demo;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;

public class ScreenCapturingThread extends Thread{
    public ScreenCapturingThread(Vector<BufferedImage> screenShots,
            int frameRate,
            Icon cursor,
            Rectangle recordingArea){
        this.screenShots = screenShots;
        this.frameRate = frameRate;
        this.cursor = cursor;
        this.recordingArea = recordingArea;

        try{
            bot = new Robot();
        }catch(Exception e){
            System.out.println(e);
        }
        calculateSleepTime();
    }
    @Override
    public void run(){
        while(keepCapturing == true){
            try{
                screenShots.insertElementAt(takeScreenShot(), 0);
                sleep(sleepTime);

                keepCapturing = false; //take only one shot

                System.out.println("here");
                JFrame frame = new JFrame("blah");
                frame.setVisible(true);
                JLabel label = new JLabel(new ImageIcon(screenShots.firstElement()));
                frame.setSize(recordingArea.width, recordingArea.height);
                frame.add(label);


            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
    public BufferedImage takeScreenShot(){
        p = m.getPointerInfo();
        Point location = p.getLocation();
        image = bot.createScreenCapture(recordingArea);
        if(cursor!=null){
            Graphics g = image.getGraphics();
            g.drawImage(((ImageIcon)cursor).getImage(), location.x,location.y,null);
        }
        return image;
    }
    public void stopIt(){
        keepCapturing = false;
    }
    public void calculateSleepTime(){
        sleepTime = 1/frameRate;
    }

    public static void main(String[] args) {
        Vector<BufferedImage> bufferedImages = new Vector<>(100);
        int frameRate = 10;
        Icon cursor = (Icon) new ImageIcon("src/images/blackCursor.png");
        Rectangle r = new Rectangle(1280,800);
        r.x = 200;
        r.y = 200;
        ScreenCapturingThread sc = new ScreenCapturingThread(bufferedImages,frameRate,cursor,r);

        sc.start();
    }
    Vector<BufferedImage> screenShots;
    int frameRate;
    long sleepTime;
    boolean keepCapturing = true;
    Icon cursor;
    Rectangle recordingArea;
    Robot bot;
    MouseInfo m;
    PointerInfo p;
    BufferedImage image;
}  

如果光标超出记录范围,则不能绘制。我的意思是,你明白了吗?

以全屏分辨率输出

enter image description here

2 个答案:

答案 0 :(得分:1)

当然,您必须在相对位置绘制光标 - 而不是全局:

    p = m.getPointerInfo();
    Point location = p.getLocation();

为您提供屏幕上的鼠标位置(全局)。 图像坐标(相对)中的鼠标位置类似于:

    int x = (int) (p.x - recordingArea.getX()); //for int y similar
    if(x < 0 || y < 0 || y >= maxHeight || x >= maxWidth) { // don't draw cursor }

答案 1 :(得分:1)

您正在将指针绘制在与运行按钮相同的位置,因为您无法检查指针的位置是否在您的录制范围内。此外,您可能希望根据录制范围缩放指针的位置。

例如,您可以通过执行以下操作来检查指针是否从查看矩形中向左或向上(偏移变量基本上与左侧和顶部有多远,您是否开始录制):

p = m.getPointerInfo();
Point location = p.getLocation();
image = bot.createScreenCapture(recordingArea);
//Check location here.
if( location.x < recordingArea.x + <<offset x variable>> || 
    location.y < recordingArea.y + <<offset y variable>>){
     //Code to change cursor position to outside of viewing rectangle here.
}
if(cursor!=null){
      Graphics g = image.getGraphics();
      g.drawImage(((ImageIcon)cursor).getImage(), location.x,location.y,null);
}