AWT ignores setColor() !!! ?

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);

uhm… try this:


Graphics g=renderBuffer.getGraphics()
g.setColor( Color.red );
g.fillRect( -50, -50, 100, 100 ); 
[...]
g.dispose();

blah3 == n00b :stuck_out_tongue:

Cas :slight_smile:

[quote]uhm… try this:


Graphics g=renderBuffer.getGraphics()
g.setColor( Color.red );
g.fillRect( -50, -50, 100, 100 ); 
[...]
g.dispose();

[/quote]
Why is it different?

Looks like you got 2 different Graphics objects. Set one to red, then draw a rect with default black using the second.

Did the change work ok?

[quote]Why is it different?
[/quote]
getGraphics() creates a graphics context for this component, which also means that the states (such as color) are resetted.

Always get, draw and dispose - each frame. Everything else either doesn’t work correctly or produces weird side effects like eating all ram.

/me slams head against desk

It’s been so long since I’ve written methods which didn’t get the Graphics passed in (these days I just hand-down the one from paintComponent(G)) I’d completely forgotten about that. Although, now you say it, I recall having to save the reference. Just been so long I’d forgotten why, and that it was necessary.

Sob.

I think I’m not so much an AWT noob as … a reformed AWT user, who now subconsciously erases AWT knowledge over time because he hated it so much…which I guess makes me a noob at the same time ;D

i am a Swing n0Ob 8) :stuck_out_tongue: :-X

worked for 5 years only with awt