How to remove previous drawStrings?

I know this is probably a pretty dumb question, but I’m stumped.

I am trying to use drawString to show a countdown timer. However, the previous values never clear so I end up with all the numbers overlaying.

I tried to use clearRect but that clears to the background color. I’m using the glasspane and want to show the countdown over what is already on screen.

How can I remove the text already showing on the glass Pane?

i haven’t used glass Pane…

what’s behind the timer? a background image? in that case, redraw the background and then draw the string on top.

to only draw behind the caiunter use Graphics.setClip(…) and then draw the image.

hope it makes sense! :smiley:

The background is transparent so the board shows through. When I try to use clearRect the screen gets set to the background color (grey) and hides the board.

I’ve tried setting the background color to a transparent alpha but that doesn’t work.

Essentially, I need to be able to make a specified rectangle clear and be transparent so I can redraw the countdown.

If there is another way, that would be great too.

      void drawBigText(String text) {
            Graphics2D g = (Graphics2D) getGlassPane().getGraphics();
            AttributedString styledText = new AttributedString(text);
            styledText.addAttribute(TextAttribute.FAMILY, "serif");
            styledText.addAttribute(TextAttribute.SIZE, new Float(120));
            styledText.addAttribute(TextAttribute.FOREGROUND, Color.yellow);
            AttributedCharacterIterator i = styledText.getIterator();
            g.drawString(i, 80, 250);
      }

ok, there gotta be something behind the timer since otherwise you would be looking through the monitor and on the wall that’s behind it!

you have to redraw what’s behind the timer! i’ll use my car game as an example.

loop

  1. draw track
  2. draw cars
  3. draw strings (lap info, time, etc).
    end loop

here (1) redraws the whole track over everything else (this is not optimal but for the sake of simplicity just ignore this). then you need to draw everything else since that is overdrawn by the track.

makes sense? i’m not really sure how to describe it better. anyone else?

Thanks. I see what you are getting at now. It was too obvious for my simple mind :-/

anytime :slight_smile:

if you are getting bad performance, look up Graphics.setClip(…)

Actually if you are using glass pane then you shouldnty need to redraw the background I dont think.

Try setting your background color to something with 0 alpha.
That shoudl make clearRect work the way you want it to.

JK

That was one of the first things I tried, and it seemed to ignore the alpha component.

I got it working by not even using glass pane though.

hey there… umm basically you need to grab the Graphics2D and execute the following line:

g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
then:
Rectangle2D rect = new Rectangle(0,0,m_nBoardWidth,m_nBoardHeight);
// or whatever the size of your screen or rectangle you want to blank to transparent is.
then
g2D.fill(rect);// now your rectangle has been cleared
after that you have to reset the composite by doing this:

g2D.setComposite(AlphaComposite.SrcOver);

// blit your image or text to the graphics2d now and your done.

Later.