Java animation clip when not moving the mouse cursor

I have a very simple animation, a large font of text constantly (pixel by pixel) moving to the left First convert the text into an image, then start the timer task, repeat (every 10-20 MS) to decrease the X coordinate of the image by 1, and redraw ()

This program shows a strange behavior on some systems It runs smoothly on computers with NVIDIA cards On my VAIO notebook, on a beagle bone black and a friend's MAC, it was put down heavily It seems to pause for a while, then move about 10 pixels to the left, pause again, and so on

What bothers me is that on these systems, if you don't touch the mouse, the animation will only stay As long as you move the mouse cursor into the window, no matter how slow, or drag the window itself, the animation runs smoothly!

Can anyone explain? This is the procedure:

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

class Textimg extends JComponent
{
    String      str;
    Font        font;
    int         x = 0;
    final int   ytext = 136;
    Image       img;

    public Textimg(String s)
    {
        str = s;
        font = new Font("Noserif",Font.PLAIN,96);
        setLayout(null);
    }

    protected void paintComponent(Graphics g)
    {
        if (img == null)
        {
            img = createImage(4800,272);
            Graphics gr = img.getGraphics();

            gr.setFont(font);
            gr.setColor(Color.BLACK);
            gr.fillRect(0,4800,272);
            gr.setColor(new Color(135,175,0));
            gr.drawString(str,ytext);
            gr.dispose();
        }

        g.drawImage(img,x,this);
    }

    public void addX(int dif)
    {
        if (isVisible())
        {
            x = x + dif;

            Graphics g = getGraphics();

            if (g != null) paintComponent(g);
        }
    }
} 


public class Banner extends JFrame 
{ 
    StringBuffer    buf;
    int             sleeptime = 10;

    Banner(String path) throws IOException 
    { 
        setSize(new Dimension(480,272));
        setTitle("Java Test");
        setDefaultCloSEOperation(EXIT_ON_CLOSE);
        setLayout(null);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(path),"UTF-8"));

        buf = new StringBuffer();

        while (true) 
        {
           String line = reader.readLine();

           if (line == null) break;
           buf.append(line);
        }

        final Textimg textimg = new Textimg(buf.toString());

        add(textimg);
        textimg.setBounds(0,480,272);

        final javax.swing.Timer timer = new javax.swing.Timer(200,new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                textimg.addX(-1);
            }
        });

        timer.setDelay(sleeptime);
        timer.start();
    }

    //----------------------------------------------------------------------

    public static void main(String[] args) throws Exception
    {
        new Banner(args[0]).setVisible(true);
    }
}

Solution

When you finish drawing, try calling this method:

Toolkit.getDefaultToolkit().sync();

This will refresh some graphics buffers on systems like Linux See Javadoc: http://docs.oracle.com/javase/7/docs/api/java/awt/Toolkit.html#sync ()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>