溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java如何獲得鼠標(biāo)所在位置的圖像的rgb值

發(fā)布時(shí)間:2022-01-07 20:25:06 來源:億速云 閱讀:240 作者:iii 欄目:編程語言

這篇文章主要介紹了java如何獲得鼠標(biāo)所在位置的圖像的rgb值的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇java如何獲得鼠標(biāo)所在位置的圖像的rgb值文章都會(huì)有所收獲,下面我們一起來看看吧。

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 ;//創(chuàng)建緩沖區(qū)圖像
Image image;
MediaTracker mt ;//創(chuàng)建媒體跟蹤器
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);//獲得當(dāng)前圖像的長度
}
catch(Exception e){}
bi = new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
//獲得鼠標(biāo)當(dāng)前位置的橫,縱坐標(biāo)
int mouseX = (int)me.getPoint().getX();
int mouseY =(int) me.getPoint().getY();
//獲得該處的rgb值
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){
  //獲得ImageHolder的色彩模型
  ColorModel cm = this.getColorModel();
  //把象素形式的色彩組件(ImageHolder)以rgb的形式返回。
  int rgb = cm.getRGB(x*y);
  //分別獲得其r,g,b值。
  int red = (rgb>>16)&0xff ;
  int green = (rgb>>8)&0xff ;
  int blue = (rgb)&0xff ;
  System.out.println( "("+red+","+green+ ","+blue+ ")");
 }
}

關(guān)于“java如何獲得鼠標(biāo)所在位置的圖像的rgb值”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“java如何獲得鼠標(biāo)所在位置的圖像的rgb值”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI