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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:24:44+00:00 2026-05-26T05:24:44+00:00

I am using a Swing Timer in my game but when the game is

  • 0

I am using a Swing Timer in my game but when the game is running it appears to have moments when it runs smoothly and moments when it slows down.

Why is the time fluctuating?

And how do I fix it?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Main extends JFrame {

public Main() {
    super("JFrame");

    // you can set the content pane of the frame
    // to your custom class.

    setContentPane(new ImagePanel());

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setSize(800, 400);
    setResizable(false);
    setVisible(true);

}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    new Main();

}

class ImagePanel extends JPanel {

    Timer movementtimer;

    int x, y;

    public ImagePanel() {

        x = 0;
        y = 0;

        movementtimer = new Timer(12, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                long timstarted = System.currentTimeMillis();
                moveImage();
                repaint();
                long timefinished = System.currentTimeMillis() - timstarted;
                System.out.println(timefinished + " to run");
            };

        });

        movementtimer.start();

    }

    public void moveImage() {

        x++;
        y++;

        if (x > 800) {
            x = 0;
        }
        if (y > 400) {
            y = 0;
        }

    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(0, 0, 800, 400);
        g.setColor(Color.BLUE);
        g.fillRect(x, y, 50, 50);

    }

}

}

Here is an example of my code. In my actual program I am drawing Images and not just a rectangle. There is also a lot of collision detection and other small calculations happening.

Also, here is a link to the Jar file for the game so you can run it and (hopefull) see what I mean. http://dl.dropbox.com/u/8724803/Get%20To%20The%20Chopper%201.3.jar

Thanks

Tom

  • 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-26T05:24:44+00:00Added an answer on May 26, 2026 at 5:24 am

    Because the rendering is trivial, I find this variation of your example to be very smooth. The render time is well below a half millisecond, so the 12 millisecond period (~83 Hz) is plenty of time to finish a frame, typically taking less that 10% of one core. As the render time grows, the timer thread becomes saturated, and events are coalesced. The effect is magnified on a single core, as rendering competes with garbage collection and external processing demands. Java is not a real-time system, and not all schedulers are created equal.

    You’ll certainly want to profile your actual code, as suggested here, to see any correlation with fluctuating performance. One alternative approach is to lengthen the period (decrease the frequency) to meet your rendering deadline and use a larger increment in moveImage() to get the same velocity.

    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Main extends JFrame {
    
        private static final int W = 800;
        private static final int H = 400;
    
        public Main() {
            super("JFrame");
            this.add(new ImagePanel());
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.pack();
            setSize(W, H);
            this.setLocationRelativeTo(null);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Main();
                }
            });
        }
    
        class ImagePanel extends JPanel {
    
            Timer movementTimer;
            int x, y;
    
            public ImagePanel() {
                x = 0;
                y = 0;
                movementTimer = new Timer(12, new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        moveImage();
                        repaint();
                    }
                });
                movementTimer.start();
            }
    
            public void moveImage() {
                x++;
                y++;
                if (x > W) {
                    x = 0;
                }
                if (y > H) {
                    y = 0;
                }
            }
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                long start = System.nanoTime();
                g.setColor(Color.RED);
                g.fillRect(0, 0, W, H);
                g.setColor(Color.BLUE);
                g.fillRect(x, y, 50, 50);
                double delta = (System.nanoTime() - start) / 1000000d;
                g.drawString(String.format("%1$5.3f", delta), 5, 15);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a task running using javax.swing.Timer. It executes once every few minutes. The
I am writing a game using Java Swing. I want to paint each time
I am using javax.swing.Timer to schedule and run events. But it seems to be
I'm writing a Java Swing application using the Metal look-and-feel. Every time there is
I created a form using Swing in Java. In the form I have used
I am using swing to create my GUI. J have a JFrame containing one
I am creating an touch screen application using Swing and have a request to
In Java (Swing), say I've got a 2D game where I have various types
I am using javax.swing.SwingWorker for the first time. I want to update a JLabel
I'm willing to make a isometric game but I'm having hard time with the

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.