Ah…in my ongoing saga to try writing a 1.1-applet, I’ve managed to get the Image/Graphics/AWT into the situation where I can blit images to it and they show up exactly as expected but drawline, fillrect, etc are broken.
Argh!
I spent ages wondering why they weren’t painting. Eventually, I tried NOT setting a background colour (black). Low and behold…the following lines of code:
logger.info( "Drawing a rectangle from -50,-50 of size 100,100 color = red");
renderBuffer.getGraphics().setColor( Color.red );
renderBuffer.getGraphics().fillRect( -50, -50, 100, 100 );
always paint a BLACK rectangle on the screen.
The subsequent lines:
renderBuffer.getGraphics().setColor( Color.white );
int numCellsAcross = 10;
int cellWidth = renderBuffer.getWidth(null) / numCellsAcross;
int numCellsDown = 10;
int cellHeight = renderBuffer.getHeight(null) / numCellsDown;
for( int i=0; i<numCellsAcross; i++)
{
logger.debug( "Drawing grid line from "+(i * cellWidth)+", "+0+" to "+(i * cellWidth)+","+ (numCellsDown * cellHeight));
renderBuffer.getGraphics().drawLine( i * cellWidth, 0, i * cellWidth, numCellsDown * cellHeight );
}
for( int k=0; k<numCellsDown; k++)
{
logger.debug( "Drawing grid line from 0, "+(k * cellHeight)+", "+0+" to "+(numCellsAcross * cellWidth)+","+ (k * cellHeight));
renderBuffer.getGraphics().drawLine( 0, k * cellHeight, numCellsAcross* cellWidth, k * cellHeight );
}
…always paint BLACK lines.
So, it was there - I just wasn’t seeing the black-on-black.
I added the following:
logger.info( "....(BEFORE setting color, graphics is: "+renderBuffer.getGraphics());
renderBuffer.getGraphics().setColor( Color.red );
logger.info( "....(AFTER setting color, graphics is: "+renderBuffer.getGraphics());
…and I get this:
WTF is going on! I’ve never seen something like this before :(.
Total setup code from program start is very little more than this (snipped boring stuff like “Button b = new Button(“test”)” etc)
canvas = new CustomCanvas();
setSize(800, 400);
setTitle( "Test application");
setLayout( new BorderLayout());
Image[] normalPixmaps = new Image[8];
for( int i = 0; i < normalPixmaps.length; i++ )
{
normalPixmaps[i] = Toolkit.getDefaultToolkit().getImage( getClass().getClassLoader().getResource( name ) );
getGraphics().drawImage( normalPixmaps[i], 0, 0, 1, 1, null );
}
setVisible(true);