如何清除Java中的框架屏幕? [英] how do i clear my frame screen in java?

查看:80
本文介绍了如何清除Java中的框架屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个积木游戏.我希望屏幕每隔0.1秒就能变清晰一点,以便我可以在边框屏幕上重绘所有内容.

I am making a brick game. I want the screen to get clear after every 0.1 second so that i can redraw every thing on the frame screen.

有什么方法可以直接清除框架屏幕而不会发生任何事件?

Is there any way to directly clear the frame screen without any event occurence??

推荐答案

您应该覆盖

public void paint(Graphics g)

并在那里完成所有绘图.

and do all your drawing in there.

然后您启动一个计时器,该计时器会调用

Then you start a timer, which calls

repaint();

这是一个基本示例:

public class MainFrame extends JFrame {

    int x = -1;
    int inc;

    public MainFrame() {
        Timer timer = new Timer(10, new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                MainFrame.this.repaint();
            }
        });
        timer.start();
    }

    public void paint(Graphics g) {
        // don't call super.paint(g), we do all the painting

        if(x > getWidth()) inc = -5;
        if(x < 0) inc = 5;

        x += inc;

        // here we clear everything
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(Color.BLUE);
        g.drawLine(x, 0, getWidth()-x, getHeight());
    }

    public static void main(String[] args) {
        MainFrame mainFrame = new MainFrame();
        mainFrame.setSize(800, 600);
        mainFrame.setVisible(true);
    }
}

这篇关于如何清除Java中的框架屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆