This is my first try to do something meaningful with Graphics2D, previously I have worked only with the Graphics class. It's not so much that I need help, rather I just want to bounce my ideas off the board and see what comes up.

OK, the goal is to improve my chess graphics. The idea is to learn graphics, not just create a better mousetrap. Eventually I want a system where the user can choose beteen piece designs. Some of the designs will be created using the GeneralPath class, others will be from externally created images, such as jpegs.

It seems to me that since I want to switch between both methods of design, then rather than drawing a GeneralPath object directly to the drawing surface, a JPanel, I should proceed as follows...

1. Draw the board directly on to the JPanel. This means the dark and light squares, not the pieces.2. Create a BufferedImage object, then associate it with a Graphics2D context by using the createGraphics() method.3. The BufferedImage should have a transparent background, so that a piece can be dragged without obscuring other pieces.4. Use the usual methods for designing a GeneralPath object, except because of step 2 we are modifying a BufferedImage.5. When I actually want to visually render the BufferedImage object, I will use a drawImage method on another graphics context which is the chess board. Since we are drawing images to the board and not GeneralPaths, it seems to me there is no need to make the board Graphics2D, plain old Graphics will do.

OK the whole point of going to the extra work of drawing a GeneralPath to a BufferedImage rather than directly to the board is that this will make it easier to switch between using GeneralPath and externally created image files such as jpg for my pieces.

make sense? or am I missing something?

regards.

p.s. OK one question, does it matter what file type I use when my pieces are externallly created images? I suppose jpg is as good as any.

p.s.s. I suppose I can say that if I was only dealing with externally created images, I wouldn't need to use Graphics2D at all.

Yeah, if you use ImageIO to create your BufferedImages, they will automatically include transparency as long as your source image had transparency. So just make the PNG as you would want it to look, then load it in.

Thanks Demonpants, I ended up using Class.getResourceAsStream, which retrieves the png from the jar file as an InputStream, then used a version of ImageIO.read() that takes an InputStream as a parameter.

Anyways, the results are available on my website if anyone wants to take a look. I included a checkBox in the GUI to toggle between the old and new piece style.

Thanks. It's worth pointing out that there really is no Graphics2D involved here. All the drawImage() stuff is from the Graphics class. I did use createGraphics() on my blank BufferedImages which creates a Graphics2D context, but then I used basic draw methods for my old style pieces. But what I have done is two set the stage for using advanced primitives and GeneralPath which is Graphics2D. If I ever get around to it. But again as far as i can see you don't need Graphics2D for external images.

Thanks. It's worth pointing out that there really is no Graphics2D involved here. All the drawImage() stuff is from the Graphics class. I did use createGraphics() on my blank BufferedImages which creates a Graphics2D context, but then I used basic draw methods for my old style pieces. But what I have done is two set the stage for using advanced primitives and GeneralPath which is Graphics2D. If I ever get around to it. But again as far as i can see you don't need Graphics2D for external images.

You're right, but Graphics2D is more powerful and JPanel, etc. actually have a Graphics2D object, not just a Graphics object. So you might as well make the cast to Graphics2D if you want any of the functionality. It won't cost you anything aside from a single cast, which is negligible.

For your external images, you will want a format that supports transparency like png or gif.

I have another question. I have access to lots of images, but not so many that have transparent backgrounds. Can you suggest a strategy for modifying an existing image with a solid color background,, usually white so that it has a transparent background? Any good open source or freeware to do the job?, I'm not a big fan of Microsoft Paint, but I'll use it if I have to.

If you want to do it while loading your images it's called 'colour keying', and should be do-able with regular BufferedImage stuff without too much problem.

If you want to do it manually then you could try gimp or paint.net. I prefer Paint.net but it's windows-only, so you might have to go with Gimp.

OK Thanks, I'll look into color keying and let you know how it goes. It'll be next week sometime before I get around to it.

p.s. For anyone who visits my site to see the images, i had to get rid of them, and go with my primitive shapes. Sort of a personal dispute with the guy who gave me the images. Which is why I am revisiting the issue now.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org