图像绘制在错误的位置 [英] The image gets drawn at the wrong position

查看:96
本文介绍了图像绘制在错误的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是

java.awt.Robot 用于获取屏幕截图的线程的最终输出,并将其存储在矢量中。屏幕上的包含光标位置。作为一种解决方法,我使用 MouseInfo 类获取 PointerInfo ,然后获取一个 Point 。然后在该点绘制图像。

如果记录区域设置为全屏分辨率,则一切都很好。但是,如果我更改记录区域,则会将光标绘制在错误的位置。

此黑色光标应该位于Eclipse IDE的 Play 按钮顶部。但是它处于错误的位置。

This is the final output of my thread that takes screenshots and stores them in a vector.
java.awt.Robot is used to take screenshots which are basically rasters of the screen and does not contain cursor position. As a way around, I use the MouseInfo class to get PointerInfo and then get a Point. Then draw an image at that point.
All is cool if recording area is set to full screen resolution. However if I change the recording area, the cursor gets drawn at the wrong position.
This black cursor is supposed to be at the Play button of the Eclipse IDE, at the top. However it is at the wrong position.

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;
}  

如果光标超出记录范围,则不得绘制。我的意思是,您说对了吗?

If the cursor goes out of the recording bounds, it must not be drawn. I mean, you get the point right?

推荐答案

当然,您必须在相对位置而非全局位置上绘制光标。

Of course you must draw a cursor in a relative position - not global:

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

可让您在屏幕上(全局)鼠标位置。
图像坐标中的鼠标位置(相对)如下:

gives you mouse position on screen (global). Mouse position in your image coordinates (relative) would be something like:

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

这篇关于图像绘制在错误的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆