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 4559328
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:46:23+00:00 2026-05-21T17:46:23+00:00

This is a part of my code which does some sort of animation; however,

  • 0

This is a part of my code which does some sort of animation; however, there seems to be something wrong: Whenever I try to use the passed ‘g’ from inside the anonymous class to draw anything, it does nothing, yet when I used it outside the anonymous class (inside the rollBalls method) it does what it’s supposed to do. Any idea why? And how do I fix this? Thank you.

   protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        rollBalls(g);
    }


    private void rollBalls(final Graphics g) { //Roll all 3 balls on the bar
        new Timer(1, new ActionListener() { 
            @Override
            public void actionPerformed(ActionEvent e) {
                                   g.setColor(Color.red);
                                   g.fillRect(0, 0, 500, 500);
            }
        }).start();
    }
  • 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-21T17:46:24+00:00Added an answer on May 21, 2026 at 5:46 pm

    Your problem is several fold but first and foremost you understand that the Graphics object passed into a paint or paintComponent method usually does not persist and may be disposed of after the method completes. Next you have program logic being called from within a paintComponent method which should never be done. You do not have full control of when or even if the paint or paintComponent methods are called and thus should not have it dictating your app’s logic.

    For this reason you do not do Swing graphics in this way. Instead, have your timer outside of any paintComponent method, have it update class fields, have it then call repaint() and have your paintComponent use these class fields to do drawing as needed and this time with a stable Graphics object that is passed into it via its parameters.

    I understand that your code is “just a sample”, but your doing things wrong by trying to paint directly from within actionPerformed. You simply shouldn’t do this.

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

Sidebar

Related Questions

It's a part of larger code base, which forces -Werror on gcc. This warning
I have some code (it's part of a wordpress plugin) which takes a text
Note : The code in this question is part of deSleeper if you want
I'm trying to reteach myself some long forgotten math skills. This is part of
I'm writing an app to help facilitate some research, and part of this involves
This is actually a two part question. First,does the HttpContext.Current correspond to the current
Good day everyone. This problem was part of another one which it as been
I wrote part of a program that does some heavy work with strings in
Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of
This is a part algorithm-logic question (how to do it), part implementation question (how

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.