博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java项目之——坦克大战04.1
阅读量:2380 次
发布时间:2019-05-10

本文共 1763 字,大约阅读时间需要 5 分钟。

功能:使用双缓冲消除闪烁现象(消除闪烁是应该的,不过处理有点不太好理解)

方法:将是所有的图像都画在虚拟图片上面然后显示出来。

关键代码:

Image offScreenImage = null;public void update(Graphics g) {		if(offScreenImage == null){			offScreenImage = this.createImage(800,600);		}		Graphics gOffScreen = offScreenImage.getGraphics();		Color c = gOffScreen.getColor();		gOffScreen.setColor(Color.GREEN);		gOffScreen.fillRect(0, 0, 800, 600);		gOffScreen.setColor(c);		print(gOffScreen);		g.drawImage(offScreenImage, 0, 0, null);	}
Graphics gOffScreen = offScreenImage.getGraphics();
全部代码:

public class TankClient extends Frame {	int x = 30; int y = 30;                //定义在方法外面	Image offScreenImage = null;		public void paint(Graphics g) {		Color c = g.getColor();		g.setColor(Color.RED);		g.fillOval(x, y, 30, 40);		g.setColor(c);				y += 5;	}	public void update(Graphics g) {		if(offScreenImage == null){			offScreenImage = this.createImage(800,600);		}		Graphics gOffScreen = offScreenImage.getGraphics();		Color c = gOffScreen.getColor();		gOffScreen.setColor(Color.GREEN);		gOffScreen.fillRect(0, 0, 800, 600);		gOffScreen.setColor(c);		print(gOffScreen);		g.drawImage(offScreenImage, 0, 0, null);	}	public void lauchFrame(){		this.setSize(800,600);		this.setTitle("TankWar");		this.setLocation(80, 60);		this.setVisible(true);		this.addWindowListener(new WindowAdapter(){			public void windowClosing(WindowEvent e) {			    System.exit(0);				}		});		this.setResizable(false);		new Thread(new paintThread()).start();	}	public static void main(String[] args) {		TankClient tc = new TankClient();		tc.lauchFrame();	}	private class paintThread implements Runnable {   //线程 内部类 为此线程服务		public void run() {			while(true){				repaint();				try {					Thread.sleep(50);				} catch (Exception e) {					e.printStackTrace();				}			}		}			}	}

转载地址:http://zlrxb.baihongyu.com/

你可能感兴趣的文章
Python列表最常见的问题【总结】
查看>>
从C++到Python,一个游戏程序员的进阶之路
查看>>
Python3优雅操作-时间处理与定时任务
查看>>
国内外大厂都在使用Python,学习第一步Python3 的入门安装!
查看>>
程序员工资高,却有很多人想转行,理由很简单!
查看>>
python优雅操作11行代码,竟然发现了室友U盘里藏着这些...
查看>>
数据预处理速度高倍提升,3行python代码简单搞定!
查看>>
零基础如何迅速学习python?
查看>>
同样是学习Python的程序员,为什么他却可以用Python两年躺赚200W
查看>>
python基础题目大全,测试你的水平,巩固知识(含答案)
查看>>
Python用了这么多年,总结出超实用的功能和特点
查看>>
Python爬虫入门,8个常用爬虫技巧盘点
查看>>
Python轻松查看微信撤回消息,秘密无处可藏
查看>>
国外Python黑客技术,攻击自动化玩得真6
查看>>
最后悔的是遇见了Python!
查看>>
前端程序员市场分析:前面是火海,后面是刀山,走还是不走?
查看>>
做程序员一般都需要什么学历?大厂招不招低学历?你想知道的都在这里了
查看>>
零基础html5网站开发学习步骤方法
查看>>
程序员真实生活曝光,蹦迪也带着电脑,网友解释:这样才有安全感!
查看>>
CSS的23个垂直居中技巧,你都学会了吗?
查看>>