标题:[转载]Java屏幕取色工具
只看楼主
longziyong
Rank: 2
来 自:四川省威远县
等 级:禁止访问
帖 子:77
专家分:23
注 册:2007-8-22
 问题点数:0 回复次数:9 
[转载]Java屏幕取色工具
程序功能: 获取屏幕上任意一点的颜色值,并分别显示出该颜色的RGB值和十六进制值。

程序运行结果如下图所示:





源代码地址: http://pdfbbs.com/java/display.asp?id=140
附件: 游客没有浏览附件的权限,请 登录注册
搜索更多相关主题的帖子: Java 屏幕 源代码 工具 RGB 
2007-10-14 11:03
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
得分:0 
不错..

我的msn: myfend@
2007-10-14 11:07
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
得分:0 
建议开源.

我的msn: myfend@
2007-10-14 11:09
dadoujiao
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2007-9-28
得分:0 
呵呵
能把源码发一下吗?
2007-10-14 11:12
longziyong
Rank: 2
来 自:四川省威远县
等 级:禁止访问
帖 子:77
专家分:23
注 册:2007-8-22
得分:0 
源码是公开的,只不过在我的个人主页上。


我也借此提高一下网站的流量撒!

LZY'Blog http://www.
2007-10-14 11:13
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
得分:0 
呵呵..不错啊..
加油哦.

我的msn: myfend@
2007-10-14 11:14
缘吇弹
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:地球
等 级:版主
威 望:43
帖 子:3038
专家分:27
注 册:2007-7-2
得分:0 

我来开源:


/*Java屏幕取色工具*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.awt.image.*;

public class GetColor extends JFrame implements ActionListener{
JLabel RGB_label,Hex_label,mouse_track_label;
JTextField RGB_jtf,Hex_jtf;
JButton start,explain;
JPanel jpcolor;

//初始化程序主窗口
public GetColor(){
Container contentPane=this.getContentPane();
GridBagLayout gbLayout=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
contentPane.setLayout(gbLayout);
c.weightx=1.0;
c.weighty=1.0;
c.gridx=0;
c.gridy=0;
c.fill=GridBagConstraints.BOTH;
Hex_label=new JLabel("十六进制值:");
gbLayout.setConstraints(Hex_label,c);
contentPane.add(Hex_label);

c.gridx=0;
c.gridy=1;
Hex_jtf=new JTextField();
gbLayout.setConstraints(Hex_jtf,c);
contentPane.add(Hex_jtf);

c.gridx=0;
c.gridy=2;
c.gridwidth=1;
RGB_label=new JLabel("RGB值:");
gbLayout.setConstraints(RGB_label,c);
contentPane.add(RGB_label);

c.gridx=1;
c.gridy=1;
ImageIcon im=new ImageIcon("buttonicon.png");
start=new JButton(im);
start.addActionListener(this);
gbLayout.setConstraints(start,c);
contentPane.add(start);

c.gridx=2;
c.gridy=1;
jpcolor=new JPanel();
jpcolor.setBackground(Color.red);
gbLayout.setConstraints(jpcolor,c);
contentPane.add(jpcolor);

c.gridx=0;
c.gridy=3;
c.gridwidth=2;
RGB_jtf=new JTextField();
gbLayout.setConstraints(RGB_jtf,c);
contentPane.add(RGB_jtf);


c.gridx=2;
c.gridy=3;
c.gridwidth=1;
explain=new JButton("说明");
explain.addActionListener(this);
gbLayout.setConstraints(explain,c);
contentPane.add(explain);

c.gridx=0;
c.gridy=4;
c.gridwidth=3;
mouse_track_label=new JLabel("鼠标轨迹:");
gbLayout.setConstraints(mouse_track_label,c);
contentPane.add(mouse_track_label);

this.setTitle("Java取色器");
this.setAlwaysOnTop(true);
this.setSize(250,150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public static void main(String args[]){
GetColor gc=new GetColor();
}

//接收按钮事件
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==start){
doStart();
} else if(ae.getSource()==explain){
doExplain();
}
}

//更新保存当前取色,即确定取色
private void updates(Color bg){
jpcolor.removeAll();
jpcolor.setBackground(bg);
this.setResizable(true);
this.setCursor(Cursor.DEFAULT_CURSOR);
SwingUtilities.updateComponentTreeUI(this);
}

//开始取色按钮事件对应的方法
private void doStart(){
try{
Robot ro=new Robot();
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension di=tk.getScreenSize();
Rectangle rec=new Rectangle(0,0,di.width,di.height);
BufferedImage bi=ro.createScreenCapture(rec);
JFrame jf=new JFrame();
jf.getContentPane().add(new Temp(jf,bi,di.width,di.height));
jf.setUndecorated(true);
jf.setSize(di);
jf.setVisible(true);
this.setResizable(false);
}catch(Exception exe){
exe.printStackTrace();
}
}

// 说明信息
private void doExplain(){
String copyright;
copyright="Java屏幕取色器:\n单击取色按钮开始取色,在程序窗口以外再次单击鼠标以确定取色!\n"+"由缘吇弹转载开源";
copyright=copyright+" 愿与您共同学习,共同进步\n"+"欢迎访问我的主页:http://hi.baidu.com/yzidan\n";
JOptionPane.showMessageDialog(null,copyright);
}

//一个暂时类,用于显示当前的屏幕图像
private class Temp extends JPanel implements MouseListener,MouseMotionListener{
private BufferedImage bi;
private int width,height;
private JFrame jf;

public Temp(JFrame jf,BufferedImage bi,int width,int height){
this.jf=jf;
this.bi=bi;
this.width=width;
this.height=height;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}

public void paintComponent(Graphics g){
jf.setCursor(Cursor.CROSSHAIR_CURSOR);
g.drawImage(bi,0,0,width,height,this);
}

public void mouseMoved(MouseEvent me){
pickColor();
}

// 获取鼠标指针当前颜色并显示
public Color pickColor() {
Color pixel = new Color(0,0,0);
Robot robot = null;
Point mousepoint;
int R,G,B;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
System.exit(1);
}
mousepoint = MouseInfo.getPointerInfo().getLocation();
pixel = robot.getPixelColor(mousepoint.x,mousepoint.y);
R = pixel.getRed();
G = pixel.getGreen();
B = pixel.getBlue();
jpcolor.setBackground(pixel);
RGB_jtf.setText("R="+R+",G="+G+",B="+B);
Hex_jtf.setText("#"+Integer.toHexString(R)+Integer.toHexString(G)+Integer.toHexString(B));
mouse_track_label.setText("鼠标轨迹:x="+mousepoint.x+",y="+mousepoint.y);
return pixel;
}

public void mouseExited(MouseEvent me){

}

public void mouseEntered(MouseEvent me){

}

public void mouseDragged(MouseEvent me){

}

public void mousePressed(MouseEvent me){

}

public void mouseReleased(MouseEvent me){

}

public void mouseClicked(MouseEvent me){
jf.dispose();
updates(pickColor());
}
}
}

[此贴子已经被作者于2007-10-14 11:18:39编辑过]


Repeat  Life=Study;Until (death);
2007-10-14 11:17
longziyong
Rank: 2
来 自:四川省威远县
等 级:禁止访问
帖 子:77
专家分:23
注 册:2007-8-22
得分:0 

楼上的你行啊,那你能不能也把

JAVA应用程序发布工具的源码也开了呢?那可是五页哦


LZY'Blog http://www.
2007-10-14 11:51
hs_lqf
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-10-14
得分:0 
8错
2007-10-14 11:56
thanatos
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-10-16
得分:0 
回复:(缘吇弹)[em02]我来开源:/*Java屏幕取色工具*...
厉害!

2007-10-18 11:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-177469-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.218113 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved