刚刚做的一个QQ找茬的外挂在一个按钮中注册下面的监听器,每点一次按钮,就会从当前屏幕中获得一个BufferedImage的对象(QQ找茬的两张图片的区域),然后比较图片的不同并更改不同处的颜色,再JLabel中显示出来。但是不管我是用JLabel.setIcon()
还是覆盖它的paint()方法使用Graphics类的drawImage() 来实现图像的显示,结果都是:在JLabel中显示图像的成功率很低,没搞懂是什么原因?
/** 监听按钮事件,处理屏幕图像的内部类 */
private class SeekListener implements ActionListener{
public void actionPerformed(ActionEvent e){
setVisible(false);

//得到当前屏幕的像素信息
try{
screenImage = new Robot().createScreenCapture(captureBounds);
}catch(AWTException ex){
ex.printStackTrace();
}

// 比较图像左右两边图像的颜色差别
for(int row = 0; row <= 445; ++row)
for(int col = 0; col <= 500;++col)
if(screenImage.getRGB(col+6,row) != screenImage.getRGB(col+515,row))
screenImage.setRGB(col+6,row,curColor.getRGB());

setVisible(true); display.setIcon(new ImageIcon(screenImage));   //  display是JLabel的实例


}
}

解决方案 »

  1.   

    我把源代码贴出来,有点乱!运行下,估计你们就知道什么问题了!(只是我不知道·······)运行条件:
    桌面分辨率:1024*768
    QQ找茬必须全屏
    (因为两张图片的位置是在上面两种情况找的坐标)
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.awt.image.ImageObserver;
    import javax.swing.*;
    import javax.swing.border.*;/** 一个找茬的外挂
    * @author snakewarhead
    * @version 0.5
    */
    public class HackGUI extends JFrame{
    private JLabel pointLabel;

    private JPanel menu;
    private JButton seek ; // 开始找茬
    private JPanel jp;

    private JComboBox selectColor ; // 颜色的选项
    private final String[] colorString ={"BLUE","BLACK","RED","GREEN"};
    private final Color[] colorValues = {Color.BLUE,Color.BLACK,Color.RED,Color.GREEN};
    private Color curColor = Color.BLUE;

    private JLabel display = new JLabel(); // 显示屏
    private BufferedImage screenImage; // 屏幕缓冲区

    private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    private GraphicsDevice gd = ge.getDefaultScreenDevice();
    private GraphicsConfiguration gc = gd.getDefaultConfiguration();
    // 屏幕的界限
    private Rectangle screenBounds = gc.getBounds();
    // 截图的界限
    private Rectangle captureBounds = new Rectangle(0,192,screenBounds.width,450);

    public HackGUI(String title){
    super(title);

    pointLabel = new JLabel();

    selectColor = new JComboBox(colorString);
    jp = new JPanel();
    jp.setBorder(new TitledBorder("选择颜色"));
    jp.add(selectColor);

    seek = new JButton("找茬");
    seek.addActionListener(new SeekListener());

    menu = new JPanel();
    menu.add(pointLabel);
    menu.add(seek);
    menu.add(jp);

    Container contentPane = getContentPane();
    contentPane.add(menu,"North");

    display.addMouseListener(new MouseAdapter(){ // 确定鼠标点击的位置
    public void mousePressed(MouseEvent e){
    Point abLocation = e.getLocationOnScreen();
    Point reLocation = e.getPoint();
    if(screenImage != null)
    pointLabel.setText("x坐标:"+reLocation.x+"y坐标:"+reLocation.y);
    }
    });
    contentPane.add(display,"Center");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // 设置为全屏
    setExtendedState(JFrame.MAXIMIZED_BOTH);

    setVisible(true);
    }


    /** 监听按钮事件,处理屏幕图像的内部类 */
    private class SeekListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    setVisible(false);

    //得到当前屏幕的像素信息
    try{
    screenImage = new Robot().createScreenCapture(captureBounds);
    }catch(AWTException ex){
    ex.printStackTrace();
    }

    // 比较图像左右两边图像的颜色差别
    for(int row = 0; row <= 445; ++row)
    for(int col = 0; col <= 500;++col)
    if(screenImage.getRGB(col+6,row) != screenImage.getRGB(col+515,row))
    screenImage.setRGB(col+6,row,curColor.getRGB());

    setVisible(true); display.setIcon(new ImageIcon(screenImage));

    //启动绘图线程
    // new PaintThread().start();

    }
    }

    /** 继承Label并能根据图像信息提供绘图方法的类 */
    /* private class ExLabel extends JLabel{
    public void paintComponent(Graphics g){
    super.paintComponent(g);

    // 第一次画组件时,图像的引用会是null
    if(screenImage != null){
    // 比较图像左右两边图像的颜色差别
    for(int row = 95; row <= 545; ++row)
    for(int col = 0; col <= 500;++col)
    if(screenImage.getRGB(col+6,row) != screenImage.getRGB(col+515,row))
    screenImage.setRGB(col+6,row,curColor.getRGB());

    System.out.println(g.drawImage(screenImage,0,0,this));
    g.dispose();
    }

    System.out.println("paint is action");
    } }
    */
    /** 继承线程类实现在Label上绘图的功能的类 */
    /* private class PaintThread extends Thread{
    public void run(){
    try{
    sleep(5000);
    System.out.println("sleep action");
    }catch(InterruptedException e){
    System.out.println("sleep interrupted");
    }
    display.repaint();
    }
    }
    */
    public static void main(String[] args){
    HackGUI hack = new HackGUI("找茬作弊器 by snakewarhead");

    }
    }