09-03
04
图片某点颜色抓取
作者:Java伴侣 日期:2009-03-04
复制内容到剪贴板 程序代码
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Test extends JFrame {
private static final long serialVersionUID = 1L;
JScrollPane scrollPane;
ImageIcon icon;
Image image;
Robot rb = null;
public Test() {
// ken.jpg要位于此java文件项目目录下,我的ide :eclipse
icon = new ImageIcon("f:/1.jpg");
JPanel panel = new JPanel() {
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g) {
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
panel.setOpaque(false);
panel.setPreferredSize(new Dimension(800, 600));
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
// 转化成#ffffff格式
Color c = rb.getPixelColor(x, y);
String r = Integer.toHexString(c.getRed());
String g = Integer.toHexString(c.getGreen());
String b = Integer.toHexString(c.getBlue());
if (r.length() == 1)
r += "0";
if (g.length() == 1)
g += "0";
if (b.length() == 1)
b += "0";
// color就是获得点颜色的字符串表示
String color = "#" + r + g + b;
System.out.println(color);
}
});
scrollPane = new JScrollPane(panel);
getContentPane().add(scrollPane);
init();
}
public void init() {
try {
rb = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Test frame = new Test();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Test extends JFrame {
private static final long serialVersionUID = 1L;
JScrollPane scrollPane;
ImageIcon icon;
Image image;
Robot rb = null;
public Test() {
// ken.jpg要位于此java文件项目目录下,我的ide :eclipse
icon = new ImageIcon("f:/1.jpg");
JPanel panel = new JPanel() {
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g) {
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
panel.setOpaque(false);
panel.setPreferredSize(new Dimension(800, 600));
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
// 转化成#ffffff格式
Color c = rb.getPixelColor(x, y);
String r = Integer.toHexString(c.getRed());
String g = Integer.toHexString(c.getGreen());
String b = Integer.toHexString(c.getBlue());
if (r.length() == 1)
r += "0";
if (g.length() == 1)
g += "0";
if (b.length() == 1)
b += "0";
// color就是获得点颜色的字符串表示
String color = "#" + r + g + b;
System.out.println(color);
}
});
scrollPane = new JScrollPane(panel);
getContentPane().add(scrollPane);
init();
}
public void init() {
try {
rb = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Test frame = new Test();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
评论: 0 | 引用: 0 | 查看次数: 509
发表评论