Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get the RGB value of the image where the mouse is located by java

Shulou Source: shulou.com Published: 2022-06-02 03:46:23 10月24日 Update

This article mainly introduces java how to get the RGB value of the image where the mouse is located, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this java article on how to get the RGB value of the image where the mouse is located. Let's take a look.

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 (400400)

Frame.setVisible (true)

}

Public static void main (String [] args) {

GetPixel getpixel = new GetPixel ()

}

}

Class ImageHolder extends Canvas {

BufferedImage bi; / / create a buffer image

Image image

MediaTracker mt; / / create a media tracker

Int imageWidth; / / Image width

Int imageHeight;// image height

Public ImageHolder () {

Mt = new MediaTracker (this)

Try {

Image = Toolkit.getDefaultToolkit () .createImage ("gigi3.jpg")

Mt.addImage (image,0)

Mt.waitForAll (); / / wait until all the images are finished loading

ImageWidth = image.getWidth (this); / / get the width of the single front image

ImageHeight = image.getHeight (this); / / get the length of the current image

}

Catch (Exception e) {}

Bi = new BufferedImage (imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB)

AddMouseListener (new MouseAdapter () {

Public void mouseClicked (MouseEvent me) {

/ / get the horizontal and vertical coordinates of the current position of the mouse

Int mouseX = (int) me.getPoint () .getX ()

Int mouseY = (int) me.getPoint () .getY ()

/ / get the RGB value there

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 pencil int y) {

/ / get the color model of ImageHolder

ColorModel cm = this.getColorModel ()

/ / returns the pixel color component (ImageHolder) in the form of rgb.

Int rgb = cm.getRGB (xroomy)

/ / the values of rmag and b were obtained respectively.

Int red = (rgb > > 16) & 0xff

Int green = (rgb > > 8) & 0xff

Int blue = (rgb) & 0xff

System.out.println ("(" + red+ "," + green+ "," + blue+ "))

}

}

This is the end of the article on "how to get the RGB value of the image where the mouse is located by java". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to get the rgb value of the image where the mouse is located". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Image location mouse location knowledge content width form article color value media easy to operate article easy to understand more model look ordinate component Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft vpn Redmi MySQL