简体   繁体   English

在 Linux 中使我的 java 摇摆动画更流畅

[英]Make my java swing animation smoother in Linux

Well I'm following this tutorial to create my own animation and when I run it everything works well on Windows but in Ubuntu 17.04 the animation is smooth only if the mouse is moving over the app window.好吧,我正在按照本教程创建我自己的动画,当我运行它时,一切在 Windows 上运行良好,但在 Ubuntu 17.04 中,只有鼠标在应用程序窗口上移动时,动画才会流畅。 If don't it works like " in fits and starts" (I think is a good translation).如果不是,它就像“断断续续”(我认为这是一个很好的翻译)。 here is my code:这是我的代码:

import javax.swing.Box;
import javax.swing.BoxLayout;
import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Window extends JFrame {

private Panel pan = new Panel();
private JButton play = new JButton("play");
private JButton pause = new JButton("pause");

public Window(String titre) {

    this.setTitle(titre);
    this.setSize(900, 600);
    this.setLocationRelativeTo(null);
    this.setAlwaysOnTop(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Box b1 = Box.createHorizontalBox();
    b1.add(play);
    b1.add(pause);

    Box b2 = Box.createVerticalBox();
    b2.add(b1);
    this.setContentPane(pan);
    this.getContentPane().add(b2);
    this.setVisible(true);
    this.go();

}

private void go() {
    // Les coordonnées de départ de notre rond
    int x = pan.getPosX(), y = pan.getPosY();
    // Le booléen pour savoir si l'on recule ou non sur l'axe x
    boolean backX = false;
    // Le booléen pour savoir si l'on recule ou non sur l'axe y
    boolean backY = false;

    // Dans cet exemple, j'utilise une boucle while
    // Vous verrez qu'elle fonctionne très bien
    while (true) {
        // Si la coordonnée x est inférieure à 1, on avance
        if (x < 1)
            backX = false;

        // Si la coordonnée x est supérieure à la taille du Panneau moins la taille du
        // rond, on recule
        if (x > pan.getWidth() - 50)
            backX = true;

        // Idem pour l'axe y
        if (y < 1)
            backY = false;
        if (y > pan.getHeight() - 50)
            backY = true;

        // Si on avance, on incrémente la coordonnée
        // backX est un booléen, donc !backX revient à écrire
        // if (backX == false)
        if (!backX)
            pan.setPosX(++x);

        // Sinon, on décrémente
        else
            pan.setPosX(--x);

        // Idem pour l'axe Y
        if (!backY)
            pan.setPosY(++y);
        else
            pan.setPosY(--y);

        // On redessine notre Panneau
        pan.repaint();

        // Comme on dit : la pause s'impose ! Ici, trois millièmes de seconde
        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

}

seconde class:第二类:

import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class Panel extends JPanel {

private int posX = -50;
private int posY = -50;

public void paintComponent(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    g.setColor(Color.red);
    g.fillOval(posX, posY, 50, 50);
}

public int getPosX() {
    return posX;
}

public void setPosX(int posX) {
    this.posX = posX;
}

public int getPosY() {
    return posY;
}

public void setPosY(int posY) {
    this.posY = posY;
}
}

and the main:和主要的:

import javax.swing.JFrame;
public class test {
public static void main(String [] args) {
    Window window = new Window("Animation");

}

}

Thank you in advance to any one who may be able to give me some ideas :)预先感谢任何能给我一些想法的人:)

Try calling试试打电话

Toolkit.getDefaultToolkit().sync(); Toolkit.getDefaultToolkit().sync();

After you do做完之后

pan.repaint(); pan.repaint();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM