Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (767)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (854)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  AWT ignores setColor() !!! ?  (Read 5426 times)
0 Members and 1 Guest are viewing this topic.
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Posted 2004-12-08 14:18:46 »

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:
1  
2  
3  
      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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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:
1  
2  
3  
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:

Quote

INFO ....(BEFORE setting color, graphics is: sun.java2d.SunGraphics2D[font=java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12],color=java.awt.SystemColor[i=9]]
26116 [AWT-EventQueue-0]
INFO ....(AFTER setting color, graphics is: sun.java2d.SunGraphics2D[font=java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12],color=java.awt.SystemColor[i=9]]
26116 [AWT-EventQueue-0]


WTF is going on! I've never seen something like this before Sad.

Total setup code from program start is very little more than this (snipped boring stuff like "Button b = new Button("test")" etc)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
      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);


malloc will be first against the wall when the revolution comes...
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #1 - Posted 2004-12-08 14:27:15 »

uhm... try this:

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

弾幕 ☆ @mahonnaiseblog
Offline princec

« JGO Spiffy Duke »


Medals: 1053
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2004-12-08 14:33:39 »

blah3 == n00b Tongue

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #3 - Posted 2004-12-08 14:44:40 »

Quote
uhm... try this:

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


Why is it different?

malloc will be first against the wall when the revolution comes...
Offline Tzan

Junior Devvie





« Reply #4 - Posted 2004-12-08 14:55:11 »

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?
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #5 - Posted 2004-12-08 14:56:22 »

Quote
Why is it different?


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.

弾幕 ☆ @mahonnaiseblog
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #6 - Posted 2004-12-08 15:03:33 »

Quote


getGraphics() *creates* a graphics context for this component,


/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 Grin

malloc will be first against the wall when the revolution comes...
Offline MGodehardt

Junior Devvie




why does the chicken cross the road?


« Reply #7 - Posted 2004-12-10 08:17:35 »

i am a Swing n0Ob  Cool Tongue Lips Sealed

worked for 5 years only with awt
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1256 views)
2018-06-10 19:43:48

EgonOlsen (1125 views)
2018-06-10 19:43:44

EgonOlsen (862 views)
2018-06-10 19:43:20

DesertCoockie (1271 views)
2018-05-13 18:23:11

nelsongames (1104 views)
2018-04-24 18:15:36

nelsongames (1335 views)
2018-04-24 18:14:32

ivj94 (2074 views)
2018-03-24 14:47:39

ivj94 (1227 views)
2018-03-24 14:46:31

ivj94 (2169 views)
2018-03-24 14:43:53

Solater (792 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!