Java Awt和swing程序以2 fps的速度运行,为什么呢?

时间:2018-10-07 16:51:09

标签: java performance swing awt

当我运行此代码时,方块响应速度非常慢,就像程序以2 fps的速度运行一样。你能告诉我为什么它这么慢,我如何使其运行更快?另外,我会说我曾经在pygame(python)中进行编码,并且有一个系统允许我设置FPS,但是如果您可以向我展示如何设置FPS,那么我在Java中找不到类似的内容。感激不尽。

感谢您的回答。

package World;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;

public class Map {
    private final String title = "The Lord of the Dungeon";
    private final int width = 1600;
    private final int height = width / 16 * 9;
    private Player player = new Player(width / 2, height / 2, 50);
    //class Player contains only: int x, int y, int width, int height
    public Map () {
        //generate map
    }
    public void draw () {
        boolean running = true;

        //Creating the frame.
        JFrame frame = new JFrame(title);
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);

        //Creating the canvas.
        Canvas canvas = new Canvas();
        canvas.setSize(width, height);
        canvas.setBackground(Color.BLACK);
        canvas.setVisible(true);
        frame.add(canvas);
        canvas.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent keyEvent) {

            }

            @Override
            public void keyPressed(KeyEvent keyEvent) {
                int keyCode = keyEvent.getKeyCode();
                if (keyCode == KeyEvent.VK_W) {
                    player.setY(player.getY() - 10);
                }
                else if (keyCode == KeyEvent.VK_A) {
                    player.setX(player.getX() - 10);
                }
                else if (keyCode == KeyEvent.VK_S) {
                    player.setY(player.getY() + 10);
                }
                else if (keyCode == KeyEvent.VK_D) {
                    player.setX(player.getX() + 10);
                }
            }

            @Override
            public void keyReleased(KeyEvent keyEvent) {

            }
        });
        canvas.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent mouseEvent) {

            }

            @Override
            public void mousePressed(MouseEvent mouseEvent) {
                int mouseCode = mouseEvent.getButton();
                if (mouseCode == 1) {
                    System.out.println("1");
                }
            }

            @Override
            public void mouseReleased(MouseEvent mouseEvent) {

            }

            @Override
            public void mouseEntered(MouseEvent mouseEvent) {

            }

            @Override
            public void mouseExited(MouseEvent mouseEvent) {

            }
        });


        canvas.createBufferStrategy(3);
        BufferStrategy bufferStrategy;
        Graphics graphics;

        while (running) {
            bufferStrategy = canvas.getBufferStrategy();
            graphics = bufferStrategy.getDrawGraphics();
            graphics.clearRect(0, 0, width, height);

            graphics.fillRect(player.getX(), player.getY(), player.getWidth(), player.getHeight());


            bufferStrategy.show();
            graphics.dispose();
        }
    }
}

这是播放器类:

public class Player {
    private int x;
    private int y;
    private int width;
    private int height;
    public Player (int x, int y, int width) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = width / 9 * 16;
    }
    public int getX () {
        return this.x;
    }
    public int getY () {
        return this.y;
    }
    public int getWidth () {
        return this.width;
    }
    public int getHeight () {
        return this.height;
    }
    public void setX (int newX) {
        this.x = newX;
    }
    public void setY (int newY) {
        this.y = newY;
    }
    public void setWidth (int newWidth) {
        this.width = newWidth;
        this.height = newWidth / 9 * 16;
    }
}

0 个答案:

没有答案