使用上稍微不太習(xí)慣,但總比到各個(gè)不靠譜的下載站下載個(gè)病毒的軟件要好多了,鼠標(biāo)點(diǎn)選要獲取的地方,然后點(diǎn)擊alt即可獲取到顏色值
效果圖如下:
Java取色器開(kāi)發(fā)背景:
自己的想法能用各種代碼的組合實(shí)現(xiàn)出來(lái),真的很有成就感啊,哈哈。
在你想要的顏色上面點(diǎn)下ALT鍵,就這么簡(jiǎn)單。
.exe文件使用exe4j做的,在我的機(jī)子上能用,沒(méi)有別的機(jī)子進(jìn)行測(cè)試,要是不能用,請(qǐng)通知我。
今天對(duì)前幾天做的取色器進(jìn)行了修改,去掉了一大堆重復(fù)的代碼,添加了一個(gè)JLabel,可以對(duì)選中的顏色進(jìn)行即時(shí)顯示,防止點(diǎn)錯(cuò),歡迎大家的使用。
經(jīng)過(guò)了這么多天,我對(duì)面向?qū)ο筮是不是很明白,寫的代碼的結(jié)構(gòu)也不好,誰(shuí)能幫我改一下啊,謝謝了。
更新日志:
v1.1
終于回到家了,對(duì)前幾天做的取色器進(jìn)行了修改,去掉了一大堆重復(fù)的代碼,
添加了一個(gè)JLabel,可以對(duì)選中的顏色進(jìn)行即時(shí)顯示,防止點(diǎn)錯(cuò),歡迎大家的使用。
經(jīng)過(guò)了這么多天,對(duì)面向?qū)ο筮是不是很明白,寫的代碼的結(jié)構(gòu)也不好,誰(shuí)能幫我改一下呢?
v1.0
經(jīng)過(guò)這么多天的學(xué)習(xí),這是我做出的第一個(gè)JAVA程序。自己的想法能用各種代碼的組合實(shí)現(xiàn)出來(lái),真的很有成就感啊,哈哈。
在你想要的顏色上面點(diǎn)下ALT鍵,就這么簡(jiǎn)單。
等有空了,我還想在這上面多加點(diǎn)功能,如果你有了好的想法,請(qǐng)告訴我一下。
謝謝。
代碼如下:
Java code
/**Java取色器 v1.1
終于回到家了,對(duì)前幾天做的取色器進(jìn)行了修改,去掉了一大堆重復(fù)的代碼,
添加了一個(gè)JLabel,可以對(duì)選中的顏色進(jìn)行即時(shí)顯示,防止點(diǎn)錯(cuò),歡迎大家的使用。
**/
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("關(guān)于");
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("十六進(jìn)制表示:");
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("復(fù)制");
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);
}
//把當(dāng)前選中的顏色的十六進(jìn)制表示復(fù)制到系統(tǒng)的剪貼板中,方便使用。
//輸入的參數(shù)就是待放入剪貼板的數(shù)據(jù)。
public static void setClipboard(String str)
{
StringSelection ss = new StringSelection(str);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}
//對(duì)按鈕和菜單進(jìn)行響應(yīng)。
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==JCopy)
{ if(s16=="")
{
JOptionPane.showMessageDialog(null,"請(qǐng)?jiān)谀阆胍念伾厦姘碅lt鍵進(jìn)行選取。","提示",JOptionPane.INFORMATION_MESSAGE);
}
else
{
setClipboard(s16);
JOptionPane.showMessageDialog(null,"你所選取的16進(jìn)制顏色代碼 "+s16+" 已經(jīng)復(fù)制到剪貼板中,請(qǐng)?jiān)谀阆胗玫牡胤桨碈trl+V粘貼。","提示",JOptionPane.INFORMATION_MESSAGE);
}
}
else if(e.getSource()==JExit)
{
JOptionPane.showMessageDialog(null,"謝謝您的使用,這是我用java做的第一個(gè)小程序,如果你在使用過(guò)程用有了什么好玩的想法,一定要告訴我!","謝謝使用",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if(e.getActionCommand()=="關(guān)于")
JOptionPane.showMessageDialog(this,"DarkMao 取色器 1.0\n和PS里的拾色器功能類似,但是使用方便,不用先截圖再打開(kāi)PS了,希望你能喜歡。\n想知道鼠標(biāo)在的這一點(diǎn)的顏色嗎?點(diǎn)下ALT鍵看看吧。\n如果你在使用過(guò)程中有了什么主意的話,請(qǐng)告訴我一下,謝謝。\nE-mail: 2whol@163.com","關(guān)于我",JOptionPane.INFORMATION_MESSAGE);
}
//當(dāng)按鍵放開(kāi)的時(shí)候才對(duì)案件進(jìn)行響應(yīng),如果不想一下一下的點(diǎn),
//可以把里邊的代碼剪切到keyPressed()中,這樣只要按下alt鍵同時(shí)移動(dòng)鼠標(biāo)就可以了。
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);//得到顏色的十六進(jìn)制表示。
JTF16.setText(s16);
Color col=new Color(R,G,B);
JCol.setForeground(col);//對(duì)當(dāng)前選中的顏色進(jìn)行顯示。
}catch(AWTException ex)
{
ex.printStackTrace();
}
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
}
}
class CT
{
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);
}
}