Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6576553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:31:35+00:00 2026-05-25T15:31:35+00:00

I have an applet which displays some data using circles and lines. As the

  • 0

I have an applet which displays some data using circles and lines. As the data continually changes, the display is updated, which means that sometimes the circles and lines must be erased, so I just draw them in white (my background color) to erase them. (There are a lot of them, so erasing everything and then recomputing and redrawing everything except the erased item would be a horribly slow way to erase a single item.)

The logic of the situation is that there are two layers that need to be displayed, and I need to be able to erase an object in one layer without affecting the other layer. I suppose the upper layer would need to have a background color of “transparent”, but then how would I erase an object, since drawing in a transparent color has no effect.

What distinguishes this situation from all the transparency-related help on the web is that I want to be able to erase lines and circles one-by-one from the transparent layer, overwriting their pixels with the “fully transparent” color.

Currently my applet draws (using just a single layer) by doing this in start():

    screenBuffer = createImage(640, 480);
    screenBufferGraphics = screenBuffer.getGraphics();

and this in paint():

    g.drawImage(screenBuffer, 0, 0, this);

and objects are rendered (or “erased” by drawing in white) by commands like:

    screenBufferGraphics.drawLine(x1,y1,x2,y2);

Is it easy to somehow make a second screen buffer with a transparent background and then be able to draw and erase objects in that buffer and render it over the first buffer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:31:36+00:00Added an answer on May 25, 2026 at 3:31 pm

    After a day of no proposed solutions, I started to think that Java Graphics cannot erase individual items back to a transparent color. But it turns out that the improved Graphics2D, together with BufferedImage and AlphaComposite, provide pretty much exactly the functionality I was looking for, allowing me to both draw shapes and erase shapes (back to full transparency) in various layers.

    Now I do the following in start():

        screenBuffer = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
        screenBufferGraphics = screenBuffer.createGraphics();
    
        overlayBuffer = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
        overlayBufferGraphics = overlayBuffer.createGraphics();
    

    I have to use new BufferedImage() instead of createImage() because I need to ask for alpha. (Even for screenBuffer, although it is the background — go figure!) I use createGraphics() instead of getGraphics() just because my variable screenBufferGraphics is now a Graphics2D object instead of just a Graphics object. (Although casting back and forth works fine too.)

    The code in paint() is barely different:

            g.drawImage(screenBuffer, 0, 0, null);
            g.drawImage(overlayBuffer, 0, 0, null);
    

    And objects are rendered (or erased) like this:

    // render to background
        screenBufferGraphics.setColor(Color.red);
        screenBufferGraphics.fillOval(80,80, 40,40);
    // render to overlay
        overlayBufferGraphics.setComposite(AlphaComposite.SrcOver);
        overlayBufferGraphics.setColor(Color.green);
        overlayBufferGraphics.fillOval(90,70, 20,60);
    // render invisibility onto overlay
        overlayBufferGraphics.setComposite(AlphaComposite.DstOut);
        overlayBufferGraphics.setColor(Color.blue);
        overlayBufferGraphics.fillOval(70,90, 30,20);
    // and flush just this locally changed region
        repaint(60,60, 80,80);
    

    The final Color.blue yields transparency, not blueness — it can be any color that has no transparency.

    As a final note, if you are rendering in a different thread from the AWT-EventQueue thread (which you probably are if you spend a lot of time rendering but also need to have a responsive interface), then you will want to synchronize the above code in paint() with your rendering routine; otherwise the display can wind up in a half-drawn state.

    If you are rendering in more than one thread, you will need to synchronize the rendering routine anyway so that the Graphics2D state changes do not interfere with each other. (Or maybe each thread could have its own Graphics2D object drawing onto the same BufferedImage — I didn’t try that.)

    It looks so simple, it’s hard to believe how long it took to figure out how to do this!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a UITableView to display some data which the user can filter.
I have a Java applet which displays an interactive map. By using the scroll
I have a NSFetchedResultsController which displays data in a UITableView. I'm using the boiler
I have read from some article that say's Apple doesn't approve the application which
I have some html which looks like this: <div style={ display:inline; width: 80px}>fig</div>vitamin c<br>
I've created an applet which has one large panel to display data surrounded by
I have an instance of a MPMoviePlayerController which is being used to display some
I have an applet which runs in a browser and is called from Javascript.
I have written a java applet which opens a JFrame (so when run in
I have created a UITableViewCell using UITableViewCellStyleValue1, which the Apple docs define as: A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.