【问题标题】:Smooth out Java paint animations平滑 Java 绘制动画
【发布时间】:2013-03-10 23:42:15
【问题描述】:

我想让我的应用程序绘制动态图像比它当前绘制它们的方式更平滑一点。我不知道该怎么做。

这是我的主游戏线程的样子:

@Override
public void run(){
    int delay = 500; //milliseconds
    ActionListener taskPerformer = new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent evt){
            Car car = new Car();
            int speed = (int)(3 + Math.floor(Math.random() * (6 - 3)));
            car.setSpeed(speed);
            MainLoop.this.gameObjects.vehicles.add(car.create("/Media/Graphics/blueCar.png", width - 20, 78));
            car.driveTo(0, 78);
        }
    };
    new Timer(delay, taskPerformer).start();
    try{
        while(true){
            this.repaint();
            for(GameObject go : this.gameObjects.vehicles){
                // loops through objects to move them
                Vehicle vh = (Vehicle) go;
                this.moveVehicle(vh);
                if(vh.getX() <= vh.getDestX()){
                    vh.markForDeletion(true);
                }
            }
            this.gameObjects.destroyVehicles();
            Thread.sleep(1);
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

这是一种计算项目下一个 x/y 位置的方法

protected void moveVehicle(Vehicle vh){
    int cx = vh.getX();
    int dx = vh.getDestX();
    int cy = vh.getY();
    int dy = vh.getDestY();
    // move along x axis
    // getMaxSpeed() = Number between 3 and 6
    if(cx > dx && vh.movingX() == -1){
        vh.setX(cx - vh.getMaxSpeed());
    }else if(cx < dx && vh.movingX() == 1){
        vh.setX(cx + vh.getMaxSpeed());
    }else{
        vh.setX(dx);
    }

    // move along y axis
    // getMaxSpeed() = Number between 3 and 6
    if(cy > dy && vh.movingY() == -1){
        vh.setY(cy - vh.getMaxSpeed());
    }else if(cy < dy && vh.movingY() == 1){
        vh.setY(cy + vh.getMaxSpeed());
    }else{
        vh.setY(dy);
    }
}

这是我的绘画方法:

@Override
public void paintComponent(Graphics graphics){
    super.paintComponent(graphics);
    Graphics2D g = (Graphics2D) graphics;

    for(GameObject go : gameObjects.vehicles){
        g.drawImage(go.getSprite(), go.getX(), go.getY(), this);
    }
}

这可能比需要的信息更多,但我想知道,我应该怎么做才能使项目从 left -&gt; right top -&gt; bottom 尽可能顺利地移动,而不会造成太大的性能损失?

编辑:请求的 sscce:

package sscce;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Sscce extends JPanel implements Runnable{

    ArrayList<Square> squares = new ArrayList<>();

    public Sscce(){
        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.add(this);
        Thread t = new Thread(this);
        t.start();
    }

    public static void main(String[] args){
        new Sscce();
    }

    @Override
    public void run(){
        int delay = 500; //milliseconds
        ActionListener taskPerformer = new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent evt){
                Square squ = new Square();
                Sscce.this.squares.add(squ);
                squ.moveTo(0);
            }
        };
        new Timer(delay, taskPerformer).start();
        while(true){
            try{
                for(Square s : this.squares){
                    int objX = s.getX();
                    int desX = s.getDestX();
                    if(objX <= desX){
                        System.out.println("removing");
                        this.squares.remove(s);
                    }else{
                        s.setX(s.getX() - 10);
                    }
                }
                this.repaint();
                Thread.sleep(30);
            }catch(Exception e){
            }
        }
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        for(Square s : squares){
            g.setColor(Color.blue);
            g.fillRect(s.getX(), s.getY(), 50, 50);
        }
    }
}

class Square{

    public int x = 0, y = 0, destX = 0;

    public Square(){
        this.x = 400;
        this.y = 100;
    }

    public void moveTo(int destX){
        this.destX = destX;
    }

    public int getX(){
        return this.x;
    }
    public int getDestX(){
        return this.destX;
    }

    public void setX(int x){
        this.x = x;
    }

    public int getY(){
        return this.y;
    }
}

【问题讨论】:

  • 你也看看thisthisthisthisthis(只是因为我喜欢它)
  • 让我们开始吧,如果没有一个有效的例子,它就不可能帮助你。但是让我们从毫无意义的 Thread.sleep(1) 开始;这是。然后继续提到 Swing 可以在您更新模型时执行重绘
  • @MadProgrammer 我添加了一个 sscce。如果我不输入Thread.sleep(1);,则不会绘制任何内容。
  • Thread.sleep(1) 的问题与Thread.sleep(0) 一样接近,没有区别。尝试生成进度条?
  • Thread.sleep(1) 的另一个问题是大多数人甚至看不出有什么不同。粗略地说,每秒 25fps 就足够了(大约 40 毫秒)。问题在于距离变化多于更新次数......

标签: java multithreading repaint


【解决方案1】:

首先请注意,由于某种原因,我在 MacOS 下运行 JPanel 时遇到了实现 Runnable 的问题 - 我不知道为什么,但这就是我将其移出的原因。

您的squares 可能会在用于绘制时被更新,这将导致异常(此外,在迭代时从列表中删除元素也不是一个好主意;))

相反,我有两个列表。我有一个模型,列表可以修改,然后是paint 方法可以使用的绘制列表。这允许线程在进行绘制过程时修改模型。

为了防止任何冲突,我添加了一个锁,它可以防止一个线程修改/访问绘制列表,而另一个线程将其锁定。

现在。回到真正的问题。您遇到的主要问题不是更新之间的时间量,而是您移动的距离。缩短距离(使其变慢)并使更新标准化。

大多数人不会注意到超过 25fps 的任何东西,因此尝试做更多的事情只会浪费 CPU 周期并让重绘管理器挨饿,从而阻止它实际更新屏幕。

可以肯定的是,这是一个平衡的行为......

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestAnimation11 extends JPanel {

    private ArrayList<Square> squares = new ArrayList<>();
    private ReentrantLock lock;

    public TestAnimation11() {
        lock = new ReentrantLock();
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }
                JFrame frame = new JFrame();
                frame.setSize(500, 500);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.add(TestAnimation11.this);
                Thread t = new Thread(new UpdateEngine());
                t.start();
            }
        });
    }

    public static void main(String[] args) {
        new TestAnimation11();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Square[] paint = null;
        lock.lock();
        try {
            paint = squares.toArray(new Square[squares.size()]);
        } finally {
            lock.unlock();
        }
        for (Square s : paint) {
            g.setColor(Color.blue);
            g.fillRect(s.getX(), s.getY(), 50, 50);
        }
    }

    public class UpdateEngine implements Runnable {

        private List<Square> model = new ArrayList<>(squares);

        @Override
        public void run() {
            int ticks = 0;
            List<Square> dispose = new ArrayList<>(25);
            while (true) {
                ticks++;
                dispose.clear();
                for (Square s : model) {
                    int objX = s.getX();
                    int desX = s.getDestX();
                    if (objX <= desX) {
                        dispose.add(s);
                    } else {
                        s.setX(s.getX() - 2);
                    }
                }
                model.removeAll(dispose);
                if (ticks == 11) {
                    Square sqr = new Square();
                    sqr.moveTo(0);
                    model.add(sqr);
                } else if (ticks >= 25) {
                    ticks = 0;
                }
                lock.lock();
                try {
                    squares.clear();
                    squares.addAll(model);
                } finally {
                    lock.unlock();
                }
                repaint();
                try {
                    Thread.sleep(40);
                } catch (Exception e) {
                }
            }
        }
    }

    class Square {

        public int x = 0, y = 0, destX = 0;

        public Square() {
            this.x = 400;
            this.y = 100;
        }

        public void moveTo(int destX) {
            this.destX = destX;
        }

        public int getX() {
            return this.x;
        }

        public int getDestX() {
            return this.destX;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return this.y;
        }
    }
}

【讨论】:

  • 谢谢,我稍后再看看!
猜你喜欢
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多