Creating image of Triangle with given co-ordinates - java

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.

Related

Java: change alpha of BufferedImage with a polygon

I have a buffered image which has a set alpha and would like to change the alpha of the buffered image but only in a specific polygon.
For example the buffered image has an alpha of 200 and i want to add 20 to all the pixels on the buffered image within(or colliding with) the polygon.
I couldn't find any which does this exactly in the graphics class or on Google.
I would appreciate any form of help :D
Thanks!

Simple image rotation

I am making a game in Java. I made a planet seen from outer space and I want to make it appear like the planet is slowly rotating. But I don't know how to rotate a image. I need a simple command that rotates my image 1 degree around its own center once. Any help?
This is what I want to do:
Image
Take a look at these tutorials:
Java2D: Have Fun With Affine Transform
Coordinate Translations and Rotations: Example Code
Transforming Shapes, Text, and Images
What you are describing is not rotating an image, but changing an image to represent a 3D rotation of the object in the image.
Ideally you wouldn't be working with this as an image but rather as a 3D object with a different camera angle. Then you would simply rotate the camera around the object and display the resulting image to the user.
However if you're set on doing this as an image, then you need to create a different images representing various states of rotation of your planet and have a separate thread that would replace the displayed image with the next one in sequence, at repeated intervals. Search the web for "java image animation" - there are plenty of tutorials on how to do this.
If you want to rotate an image in 2d space, you can use something like this:
Image image = ...
Graphics2D g2d = ...; //
g2d.translate(170, 0); // If needed
g2d.rotate(1); // Rotate the image by 1 radian
//or g2d.rotate(180.0/3.14); to rotate by 1 degree
g2d.drawImage(image, 0, 0, 200, 200, observer);

How to get the colour of a applet viewer pixel in 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.).

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.

get rid of some parts of png in java

In my program,I am producing a png from a panel,but I want to get rid of the bounding box of
this panel and to be more focused to the object in the panel.To do so, I want to get rid of the peripheral parts of png, and produce only the center of it.How can i do that ?
Assuming that you already have your image loaded into a BufferedImage (which seems to be the case), this code will crop the image from (x,y) to (width,height).
image = image.getSubimage(x, y, width, height);
Then save as normal.

Categories