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中清除我的框架屏幕?
基础教程推荐
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 Windows 证书导入 Java 2022-01-01
