当前位置: 技术问答>java相关
如何取得一个像素点的颜色?
来源: 互联网 发布时间:2015-01-05
本文导语: 我想返回鼠标点击的点的颜色,怎么办? | //希望这个程序对你有用 import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; public class GetPixel { public PixelMadness() { ImageHol...
我想返回鼠标点击的点的颜色,怎么办?
|
//希望这个程序对你有用
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class GetPixel {
public PixelMadness() {
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 {
ImageIcon icon = new ImageIcon("zx2.jpg");
Image image = icon.getImage();
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
int[] pixelArray = new int[imageWidth * imageHeight];
PixelGrabber grabber = new PixelGrabber(image,0,0,imageWidth,imageHeight,pixelArray,0,imageWidth);
public ImageHolder() {
try { grabber.grabPixels(); }
catch(InterruptedException ie){
System.out.println("Pixel array not completed");
return;
}
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
int mouseX = (int)me.getPoint().getX();
int mouseY =(int) me.getPoint().getY();
int pixel = pixelArray[(mouseY * imageWidth) + mouseX];
getPixel(pixel);
}
});
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 pixel){
int alpha = (pixel >> 24)&0xff;
int red = (pixel >> 16)&0xff;
int green = (pixel >> 8)&0xff;
int blue = (pixel)&0xff;
System.out.println( "("+alpha+","+red+","+green+ ","+blue+ ")");
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class GetPixel {
public PixelMadness() {
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 {
ImageIcon icon = new ImageIcon("zx2.jpg");
Image image = icon.getImage();
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
int[] pixelArray = new int[imageWidth * imageHeight];
PixelGrabber grabber = new PixelGrabber(image,0,0,imageWidth,imageHeight,pixelArray,0,imageWidth);
public ImageHolder() {
try { grabber.grabPixels(); }
catch(InterruptedException ie){
System.out.println("Pixel array not completed");
return;
}
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
int mouseX = (int)me.getPoint().getX();
int mouseY =(int) me.getPoint().getY();
int pixel = pixelArray[(mouseY * imageWidth) + mouseX];
getPixel(pixel);
}
});
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 pixel){
int alpha = (pixel >> 24)&0xff;
int red = (pixel >> 16)&0xff;
int green = (pixel >> 8)&0xff;
int blue = (pixel)&0xff;
System.out.println( "("+alpha+","+red+","+green+ ","+blue+ ")");
}
}