I'm currently working on a game in Java and am trying to create a background without using any image files. The image consists of a square split into 4 triangles, each of which is a different color.
If anyone could point me towards some was of using Graphics2D
and then saving it to a BufferedImage
, that would be great.
Java provides immediate access to the image pixels and color information and allows conversions and image processing.
Image class is the superclass that represents graphical images as rectangular arrays of pixels. The java. awt. image. BufferedImage class, which extends the Image class to allow the application to operate directly with image data (for example, retrieving or setting up the pixel color).
The formatName parameter selects the image format in which to save the BufferedImage . try { // retrieve image BufferedImage bi = getMyImage(); File outputfile = new File("saved. png"); ImageIO. write(bi, "png", outputfile); } catch (IOException e) { ... }
I recommend:
createGraphics()
method. drawXXX(...)
methods of which you have many to select from.setColor(Color c)
on your Graphics/Graphics2D object.dispose()
method.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With