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

  • SEARCH
  • Home
  • 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 136379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:56:20+00:00 2026-05-11T06:56:20+00:00

Some users of our Swing application have reported weird artefacts appearing on the display.

  • 0

Some users of our Swing application have reported weird artefacts appearing on the display. This ranges from components not repainting themselves properly for a second or two, right upto whole portions of the application being repainted like tiled wallpaper over areas of the window.

The app has been worked on by developers of all levels between experienced Java guys to young chaps right out of university over the course of five years or so, and as you’d expect, some of the AWT code is an outright mess. I’m now faced with the task of trying to correct as much of the badness as I can over the next few months or so.

Some of this is easy to deal with. Deal with components only on the event dispatch thread, IO asynchronously, kind of thing, and I’m hopefully getting the message across to the rest of the team.

What I’d like to know is the best way of dealing with Graphics contexts, especially in a paintComponent() context. I see a lot of…

public void paintComponent( Graphics g ) {     super.paintComponent( g );     Graphics2D gfx = (Graphics2D)g;     // ...Whole lotta drawing code... } 

Is it better practice to do this?

public void paintComponent( Graphics g ) {     super.paintComponent( g );     Graphics2D gfx = (Graphics2D)g.create();     // ...Whole lotta drawing code...     gfx.dispose(); } 

If the g parameter is going to be reused in other paints then don’t I need to restore it to a good state, undo AffineTransforms, etc.?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T06:56:21+00:00Added an answer on May 11, 2026 at 6:56 am

    According to Filthy Rich Clients, you should not alter the Graphics object passed to you (which sucks as an API, IMO).

    The correct way to handle it is slightly more verbose:

    public void paintComponent(Graphics g1) {     super.paintComponent(g1);     final Graphics2D g = (Graphics2D)g1.create();     try {          // ...Whole lotta drawing code...     } finally {          g.dispose();     } } 

    IIRC, in the Sun implementation it doesn’t matter if you don’t dispose of ‘sub-Graphics’ objects. (Don’t quote me on that.)

    You might want to delegate that comment bit to another object.

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

Sidebar

Related Questions

In error reports from some end users of our game I have quite often
I have a table for tags. Since some users have done copy-paste from our
I have a MVC web application which shows some information about users in our
We have a business requirement that some users be automatically logged out of our
I have a site where some users will be registered by our staff, and
I have a table containing data about some users. Many of them use our
Many of our users, internal and external, start our web application. Then at some
A user reported to us that some strange behaviour in our application can be
Some of the users of our Ruby on Rails app have complained that page
Some of our users are getting this exception sometimes (not for every contact): System.Runtime.InteropServices.COMException

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.