Texturing without Swing

I noticed in a code example that I ran across that people were using Swing to load textures. If you really want to ditch the windowing tookit as a dependency you can’t utilize javax.swing.* in your operations.

Below is a method for loading images which utilizes something that’s guaranteed to be in JDK1.4 runtime and doesn’t have any (obvious) dependencies on the swing architecture (and is cleaner code to boot).


            // load the image from disk
            //
            BufferedImage bufferedImage = ImageIO.read( new File( fileName ));

            // flip image to make GL happy
            //
            AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
            tx.translate(0, -bufferedImage.getHeight(null));
            AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            bufferedImage = op.filter(bufferedImage, null);

...


That will handle the loading and flipping of your textures. I leave the rest of the excercise of putting the textures into memory as an excercise for the reader.

Note. If you really want to have your own custom formats and such, you can just implement the SPI for images with ImageIO (trivial amount of work) and booyah, new image formats without having to pollute your engine code with it. ImageIO is good stuff.

That said, its complete a$$ that you can’t go from an ImageIO load to a ByteBuffer (for all you Sun folks that might be reading this). ImageIO should have been endowed with those abilities dang-it, all of the new APIs should be able to produce and consume ByteBuffers!