Java - Creating an image
Asked Answered
H

1

5

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.

Hypno answered 5/4, 2013 at 2:46 Comment(0)
W
9

I recommend:

  • First create a BufferedImage using the constructor that takes three ints: a width, height, and a BufferedImage type, BufferedImage.TYPE_INT_ARGB would probably work well, and the width and height will likely be constants in your program.
  • You would extract a Graphics2D object out of the BufferedImage by calling its createGraphics() method.
  • Then draw with the Graphics object using its drawXXX(...) methods of which you have many to select from.
  • To change color, simply call setColor(Color c) on your Graphics/Graphics2D object.
  • When done drawing, be sure to dispose of your Graphics object via its dispose() method.
  • Edit as per Adrian Blackburn, check out the BufferedImage Tutorial as part of the standard Oracle Java tutorials.
War answered 5/4, 2013 at 2:50 Comment(3)
docs.oracle.com/javase/tutorial/2d/images/drawonimage.html The java tutorial says the same thingQuinque
@AdrianBlackburn: thanks for the link -- and it probably says it a lot clearer than anything I can say! Edit: I've added your rec to my answer. Again, thanks for the link!War
Beautiful answer, but I'd expect nothing less from you Hovercraft. Thanks a lot!Hypno

© 2022 - 2024 — McMap. All rights reserved.