• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using the IndexColorModel class to create a BufferedImage

 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

regards.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic