Java的Paint方法存在问题,刷新速度荒谬

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java的Paint方法存在问题,刷新速度荒谬相关的知识,希望对你有一定的参考价值。

我正在开发一个非常简单的R-Type版本作为大学的工作,但尽管它有效,但工艺速度很慢,所以运动是丑陋和笨拙的。我使用方法重绘来刷新屏幕,还有其他方法或方法比它更好吗?

Video of Movement

主面板上的绘制方法

@Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawImage(fondo, 0, 0,1200,600,this);
        pj.paint(g2);
        g2D=g2;

    }

PJ的绘画方法

public void paint(Graphics2D g) {

    g.drawImage(imagen,x,y,this);
}

PJ的移动方法

public void move (KeyEvent e)  {
    int dx = 0; int dy = 0;
    int code = e.getKeyCode();

    switch (code) {
    case KeyEvent.VK_Q: dy-=1; break;
    case KeyEvent.VK_A: dy+=1; break;
    case KeyEvent.VK_P: dx+=1; break;
    case KeyEvent.VK_O: dx-=1; break;
    }

    int x = (getX()<maxX&&getX()!=0) ? getX()+dx : getX();
    int y = (getY()<maxY&&getY()!=0) ? getY()+dy : getY();

    if (getY()>=maxY||getY()==0) {
        if (dy==+1) y=y+1;
    }

    setPosicion(x, y); 

}
答案
  • 图像fondo应该已经缩放到1200x600。
  • 我不确定,但需要super.paint(g)吗?你也可以使用paintComponent

必须正确完成事件处理(您似乎在按键上向下移动1个像素)。我会设置方向和速度(1px),然后将它留给挥杆计时器进行连续移动。

重绘最好是弹性/灵活:repaint(20L)(每秒50帧);关键事件可能与EventQueue.invokeLater(new Runnable() { ... });

特别是你可以使用更改区域的重绘。

另一答案

你可以找到类似程序here的一个很好的例子。该示例演示了如何创建一个新线程并让该线程在主循环中每次迭代都进入休眠状态。

Here是关于在Java中加载游戏图像的另一个问题。

看起来摇摆本身在游戏中使用图像非常糟糕。您可能需要考虑使用更合适的库。

另一答案

下面是使用背景作为简单游戏循环的简单示例。它更新游戏对象的状态并计算所需的延迟以保持所需的fps。

游戏对象(Ship)具有在短时间内加速/减速的能力

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Path2D;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class AnimationTest {

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

    public AnimationTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new GamePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class GamePane extends JPanel {

        private Ship ship;

        public GamePane() {

            ship = new Ship();
            Thread thread = new Thread(new MainLoop(this));
            thread.setDaemon(true);
            thread.start();

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            // Key controls...
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "upPressed");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "downPressed");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "upReleased");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "downReleased");

            am.put("upPressed", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Change the direction...
                    ship.setDirection(-1);
                    // Accelerate by 1 per frame
                    ship.setVelocity(1);
                }

            });
            am.put("downPressed", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Change direction
                    ship.setDirection(1);
                    // Accelerate by 1 per frame
                    ship.setVelocity(1);
                }

            });
            am.put("upReleased", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Deccelerate by 1 per frame
                    ship.setVelocity(-1);
                }

            });
            am.put("downReleased", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Deccelerate by 1 per frame
                    ship.setVelocity(-1);
                }

            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        public void updateState() {
            // Update the state of the game objects.
            // This would typically be better done in 
            // some kind of model
            ship.update(getWidth(), getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Paint the game state...
            Graphics2D g2d = (Graphics2D) g.create();
            ship.paint(g2d);
            g2d.dispose();
        }

    }

    public class MainLoop implements Runnable {

        private GamePane pane;
        private int fps = 25;

        public MainLoop(GamePane pane) {
            this.pane = pane;
        }

        @Override
        public void run() {
            // Wait until the screen is ready
            while (pane.getHeight() <= 0) {
                try {
                    Thread.sleep(125);
                } catch (InterruptedException ex) {
                }
            }
            // Main loop
            while (true) {
                // Start time loop
                long startTime = System.currentTimeMillis();
                // Update the game state
                pane.updateState();
                // Calculate the amount of time it took to update
                long elasped = System.currentTimeMillis() - startTime;
                // Calculate the number of milliseconds we need to sleep
                long sleep = Math.round((1000f / fps) - elasped);
                pane.repaint();
                if (sleep > 0) {
                    try {
                        Thread.sleep(sleep);
                    } catch (InterruptedException ex) {
                    }
                }
            }
        }

    }

    public static class Ship {

        public static int MAX_SPEED = 8;
        private int direction = 0;
        private int velocity = 0;
        private int x;
        private int y;
        private int speed = 0;
        private Path2D shape;
        private boolean initState;

        public Ship() {
            shape = new Path2D.Float();
            shape.moveTo(0, 0);
            shape.lineTo(5, 5);
            shape.lineTo(0, 10);
            shape.lineTo(0, 0);
            shape.closePath();
            initState = true;
        }

        public void setDirection(int direction) {
            this.direction = direction;
        }

        public void setVelocity(int velocity) {
            this.velocity = velocity;
        }

        public void update(int width, int height) {
            if (initState) {
                y = (height - 10) / 2;
                initState = false;
            } else {
                // Add the velocity to the speed
                speed += velocity;
                // Don't over accelerate
                if (speed > MAX_SPEED) {
                    speed = MAX_SPEED;
                } else if (speed < 0) {
                    speed = 0;
                }
                // Adjust out position if we're moving
                if (speed > 0) {
                    y += (direction * speed);
                }

                // Bounds check...
                if (y - 5 < 0) {
                    y = 5;
                } else if (y + 5 > height) {
                    y = height - 5;
                }
            }
        }

        public void paint(Graphics2D g2d) {
            int yPos = y - 5;
            g2d.translate(10, yPos);
            g2d.fill(shape);
            g2d.translate(-10, -yPos

以上是关于Java的Paint方法存在问题,刷新速度荒谬的主要内容,如果未能解决你的问题,请参考以下文章

Java JPanel中,repaint(),paint(),方法的区别?

我用DrawDib显示图片为啥WM_PAINT会闪烁

如何在 Java 中刷新 GUI?

java中paint()的具体用法是啥?

paint() 方法不会在 JPanel 上绘制

Java绘图