Reflection [SOLVED]

Hi lads! :smiley:

Soooo here I am back in Newbie & Debugging… :persecutioncomplex:
In fact, I’d need a little help on this one… You see, I have this 2D GUI which has water flowing at the bottom, like so:

//deleted image for spoiler reasons :stuck_out_tongue:

As you may notice, I’d like to have the whole thing reflected in the water part. The reflection you can see on the image is the result of an early attempt to do so, but I now have to rewrite it, because of a few things:
• the black plants are not reflecting properly;
• there is this overlaying problem at the surface (see purple zone);
• what I use for the moment is quite resource-consuming, so I only render a new reflection every .5 second or so.

Until now, I actually drew a screen capture of the over-water zone, using java.awt.Robot’s methods… ::slight_smile:
This is the last thing for me to do on this GUI before being able to move on, so here’s the thing:
I guess it would be better to copy the contents of the java.awt.Graphics2D object — before painting the plants and the water — inside a java.awt.image.BufferedImage object, and then to paint it back, upside down, where I currently draw the screen capture.
That’s why I have been wondering (and wandering on the internet for quite a long time)… How does one make a BufferedImage from Graphics2D? Something that would work a little like that maybe? ???

BufferedImage img;
img = g2d.toImage();//where g2d is a Graphics2D object

I’d be very thankful if anyone could help me out by pointing out that one method I’ve been missing the whole time, or maybe by indicating an alternative solution ;D
Thanks :slight_smile:
J0

What you’re saying doesn’t make a ton of sense: you don’t make a Graphics2D from a BufferedImage. You can create a BufferedImage using a Graphics2D though.

Graphics2D is the pen. BufferedImage is the paper. Graphics2D doesn’t store what it has previously drawn inside itself. It stores that on the BufferedImage (or on the component you’re drawing to).

You could get your BufferedImage’s Graphics2D using the getGraphics() function, and then just draw to that.

But an easier solution would be to simply draw your stuff upside down as well whenever you draw it right-side up.

Yeah, I… kind of thought that would make my code twice as big ;D

Anyway, thank you, I think I might begin to figure out how to finish my GUI thanks to your feedback 8)

J0

I doubt it. Just draw your stuff once, and then draw it again upside down.

Alternatively, you could draw all of your stuff to a BufferedImage, and then draw that BufferedImage twice: once for the right-side-up, and once for the reflection.