How to get the colour of a applet viewer pixel in Java? - java

I have drawn any picture, used Graphics 2D. How do I get the colour of a pixel at x, y? getPixelColor don't work, because this method get pixel from screen, not applet viewer coordinates.

Draw the picture to the Graphics of a BufferedImage
Draw the image to the Graphics2D.
To get the color of any pixel, call BufferedImage.getRGB(x,y) or variants (check the docs.).

Related

Creating image of Triangle with given co-ordinates

I want to image file containing polygon with given coordinates with white color inside the polygon and black color outside of given size. Size of the image and co-ordinates of the polygon are given. How can I write java code to output the image file. Will there be any inbuilt functions? Thank you in advance.
Create a Polygon and use the contains(int x, int y) inside a loop to figure out which pixels should be white and which should be black. Create a BufferedImage, and use createGraphics() to draw the correct colors to the image. Finally, check How to save a BufferedImage as a File for help with outputting the image file.

Android: Drawing bounding box on object in bitmap

I have a black and white threshold bitmap. How will I detect and draw a bounding box around an object? For example:
The red box that is the box that I need to have drawn. The box is there because in this bitmap, the background is black, and inside the box is where the majority of the actual image (white) is. How would I get the app to detect and draw the bounding box?

How to get background color of JPanel

I am trying to implement Connected Component Labeling algorithm using java swing library.
First, my code reads any B/W image from computer. Then, it iterates through image pixels (x,y) and returns specified pixel color.
The new image will be drawn on a JPanel. using Graphics library.
The code to draw a pixel is: g.fillRect(x, y, width, height);
The problem is that I am willing to get background color of the JPanel that I am drawing on it in order to fully implement this algorithm.

Rotate rectangle on an Image Swing

I have an Image on a JPanel. I'm then drawing a rectangle on top of the image like this:
Graphics2D image = (Graphics2D) g;
image.drawRect(......);
//create image code here.
image.rotate(1.5);
image.drawImage(....);
Problem is that when I rotate the image image.rotate(1.5), the rectangles stay in the same place.
I've tried creating the rectangle before rotating the image and after rotating the image, but both times it fails.
Is there an easy way to make the rectangles also rotate along with the image?
Thanks.
One approach is to rotate the graphics context's affine transform, as shown in this example. In that way, all drawing will be rotated by the same amount.
You might want to try using Rectangle, an implementation of the Rectangle2D class, then use g2d.draw(rectangle). This might be able to get the rotation state from the Graphics2D object better.

How to grab pixels of a rectangle

How to grab pixels of a rectangle and how to get color of grabbed pixels in Java...
(Assuming you want to grab pixels of a rectangle in an existing Image), have a look at the PixelGrabber class. The example in the Javadocs shows how to get the color of each grabbed pixel.

Categories