【问题标题】:Make my java swing animation smoother in Linux在 Linux 中让我的 java swing 动画更流畅
【发布时间】:2018-03-16 19:02:22
【问题描述】:

好吧,我正在关注这个tutorial 来创建我自己的动画,当我运行它时,一切都在 Windows 上运行良好,但在 Ubuntu 17.04 中,只有当鼠标在应用程序窗口上移动时,动画才会流畅。如果不是这样,它就像“断断续续”(我认为这是一个很好的翻译)。 这是我的代码:

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();
        }
    }
}

}

第二类:

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;
}
}

还有主要的:

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

}

}

提前感谢任何能给我一些想法的人:)

【问题讨论】:

  • 这可能是Linux有一些资源节省,如果窗口不活动,为什么要浪费资源绘制它?
  • 好吧,我在监控系统上放了高资源,它也一样。此外,当我玩我的世界时,即使我的鼠标不在窗口上方也能正常工作哈哈
  • 我会提防你 paintComponent 方法依赖于可以由另一个线程修改的属性 - 如果你想更好地控制绘画过程,你应该考虑使用 BufferStrategy
  • 是的,我是java中的菜鸟,甚至是动画方面的菜鸟,我正在关注这个tuto但我才第二周,我希望几周后事情会更正确;)

标签: java swing ubuntu animation smoothing


【解决方案1】:

尝试调用

Toolkit.getDefaultToolkit().sync();

做完之后

pan.repaint();

【讨论】:

  • @DaniSanz 这是一个非常广泛的问题,首先看一下“JavaDocs - ”这个方法可以确保显示是最新的。它对动画很有用。”,我还建议您查看Painting in Swing,了解有关绘画过程实际工作原理的更多详细信息
  • Linux 的图形调度存在问题,这意味着它由于某种原因变得越来越慢。该调用占用了宝贵的 CPU 周期(实际上相当多),因此请使用 System.getProperty("os.name") 来确定是否需要使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 2012-11-05
  • 1970-01-01
  • 2011-09-13
相关资源
最近更新 更多