图形闪烁

时间:2011-10-21 21:32:34

标签: java swing graphics2d keylistener

当我运行程序时,一切正常,一切正常,但图形不断闪现。我是新手做这样的事情,我不知道如何解决这个问题。

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;


public class MovementTest extends Frame implements Runnable, MouseMotionListener, KeyListener {

    private static Thread thread;
    private int mouseX, mouseY;
    private boolean movingRight;
    private boolean movingLeft;
    private boolean movingUp;
    private boolean movingDown;

    private Rectangle player = new Rectangle(50, 50, 100, 100);
    private Rectangle wallOne = new Rectangle(400, 50, 100, 100);

    public MovementTest() {
        setSize(800, 600);
        setVisible(true);
        addKeyListener(this);
        addMouseMotionListener(this);
    }

    public static void main(String[] args) {
        thread = new Thread(new MovementTest());
        thread.start();
    }

    @Override
    public void run() {
        try {
            while(true) {
                if(player.intersects(wallOne)) {
                    thread.stop();
                    break;
                }
                if(movingRight) {
                    player.x++;
                } else if(movingLeft) {
                    player.x--;
                }
                if(movingUp) {
                    player.y--;
                } else if(movingDown) {
                    player.y++;
                }
                paint(getGraphics());
                Thread.sleep(5);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_D)
            movingRight = true;
        if (key == KeyEvent.VK_A)
            movingLeft = true;
        if (key == KeyEvent.VK_W)
            movingUp = true;
        if (key == KeyEvent.VK_S)
            movingDown = true;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_D)
            movingRight = false;
        if (key == KeyEvent.VK_A)
            movingLeft = false;
        if (key == KeyEvent.VK_W)
            movingUp = false;
        if (key == KeyEvent.VK_S)
            movingDown = false;
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void paint(Graphics g) {
        g.drawString("x: " + mouseX + " y: " + mouseY, 5, 35);
        g.drawRect(player.x, player.y, 100, 100);
        g.drawRect(wallOne.x, wallOne.y, 100, 100);
        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();
    }
}

2 个答案:

答案 0 :(得分:4)

你肯定想要研究双缓冲。现在你正在做的是直接将所有内容绘制到屏幕上,用户就会看到这一点。双缓冲的作用是首先在屏幕外缓冲区(图像)上绘制所有内容,一旦计算完所有,并且图像就绪,您将屏幕外缓冲区绘制到可见缓冲区(因此用户只能看到最后一帧而不是中间帧。

这与OpenGL或Direct3D应用程序最常见的概念类似。他们有两个缓冲区。一切都被屏幕缓冲区吸引,一旦完成,交换缓冲区,清除现在关闭屏幕的缓冲区,之后再次在其上绘制新图像。

正如glowcoder所指出的那样,你有一个非常高的fps。尝试将其与显示器的刷新率同步,以避免浪费资源,而不是必要的消耗。

答案 1 :(得分:1)

从您的绘画方法中重新绘制。你真的不希望它在那里。这就是造成你问题的原因。

之后,添加它 - 它有助于长期保持组织。

public void update() {
    repaint();
}

然后,添加重绘方法来移动东西。您还需要为paint方法添加一个clear。这实际上会让你的矩形移动。 更新:我注意到你已经有了重绘东西的东西了。优秀。它真的很快(200 fps !!)所以你肯定想看看双缓冲。

然后我们可以谈论双缓冲:)但你可以谷歌那个或那样。