当前位置: 技术问答>java相关
怎样获得当前mouse坐标处的颜色??
来源: 互联网 发布时间:2015-01-10
本文导语: | //程序有误,更正如下。 import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; public class GetRectPixel { public GetRectPixel() { ImageHolder holder = new ImageHolder(); JFrame frame = new JFrame(); JPanel pane...
|
//程序有误,更正如下。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class GetRectPixel {
public GetRectPixel() {
ImageHolder holder = new ImageHolder();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(holder);
frame.getContentPane().add(panel);
frame.setSize(400,400);
frame.setVisible(true);
}
public static void main(String[] args) {
GetPixel getpixel = new GetPixel();
}
}
class ImageHolder extends Canvas{
BufferedImage bi ;
Image image;
MediaTracker mt ;
int imageWidth;
int imageHeight;
public ImageHolder() {
mt =new MediaTracker(this);
try{
image = Toolkit.getDefaultToolkit().createImage("gigi3.jpg");
mt.addImage(image,0);
mt.waitForAll();
imageWidth = image.getWidth(this);
imageHeight = image.getHeight(this);
}
catch(Exception e){}
bi = new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
int mouseX = (int)me.getPoint().getX();
int mouseY =(int) me.getPoint().getY();
getPixel(mouseX,mouseY);
}
});
setSize(imageWidth,imageHeight);
}
public void paint(Graphics g){
int xCenter = (getSize().width / 2) - (imageWidth / 2);
int yCenter = (getSize().height / 2) - (imageHeight / 2);
g.drawImage(image, xCenter, yCenter, imageWidth, imageHeight, this);
}
public void getPixel(int x,int y){
ColorModel cm = this.getColorModel();
int rgb = cm.getRGB(x*y);
int red = (rgb>>24)&0xff ;
int green = (rgb>>16)&0xff ;
int blue = (rgb>>8)&0xff ;
System.out.println( "("+red+","+green+ ","+blue+ ")");
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class GetRectPixel {
public GetRectPixel() {
ImageHolder holder = new ImageHolder();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(holder);
frame.getContentPane().add(panel);
frame.setSize(400,400);
frame.setVisible(true);
}
public static void main(String[] args) {
GetPixel getpixel = new GetPixel();
}
}
class ImageHolder extends Canvas{
BufferedImage bi ;
Image image;
MediaTracker mt ;
int imageWidth;
int imageHeight;
public ImageHolder() {
mt =new MediaTracker(this);
try{
image = Toolkit.getDefaultToolkit().createImage("gigi3.jpg");
mt.addImage(image,0);
mt.waitForAll();
imageWidth = image.getWidth(this);
imageHeight = image.getHeight(this);
}
catch(Exception e){}
bi = new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
int mouseX = (int)me.getPoint().getX();
int mouseY =(int) me.getPoint().getY();
getPixel(mouseX,mouseY);
}
});
setSize(imageWidth,imageHeight);
}
public void paint(Graphics g){
int xCenter = (getSize().width / 2) - (imageWidth / 2);
int yCenter = (getSize().height / 2) - (imageHeight / 2);
g.drawImage(image, xCenter, yCenter, imageWidth, imageHeight, this);
}
public void getPixel(int x,int y){
ColorModel cm = this.getColorModel();
int rgb = cm.getRGB(x*y);
int red = (rgb>>24)&0xff ;
int green = (rgb>>16)&0xff ;
int blue = (rgb>>8)&0xff ;
System.out.println( "("+red+","+green+ ","+blue+ ")");
}
}
|
用BufferedImage就可以了,如:
import java.awt.color.*;
....
BufferedImage bi = new BufferedImage(400,300,ColorSpace.TYPE_RGB);
//画图
Graphics g = bi.getGraphics();
g.drawString(...);
//取得象素
Color color = new Color(bi.getRGB(int x, int y));
再作判断就好了,你试试。
import java.awt.color.*;
....
BufferedImage bi = new BufferedImage(400,300,ColorSpace.TYPE_RGB);
//画图
Graphics g = bi.getGraphics();
g.drawString(...);
//取得象素
Color color = new Color(bi.getRGB(int x, int y));
再作判断就好了,你试试。