说明
在java的小游戏编程中,如果主人公移动,需要重绘界面
此时用到 paint()、paintComponent()、repaint() 方法,这几个方法区别还是很大的
paint()
实现了双缓冲,显示图片时不会 闪烁,所以 不要覆写,会导致一系列问题
paintComponent()
可以重写该方法,画上图片
@Override
public void paintComponent(Graphics g) {
//下面代码可以消除之前的画面
super.paintComponent(g);
// 在下面画上自己的图片
}
具体代码详见:swing JPanel显示大图片
repaint()
触发重画,触发执行 paintComponent() 方法,调用该方法,如:一秒重画30次,即fps是30,如下:
Thread t = new Thread(){
@Override
public void run() {
while(repaintTag) {
jpanel.repaint(); //重画
try {
Thread.sleep(33); // 每秒刷新30次
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
debug.debug(null,"停止刷新!");
}
};
t.start();
参考:
https://blog.csdn.net/xr506660464/article/details/28423665
https://www.cnblogs.com/yizhaoAI/p/10005296.html