Using BufferedImage, WritableRaster etc. for making a "screen"
I am trying to make a "screen" that will consist of several mini screens, but in my attempt to build this, I find myself drowning in choices and not being able to understand half of my options even though I've read through the Java docs.
Here's what I want to do. I want to build a screen that is 32 pixels high and 32 pixels wide. I want it to contain my mini screens, but other than that I only want to be able to change the background colour of the screen. So therefore I have chosen to extend the Component class for my Screen class for now.
I want each of my mini screens to be 10 pixels high and 10 pixels wide, and I want to be able to change seperate pixels. So I have chosen BufferedImage as the extension for my MiniScreen class.
And finally, I want a Tile class, that just contains a 2-dimensional array of ints, i.e. the pixels. Because BufferedImage uses a WritableRaster, I've had to let my Tile class extend WritableRaster.
Besides this, I have decided that I only need 9 colours, so 1 bit each for Red, Green and Blue, and then another bit for the alpha channel, so that the last colour is transparent.
This setup causes some problems though. First of all, I would prefer not having to use the paint method, and instead just being able to add my mini screens to my screen as you add components in AWT. I could let my Screen class extend Container, but that gives me a problem with my mini screens since BufferedImage doesn't extend Component, and I don't know of any class that does this and also has the ability to write pixels.
Second of all, is there a way to replace the WritableRaster contained in the BufferedImage class? I couldn't find one, but I may have overlooked something. Otherwise I will have to use the setPixels method where I would prefer to just replace the WritableRaster. But that's a minor issue.
The big problem is all the new classes that popped up when I decided to use this setup. For BufferedImage, I need a data type, and I think I need to use BufferedImage.TYPE_BYTE_INDEXED with my colours in mind, but I am not sure. And there are also a lot of options in DataBuffer, but I am not entirely sure if they can be used with BufferedImage at all.
I also need a ColorModel. As far as I've understood it, the ColorModel can be thought of as a palette, with all the colours saved as 32 bits each, and the array of pixels for my tiles are just then the indexes in the palette for whichever colour I wish to use. But the problem here is figuring out which ColorModel I need to use, since there are ComponentColorModel, IndexColorModel, PackedColorModel and maybe others, and I can't figure out which one is best for my colours.
Another thing is the setPixels and setPixel methods in the WritableRaster class. I don't understand why the third argument is an array, when you're only setting one pixel. And does setPixels only set one single row of pixels, or as many as you want? I'd imagine that if it set a rectangle of pixels that it would have a 2-dimensional array as an argument, but I figure that it might just calculate when to go to the next row since it's using a 1-dimensional array. So can someone tell me how it works exactly?
Right now I'm using the IndexColorModel, although I'm not sure of this as mentioned before. But I use this constructor:
IndexColorModel(int bits, int size, int cmap, int start, boolean hasalpha, int trans, int transferType)
The thing that confuses me is the colour map. I get that this is the actual colours, but how do I define them? Is it always 32 bits, or since I use 4 bit colours only, do I pack 4 colours into 1 int? The Java doc isn't too helpful with figuring this out.
And finally, since I'm extending WritableRaster for my Tile class, I need to call one of WritableRaster's constructors. They all use a SampleModel and a Point for determining the upper left corner. I can't quite figure out what the SampleModel is for though, and when I looked it up I found that there are a lot of extensions of this, so again I'm not sure which one I should use.
So I hope someone can help me out of this confusing state. I don't expect anyone to answer all my questions, but if you can answer just some of them, and guide me in finding out which option of many I should choose, I would be most grateful. Because right now, I have opened a can of worms, and they're crawling all over the place.