• 0

[Java] drawString Sometimes Draws Wrong Color


Question

For some reason, when this code is executed, it will sometimes draw the strings in the color set in the preceding setColor method (what it should be doing), but it will often draw it using textColor or some other color set afterwards instead. The colors drawn will vary almost every time it is executed.

The colors are static constants and are declared at the top of the class.

Any ideas what I'm doing wrong?

statistics = new GameImage(240, 300);
statsG = (Graphics2D) statistics.getGraphics();
statsG.setColor(bgColor);
statsG.fillRect(0, 0, 240, 300);
statsG.setFont(new Font("Old English Text", Font.PLAIN, 16));
statsG.setColor(healthColor);
statsG.drawString("Health", 0, 20);
statsG.setColor(energyColor);
statsG.drawString("Energy", 0, 40);
statsG.setColor(expColor);
statsG.drawString("Exp", 0, 60);
statsG.setColor(textColor);
statsG.drawString("Strength", 60, 80);
statsG.drawString("Agility", 60, 100);
statsG.drawString("Intelligence", 60, 120);
statsG.drawString("Endurance", 60, 140);
statsG.drawString("Luck", 60, 160);
statsG.drawString("Base Attacks", 0, 200);
statsG.drawString("Spirit Force", 0, 220);
statsG.drawString("Elemental Bolts", 0, 240);
statsG.drawString("-", 113, 200);
statsG.drawString("-", 113, 220);
statsG.drawString("-", 113, 240);

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

You might want to check if the change color method works directly or if it works in some sort of background thread (maybe the renderer thread).

Link to comment
Share on other sites

  • 0
You might want to check if the change color method works directly or if it works in some sort of background thread (maybe the renderer thread).

Well, I know that the setColor method is working immediately. I tried having it loop until the getColor method returned the correct color and, after several tests, this has never tested false. I also tried sleeping for about a second after each drawString and still had the same issue. However, by process of elimination, I think that the drawString is getting queued up if anything. Is there a quick way to test whether or not a draw operation has completed?

Link to comment
Share on other sites

  • 0

The Java graphics library (much like any graphics library) batches up draw operations for performance reasons.

Try calling flush() or statsG.flush() before you change colors. This should force it to render and gives you control over the batching operations.

Link to comment
Share on other sites

  • 0
The Java graphics library (much like any graphics library) batches up draw operations for performance reasons.

Try calling flush() or statsG.flush() before you change colors. This should force it to render and gives you control over the batching operations.

I had to change that slightly to try it. Graphics objects don't have a flush method so I called the image's (statistics) flush method instead (GameImage extends BufferedImage. In this case you should be able to treat it as one).

It seemed more likely to render properly when doing this, but it still didn't work every time.

Here is the change:

statistics = new GameImage(240, 300);
statsG = (Graphics2D) statistics.getGraphics();
statsG.setColor(bgColor);
statsG.fillRect(0, 0, 240, 300);
statsG.setFont(new Font("Old English Text", Font.PLAIN, 16));
statistics.flush();
statsG.setColor(healthColor);
statsG.drawString("Health", 0, 20);
statistics.flush();
statsG.setColor(energyColor);
statsG.drawString("Energy", 0, 40);
statistics.flush();
statsG.setColor(expColor);
statsG.drawString("Exp", 0, 60);
statistics.flush();
statsG.setColor(textColor);
statsG.drawString("Strength", 60, 80);
statsG.drawString("Agility", 60, 100);
statsG.drawString("Intelligence", 60, 120);
statsG.drawString("Endurance", 60, 140);
statsG.drawString("Luck", 60, 160);
statsG.drawString("Base Attacks", 0, 200);
statsG.drawString("Spirit Force", 0, 220);
statsG.drawString("Elemental Bolts", 0, 240);
statsG.drawString("-", 113, 200);
statsG.drawString("-", 113, 220);
statsG.drawString("-", 113, 240);

Link to comment
Share on other sites

  • 0

Since I was planning on adding hardware acceleration, I went ahead and tested the original code in the context of a VolatileImage. It worked. However, I'm still curious as to what the problem was.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.