File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Using the IndexColorModel class to create a BufferedImage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using the IndexColorModel class to create a BufferedImage" Watch "Using the IndexColorModel class to create a BufferedImage" New topic

Using the IndexColorModel class to create a BufferedImage

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 684
OK, what I am doing is designing a chess piece using the Graphics2D class. For reasons of flexibility, the plan is to create a "blank" transparent BufferedImage object, associate with the BufferedImage a Graphics2D context, then draw a GeneralShape to that context.

The actual chessboard, without pieces, will be drawn directly onto the JPanel, then each piece will be drawn using a drawImage method.

I figure the appropriate BufferedImage constructor is...

and the constructor for my IndexColorModel Object will be

I choose this constructor for IndexColorModel because it is the last alpha component that deals with transparency. Where I am struggling is how to define each argument in my call to the ICM contructor.

if the dimension of my square on the chessboard is 50x50 then does that mean that my bits and size parameters must be 50, and the byte arrays must also be of length 50? Even when the initial image (i.e. the background) is transparent? If so it would be nice to find a trick to quickly populate the arrays. I guess as simple loop will do it.

the other question is seeing as how my BufferedImage will be transparent, does it matter what the actuall values are in the rgb arrays? 255 accross the board (white) seems logical, then my final a array will be 0's for transparent.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2447
I've never worked with IndexColorModels but I think you can just do:

I think the image will be transparent by default and now you can use the g2d object to draw your chess pieces.

If the image isn't transparent then you would need to add the following before doing the custom painting.

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 684
Thanks Rob, i put your idea to the test with a very simple prototype which I include below. It worked fine.

You know, the Sun API docs are brilliantly put together on some ways. Everything is so tight, but the downside is that they don't make it easy to figure out little things like "the default is transparent. I mean here is a section from the API doc for BufferedImage. The description is a model of precision yes, but not of clarity. Anyways, thank God for JavaRanch, I'd probably still be trying to figure this out this evening.

So you look up things like DirectColorModel, premultiplied, etc. And after a 3 hour course maybe you find out that yes indeed the default is transparent.

Ayways, life goes on. Here is my prototype.

I agree. Here's the link:
subject: Using the IndexColorModel class to create a BufferedImage
It's not a secret anymore!