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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:42:07+00:00 2026-05-22T21:42:07+00:00

I am having a problem using the repaint method in the following code.Please suggest

  • 0

I am having a problem using the repaint method in the following code.Please suggest how to use repaint method so that my screen is updated for a small animation.
This is my code :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class movingObjects extends JPanel {
   Timer timer;
   int x = 2, y = 2, width = 10, height = 10;

   public void paintComponent(final Graphics g) { // <---- using repaint method
      ActionListener taskPerformer = new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            g.setColor(Color.red);
            g.drawOval(x, y, width, height);
            g.fillOval(x, y, width, height);
            x++;
            y++;
            width++;
            height++;
         }
      };
      new Timer(100, taskPerformer).start();
   }
}

class mainClass {
   mainClass() {
      buildGUI();
   }

   public void buildGUI() {
      JFrame fr = new JFrame("Moving Objects");
      movingObjects obj = new movingObjects();
      fr.add(obj);
      fr.setVisible(true);
      fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      fr.setSize(1300, 700);
   }

   public static void main(String args[]) {
      new mainClass();
   }
}
  • 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-22T21:42:09+00:00Added an answer on May 22, 2026 at 9:42 pm

    Don’t try to delay the actual painting. The component needs to be painted when it is asked to be painted.

    Instead, use your timer to modify some state in MovingObjects. In your case the state you want to change is x, y, width and height. When your timer fires, increment those values and call repaint().

    Then in your paintComponents method, you would just use those values to paint the component

    public void paintComponent(Graphics g) {
      g.setColor(Color.red);
      g.drawOval(x,y,width,height);
      g.fillOval(x,y,width,height);
    }
    

    Edit

    Not sure what you’re having trouble with, but calling repaint() is not difficult:

    ActionListener taskPerformer=new ActionListener() {
       public void actionPerformed(ActionEvent ae) {
          x++;
          y++;
          width++;
          height++;
          repaint();
       }
    };
    new Timer(100,taskPerformer).start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert
I am having a problem using the Unity Application Block, I have created a
I'm having a problem using CSS's display:inline property with the list-style-image: property on <li>
I'm having a problem running a T4 template using TextTransform.exe on my build server.
I'm having a problem writing Norwegian characters into an XML file using C#. I
I'm having a problem with a POST I'm doing using the HttpWebRequest object from
I am using the tablesorter plugin ( http://tablesorter.com ) and am having a problem
I'm using Intelligencia's UrlRewriter in my application and I'm having a problem where the
I'm developing a website (using asp.net by the way) and I'm having a problem
Problem: I have a method that creates a list from the parsed ArrayList. I

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.