how do i clear my frame screen in java?(如何在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中清除我的框架屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在java中清除我的框架屏幕?


基础教程推荐
- 不推荐使用 Api 注释的描述 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java Swing计时器未清除 2022-01-01