在Linux中运行生涩的Java动画程序 [英] Java Animation programs running jerky in Linux

查看:115
本文介绍了在Linux中运行生涩的Java动画程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Ubuntu 14.4.1中编写了一个简单的Java动画程序。在JPanel内部移动的球。但是在执行时,球在JPanel中移动得非常剧烈。这个问题一直持续到我将鼠标移到JPanel内。在JPanel内部移动鼠标时,球的移动非常平稳。应该说我已经在Windows 10中运行了该程序,并且没有出现问题。我程序的代码如下:

I have written a simple Java animation program in Ubuntu 14.4.1. A ball moving inside a JPanel. But at execution, the ball moves quite jerky in the JPanel. This problem continues until I move the mouse inside the JPanel. At the time of moving the mouse inside the JPanel the ball movement is quite smooth. It should be said that I've run this program in Windows 10, and no problem occurred. The code for my program is as follows:

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

public class BouncingBall extends JPanel {
    Ball ball = new Ball();

    void startAnimation() {
        while( true ) {
            try {
                Thread.sleep( 25 );
                ball.go();
                repaint();
            } catch( InterruptedException e ) {}
        } // end while( true )
    } // end method startAnimation()

    protected void paintComponent( Graphics g ) {
        super.paintComponent( g );
        ball.draw( g );
    } // end method paintComponent


    // inner class Ball
    class Ball {
        int x;
        int y;
        int diameter = 10;
        int xSpeed = 100;
        int ySpeed = 70;

        void go() {
            x = x + (xSpeed*25)/1000;
            y = y + (ySpeed*25)/1000;

            int maxX = getWidth() - diameter;
            int maxY = getHeight() - diameter;
            if( x < 0 ) {
                // bounce at the left side
                x = 0;
                xSpeed = -xSpeed;
            } else if( x > maxX ) {
                // bounce at the right side
                x = maxX;
                xSpeed = -xSpeed;
            } else if( y < 0 ) {
                // bounce at the top side
                y = 0;
                ySpeed = -ySpeed;
            } else if( y > maxY ) {
                // bounce at the bottom size
                y = maxY;
                ySpeed = -ySpeed;
            } // end if-else block
        } // end method go()

        void draw( Graphics g ) {
            g.fillOval( x , y , diameter , diameter );
        } // end method draw
    } // end inner class Ball


    public static void main( String[] args ) {
        JFrame window = new JFrame();
        window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        BouncingBall animation = new BouncingBall();
        animation.setPreferredSize( new Dimension( 500 , 500 ) );
        animation.setBackground( Color.white );
        window.add( animation );
        window.pack();
        window.setVisible( true );

        animation.startAnimation();
    } // end method main
} // end class BouncingBall

什么是问题吗?我是否需要在Ubuntu中更改某些设置?
我还已经在paintComponent方法中放置了一些测试代码,如下所示:

What is the problem? Do I have to change some settings in my Ubuntu? I've also put some test code inside the paintComponent method as follows:

protected void paintComponent( Graphics g ) {
    System.out.println( "paintComponent call number: " + counter );
    ++counter;
    super.printComponent( g );
    ball.draw( g );
}  

在MovingBall类中声明的可变计数器初始值为0。我观察到,paintComponent每秒的调用次数远远超过了其显示的JPanel的实际刷新率。

with variable counter initial value of 0 declared in class MovingBall. I observed that the number of paintComponent's calls per second is much more than the actual refresh rate of the JPanel as it appears.

推荐答案

Windows默认情况下启用视频加速,而Linux默认情况下未启用视频加速。 (这已经有很多年了;我可能已经宣誓为最近的Java版本更改了默认设置,但是显然我错了。)

Video acceleration is enabled by default in Windows, but is not enabled by default in Linux. (This has been true for many years now; I could have sworn this default was changed for recent Java releases, but evidently I was wrong.)

您可以启用OpenGL 以获得加速的性能:

You can enable OpenGL to get accelerated performance:

public static void main( String[] args ) {
    System.setProperty("sun.java2d.opengl", "true");

    JFrame window = new JFrame();

或者,您可以在命令行上设置属性:

Alternatively, you can set the property on the command line:

java -Dsun.java2d.opengl=true BouncingBall

这篇关于在Linux中运行生涩的Java动画程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆