package com.image;
/**Java取色器 v1.1
终于回到家了,对前几天做的取色器进行了修改,去掉了一大堆重复的代码,
添加了一个JLabel,可以对选中的颜色进行即时显示,防止点错,欢迎大家的使用。
**/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.MouseInfo.*;
import java.awt.datatransfer.*;
public class ColorTest extends JFrame implements ActionListener, KeyListener {
Robot robot;
Point mousepoint;
Integer R = 0, G = 0, B = 0;
Integer X = 0, Y = 0;
Color pixel = new Color(0, 0, 0);
String s16 = "";
MouseInfo mouseinfo = null;
JLabel JR, JG, JB, JX, JY, J16, JCol;
JTextField JTFR, JTFG, JTFB, JTFX, JTFY, JTF16;
JButton JCopy, JExit;
JPanel pix = new JPanel(), zb = new JPanel(), pb = new JPanel();
// 主窗口上菜单的建造。
public void setMenuBar() {
JMenuBar myBar = new JMenuBar();
JMenu helpMenu = new JMenu("帮助");
JMenuItem help_About = new JMenuItem("关于");
this.setJMenuBar(myBar);
myBar.add(helpMenu);
helpMenu.add(help_About);
help_About.addActionListener(this);
}
// 界面各种组件的初始化以及布局。
public void ColorTest() {
setTitle("DarkMao");
JR = new JLabel("R: ");
JG = new JLabel("G: ");
JB = new JLabel("B: ");
JX = new JLabel("X: ");
JY = new JLabel("Y: ");
J16 = new JLabel("十六进制表示:");
JCol = new JLabel("■■■■■");
JTFR = new JTextField(5);
JTFG = new JTextField(5);
JTFB = new JTextField(5);
JTF16 = new JTextField(6);
JTFX = new JTextField(5);
JTFY = new JTextField(5);
JCopy = new JButton("复制");
JExit = new JButton("退出");
setFocusable(true);
addKeyListener(this);
JCopy.addActionListener(this);
JExit.addActionListener(this);
JCopy.addKeyListener(this);
JExit.addKeyListener(this);
JTFR.addKeyListener(this);
JTFG.addKeyListener(this);
JTFB.addKeyListener(this);
JTF16.addKeyListener(this);
JTFX.addKeyListener(this);
JTFY.addKeyListener(this);
pix.setLayout(new FlowLayout());
zb.setLayout(new FlowLayout());
pb.setLayout(new FlowLayout());
pix.add(JR);
pix.add(JTFR);
pix.add(JG);
pix.add(JTFG);
pix.add(JB);
pix.add(JTFB);
zb.add(JX);
zb.add(JTFX);
zb.add(JY);
zb.add(JTFY);
pix.add(JCol);
pix.add(J16);
pix.add(JTF16);
pb.add(JCopy);
pb.add(JExit);
setLayout(new BorderLayout());
add(pix, BorderLayout.CENTER);
add(zb, BorderLayout.NORTH);
add(pb, BorderLayout.SOUTH);
}
// 把当前选中的颜色的十六进制表示复制到系统的剪贴板中,方便使用。
// 输入的参数就是待放入剪贴板的数据。
public static void setClipboard(String str) {
StringSelection ss = new StringSelection(str);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}
// 对按钮和菜单进行响应。
public void actionPerformed(ActionEvent e) {
if (e.getSource() == JCopy) {
if (s16 == "") {
JOptionPane.showMessageDialog(null, "请在你想要的颜色上面按Alt键进行选取。",
"提示", JOptionPane.INFORMATION_MESSAGE);
} else {
setClipboard(s16);
JOptionPane.showMessageDialog(null, "你所选取的16进制颜色代码 " + s16
+ " 已经复制到剪贴板中,请在你想用的地方按Ctrl+V粘贴。", "提示",
JOptionPane.INFORMATION_MESSAGE);
}
} else if (e.getSource() == JExit) {
JOptionPane.showMessageDialog(null,
"谢谢您的使用,这是我用java做的第一个小程序,如果你在使用过程用有了什么好玩的想法,一定要告诉我啊!",
"谢谢使用", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if (e.getActionCommand() == "关于")
JOptionPane
.showMessageDialog(
this,
"DarkMao 取色器 1.0\n和PS里的拾色器功能类似,但是使用方便,不用先截图再打开PS了,希望你能喜欢。\n想知道鼠标在的这一点的颜色吗?点下ALT键看看吧。\n如果你在使用过程中有了什么主意的话,请告诉我一下,谢谢。\nE-mail: 2whol@163.com",
"关于我", JOptionPane.INFORMATION_MESSAGE);
}
// 当按键放开的时候才对案件进行响应,如果不想一下一下的点,
// 可以把里边的代码剪切到keyPressed()中,这样只要按下alt键同时移动鼠标就可以了。
public void keyReleased(KeyEvent e) {
// int code=e.getKeyCode();
// System.out.println(code);
if (e.getKeyCode() == 18) {
try {
Robot robot = new Robot();
mousepoint = mouseinfo.getPointerInfo().getLocation();
// System.out.println(mousepoint);
pixel = robot.getPixelColor(mousepoint.x, mousepoint.y);
X = mousepoint.x;
Y = mousepoint.y;
R = pixel.getRed();
G = pixel.getGreen();
B = pixel.getBlue();
JTFR.setText(R.toString());
JTFG.setText(G.toString());
JTFB.setText(B.toString());
JTFX.setText(X.toString());
JTFY.setText(Y.toString());
// System.out.println(pixel);
s16 = "#" + Integer.toHexString(R) + Integer.toHexString(G)
+ Integer.toHexString(B);// 得到颜色的十六进制表示。
JTF16.setText(s16);
Color col = new Color(R, G, B);
JCol.setForeground(col);// 对当前选中的颜色进行显示。
} catch (AWTException ex) {
ex.printStackTrace();
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public static void main(String[] args) {
ColorTest ct = new ColorTest();
ct.setMenuBar();
ct.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ct.setSize(300, 200);
ct.setLocation(200, 200);
ct.ColorTest();
ct.setVisible(true);
}
}