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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:04:00+00:00 2026-05-16T23:04:00+00:00

I’m trying to edit my timer so that every 25 times repaint() is called

  • 0

I’m trying to edit my timer so that every 25 times repaint() is called the timer firing speed cuts in half. So the first 25 times it’s 500; then the next 25 times its 250; and so on.

Two ‘EASY FOR THE EXPERIENCED’ questions:

1) Why is Eclipse making me make the variables static (or otherwise not compiling)?

2) The program doesn’t seem to reach the function where I divide the speed in half and set the delay to that new speed. Why is that? How do I fix it?

public class MovingCircle extends JFrame implements ActionListener {

    Ellipse2D.Double myEllipse;
    Rectangle2D.Double backgroundRectangle;
    private static int paintCount = 0;
    private static int speed = 500;

    public MovingCircle() {

        //Make the ellipse at the starting position
        myEllipse = new Ellipse2D.Double( 30, 30, 20, 20 );

        //Make the background rectangle to "erase" the screen
        backgroundRectangle = new Rectangle2D.Double( 0, 0, 400, 300 );
    }

    public static void main(String[] args ) {

        MovingCircle b = new MovingCircle();
        b.setSize( 400, 300 );
        b.setVisible(true);
        b.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        Timer t = new Timer(500, b );
        t.start();

        if(paintCount % 25 == 0) {

            t.setDelay((int)(speed / 2));
            speed = (int)(speed / 2);
            System.out.println(speed);
        }
  }

    public void actionPerformed( ActionEvent ae ) {

        //This will be called by the Timer
        myEllipse.setFrame( myEllipse.getX()+1, myEllipse.getY()+1, myEllipse.getWidth(), myEllipse.getHeight());  
        //Move 1 x-pixel and 1 y-pixel every 50 milliseconds ^
        repaint();
    }

    public void paint(Graphics g) {

        paintCount++;     // Incremenets by one for every repaint().
        System.out.println(paintCount);
        int isPaintTen = (int)(paintCount / 10);  // Divid current count by 10.
        Graphics2D g2 = (Graphics2D)g;

        if((isPaintTen % 2) == 0){      // Take modulus to set if #/10 is odd or even.

            g2.setColor( Color.YELLOW );
            g2.fill( backgroundRectangle );
            g2.setColor( Color.RED );
            g2.draw( myEllipse );
        }

        else if((isPaintTen % 2) == 1) {

            g2.setColor( Color.RED );
            g2.fill( backgroundRectangle );
            g2.setColor( Color.YELLOW);
            g2.draw( myEllipse );  
        }
   }

}

  • 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-16T23:04:00+00:00Added an answer on May 16, 2026 at 11:04 pm
    1. In your example, paintCount and speed have to be static because you are using them without an instance, from within a method, main(), which is itself static. To avoid having to make them static, you could have referenced them as b.paintCount and b.speed.

    2. The code that modifies your timer needs to move into your paint() method. This means your Timer instance will need to become an instance variable, and you should probably create and start the timer within the constructor. Incidentally, these changes also require that paintCount and speed also be made “non-static”.

    You should end up with something like this:

    public class MovingCircle extends JFrame implements ActionListener{
        Ellipse2D.Double myEllipse;
        Rectangle2D.Double backgroundRectangle;
        private int paintCount = 0;
        private int speed = 500;
        private Timer tmr;
    
        public MovingCircle() {
            //Make the ellipse at the starting position
            myEllipse = new Ellipse2D.Double( 30, 30, 20, 20 );
    
            //Make the background rectangle to "erase" the screen
            backgroundRectangle = new Rectangle2D.Double( 0, 0, 400, 300 );
    
            this.tmr = new Timer(500, this);
            tmr.start();
        }
    
        public static void main(String[] args ) {
            MovingCircle b = new MovingCircle();
            b.setSize( 400, 300 );
            b.setVisible(true);
            b.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        }
    
        public void actionPerformed( ActionEvent ae ) {
            //This will be called by the Timer
            myEllipse.setFrame( myEllipse.getX()+1, myEllipse.getY()+1, myEllipse.getWidth(), myEllipse.getHeight());   //Move 1 x-pixel and 1 y-pixel every 50 milliseconds
            repaint();
        }
    
        public void paint(Graphics g) {
            paintCount++;     // Incremenets by one for every repaint().
            System.out.println(paintCount);
    
            if(paintCount % 25 == 0){
                tmr.setDelay((int)(speed / 2));
                speed = (int)(speed / 2);
                System.out.println(speed);
            }
    
            int isPaintTen = (int)(paintCount / 10);  // Divid current count by 10.
            Graphics2D g2 = (Graphics2D)g;
            if((isPaintTen % 2) == 0){       // Take modulus to set if #/10 is odd or even.
                g2.setColor( Color.YELLOW );
                g2.fill( backgroundRectangle );
                g2.setColor( Color.RED );
                g2.draw( myEllipse );
    
            } else if((isPaintTen % 2) == 1) {
                g2.setColor( Color.RED );
                g2.fill( backgroundRectangle );
                g2.setColor( Color.YELLOW);
                g2.draw( myEllipse );    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.