Skip to main content

Strange difference between windows and linux

2 replies [Last post]
boomah
Offline
Joined: 2007-07-13
Points: 0

Hi people.

I've been running a very simple example on my system that just animates a circle going across the screen when you press a button.

I set the resolution on the animator to 0 and run it.

On windows it's very smooth. On Ubuntu 7.10 it's jerky.

This is strange because the timing event method is called roughly the same number of times on each OS (and its dual boot so the hardware is the same), and the paint component method has the same number of calls as well. I'm using the same version of Java 6 on both OS's.

Does anyone know what could be causing the difference I see? and more importantly, how do I fix it?

Any help would be much appreciated!

Thanks, Paul.

Edit: In fact, here's the code

import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.TimingTarget;

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


public class MovementTest extends JFrame
{
    public MovementTest()
    {
        super("Test Movement");

        final MovementComponent movementComponent = new MovementComponent();

        JButton startButton = new JButton("Start");
        startButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                movementComponent.animator.start();
            }
        });
        add(startButton, BorderLayout.NORTH);

        add(movementComponent, BorderLayout.CENTER);



        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 400, 400);
        setVisible(true);
    }

    private class MovementComponent extends JComponent implements TimingTarget
    {
        private final int duration = 50000;
        private final Animator animator;

        private float xPos = 0.0f;
        private float yPos = 0.0f;

        private int size = 10;

        private long counter = 0;
        private long paintCounter = 0;


        public MovementComponent()
        {
            animator = new Animator(duration, this);
            animator.setResolution(0);
        }

        @Override
        protected void paintComponent(Graphics g)
        {
            paintCounter++;

            Graphics2D g2 = (Graphics2D)g;

            g2.setColor(Color.BLACK);
            g2.fillRect(0, 0, getWidth(), getHeight());

            g2.setColor(Color.RED);
            g2.fillOval(Math.round(xPos), Math.round(yPos), size, size);
        }

        @Override
        public void timingEvent(float fraction)
        {
            counter++;
            xPos = getWidth() * fraction;
            yPos = getHeight() / 2.0f;
            repaint();
        }

        @Override
        public void begin()
        {
            counter = 0;
            paintCounter = 0;
        }

        @Override
        public void end()
        {
            System.out.println("Counter: " + counter);
            System.out.println("Paint Counter: " + paintCounter);
        }

        @Override
        public void repeat()
        {

        }
    }


    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new MovementTest();
            }
        });
    }
}

Message was edited by: boomah

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sherington
Offline
Joined: 2005-02-24
Points: 0

I know this is an old thread, but I'm posting here as the question wasn't answered and in case the solution is useful to anyone else...

I saw the exact same behaviour on Kubuntu 8.10. Moving the mouse makes the animation very smooth.

If you put "Toolkit.getDefaultToolkit().sync();" at the of paintComponent it seems to improve things significantly.

The Javadoc for the sync method adds the helpful comment "It is useful for animation".

biker
Offline
Joined: 2008-06-14
Points: 0

Hi,

Sorry to reply with no help, just wanted to confirm that I've seen the same thing on Ubuntu 8.04. Simple animation with filled circle moving down the screen - fine on XP, jerky and jumpy on Ubuntu. One bizzare thing - if I move the mouse cursor around rapidly in the window, the motion becomes smoother. Don't know if the frame picking up the mouse movement events has something to do with it.

Have you managed to figure out what's happening?

Thanks,

Marcus.