Java 游戏报错
3.run(UnknownSource)atjava.security.AccessController.doPrivileged(NativeMethod)atjava.security.ProtectionDomainJavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue4.run(UnknownSource)atjava.security.AccessController.doPrivileged(NativeMethod)atjava.security.ProtectionDomain 4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain4.run(UnknownSource)atjava.security.AccessController.doPrivileged(NativeMethod)atjava.security.ProtectionDomainJavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
public class MyGameFrame extends Frame {
/**
*
*/
private static final long serialVersionUID = 905833189003569926L;
Image planeImg = GameUtil.getImage("images/plane.png");
Image bg = GameUtil.getImage("images/bg.jpg");
Plane plane = new Plane(planeImg,300,150);
Shell[] shells = new Shell[60];
Explode bao ;
Date startTime = new Date();
Date endTime;
int period; //游戏持续的时间
public void paint(Graphics g) { //自动被调用。 g相当于一只画笔
g.drawImage(bg, 0, 0, null);
plane.drawSelf(g);
//画出所有的炮弹
for(int i=0;i<shells.length;i++){
shells[i].draw(g);
//飞机和炮弹的碰撞检测
boolean peng = shells[i].getRect().intersects(plane.getRect());
if(peng){
plane.live = false;
if(bao ==null){
bao = new Explode(plane.x, plane.y);
}
bao.draw(g);
}
//计时
if(!plane.live){
if(endTime==null) {
endTime =new Date();
}
period = (int)((endTime.getTime()-startTime.getTime())/1000);
printInfo(g,"时间:"+period+"秒",40,120,240,Color.BLUE);
}
}
}
public void printInfo (Graphics g,String str, int size, int x,int y,Color color) {
g.setColor(color);
g.getFont();
g.drawString(str, x, y);
}
//重画窗口
class PaintThread extends Thread {
public void run() {
while(true){
repaint(); //重画
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//定义键盘监听的内部类
class KeyMonitor extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
plane.addDirection(e);
}
@Override
public void keyReleased(KeyEvent e) {
plane.minusDirection(e);
}
}
public void launchFrame(){
this.setTitle("初作");
this.setVisible(true);
this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
this.setLocation(200, 300);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new PaintThread().start(); //启动重画窗口的线程
addKeyListener(new KeyMonitor()); //给窗口增加键盘的监听
//初始化60个炮弹
for(int i=0;i<shells.length;i++){
shells[i] = new Shell();
}
}
public static void main(String[] args) {
MyGameFrame f = new MyGameFrame(http://www.my516.com);
f.launchFrame();
}
private Image offScreenImage = null;
public void update(Graphics g) {
if(offScreenImage == null)
offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//这是游戏窗口的宽度和高度
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
---------------------