屏幕取点工具 elcipse下运行

package com.test;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.Timer;



public class test extends JFrame implements KeyListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int xPoint; // 鼠标实时坐标点
	private int yPoint;
	private JLabel xShow,yShow; //坐标显示
	private JLabel tipLab;      //提示信息
	private  boolean  flag = false; 
	private Robot robot;
	private JLabel imgPixe,imgScreen; //点的像素    图像
	private Rectangle rect;
	private JPopupMenu popupMenu;
	public test(){
		super("屏幕取点工具");
		xShow = new JLabel(String.valueOf(xPoint));
		xShow.setBounds(200, 30,40,40);
		yShow = new JLabel(String.valueOf(yPoint));
		yShow.setBounds(245,30,40, 40);
		tipLab = new JLabel(" ");
//		tipLab.setFont(new Font("宋体",14,Font.PLAIN));
		tipLab.setBounds(185, 60, 140, 40);
		rect =new Rectangle();
		Container c = this.getContentPane();
		try{
			robot = new Robot();
		}catch(AWTException e){
			e.printStackTrace();
		}
		imgPixe = new JLabel(" ");
		imgPixe.setBounds(10,10,80,80);
		imgPixe.setOpaque(true);
		imgPixe.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		this.add(imgPixe);
		imgScreen = new JLabel(" ");
		imgScreen.setBounds(100, 10, 80, 80);
		imgScreen.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		this.add(imgScreen);
		c.setLayout(null);
//		imgPane.setLayout(new BoxLayout(imgPane,BoxLayout.X_AXIS));
//		imgPane.add(Box.createHorizontalStrut(10));
		//弹出菜单
		popupMenu = new JPopupMenu();   
		// 增加菜单项到菜单上  
	    JMenu menuColor = new JMenu("复制颜色");
	    JMenuItem item1 = new JMenuItem("红(RED)");
	    JMenuItem item2 = new JMenuItem("绿(GREEN)");
	    JMenuItem item3 = new JMenuItem("蓝(BLUE)");
	    menuColor.add(item1);
	    menuColor.add(item2);
	    menuColor.add(item3);
	    popupMenu.add(menuColor);
	    
	    JMenu menuPoint = new JMenu("复制坐标");
	    JMenuItem item4 = new JMenuItem("X坐标");
	    JMenuItem item5 = new JMenuItem("Y坐标");
	    menuPoint.add(item4);
	    menuPoint.add(item5);
	    popupMenu.add(menuPoint);
		
        JLabel xLab = new JLabel("X:");
        xLab.setBounds(185, 40, 20, 20)	;
		c.add(xLab);
		c.add(xShow);
		JLabel yLab = new JLabel("Y:");
		c.add(yLab);
		yLab.setBounds(230, 40, 20, 20)	;
		c.add(yShow);
		c.add(tipLab);
		this.addKeyListener(this);
		showTime();
		new Timer(2000,new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				flag =!flag;
			}
			
		}).start();
//		String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
//		UIManager.LookAndFeelInfo
		this.setSize(330, 140);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	private void showTime()
	{
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true){
					xPoint = MouseInfo.getPointerInfo().getLocation().x;
					yPoint = MouseInfo.getPointerInfo().getLocation().y;
//					xShow.setBackground(Toolkit.getDefaultToolkit().gets);
	   			    Color bgColor = robot.getPixelColor(xPoint, yPoint);
	   			    rect.setRect(xPoint-10, yPoint-10, 20, 20);

	   			    BufferedImage bImg = robot.createScreenCapture(rect);
	   			    BufferedImage newImg = extendImg(bImg,4);
	   			    imgScreen.setIcon(new ImageIcon(newImg));
	   			    imgPixe.setBackground(bgColor);
					xShow.setText(String.valueOf(xPoint));
					yShow.setText(String.valueOf(yPoint));
//					System.out.println(xPoint + yPoint +"");
					if(!flag){
						test.this.tipLab.setText("按ctrl+1弹出选项");
					}else{
						test.this.tipLab.setText("按ctrl+方向键移动鼠标");
					}
				}
			}	
		}).start();
	}
	// 图像放大
	private BufferedImage extendImg(BufferedImage oldImg,Integer times){
		  int width = oldImg.getWidth()*times;
		  int height = oldImg.getHeight()*times;
		  BufferedImage newImg = new BufferedImage(width,height,oldImg.getType());
			   
		  Graphics g = newImg.getGraphics();
	      g.drawImage(oldImg, 0,0,width,height,null);
	      g.dispose();
	      
	      return newImg;
	}
	public static void main(String[] args){
		new test();
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_1){
			System.out.println("Win+1");
//			popupMenu.setLocation(100, 100);
			popupMenu.show(e.getComponent(), 300, 200);
		}
		if(e.isControlDown()&& e.getKeyCode()==KeyEvent.VK_UP){
			yPoint -=1;
			robot.mouseMove(xPoint, yPoint);
		}else if(e.isControlDown()&& e.getKeyCode()==KeyEvent.VK_DOWN){
			yPoint +=1;
			robot.mouseMove(xPoint, yPoint);
		}else if(e.isControlDown()&& e.getKeyCode()==KeyEvent.VK_LEFT){
			xPoint -=1;
			robot.mouseMove(xPoint, yPoint);
		}else if(e.isControlDown()&& e.getKeyCode()==KeyEvent.VK_RIGHT){
			xPoint +=1;
			robot.mouseMove(xPoint, yPoint);
		}

	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值