弹球Java代码文档_【JAVA】简单动态交互程序——弹球

importjava.awt.Frame;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Point;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjavax.swing.JApplet;importjavax.swing.JPanel;importjava.awt.Color;importjava.util.ArrayList;importjava.util.List;importjava.awt.event.*;publicclassBounceBallextendsFrame {publicstaticfinalintBOARD_WIDTH=540;publicstaticfinalintBOARD_HEIGHT=570;

Listballs=newArrayList();

Image offScreenImage=null;//双缓冲用的classBall {intdirection=0;intx=0;inty=0;

Color color;publicstaticfinalintBALLSIZE=20;publicfinalColor[] colors={Color.black,Color.blue,Color.cyan,Color.green,Color.orange,Color.pink,Color.red};publicvoiddrawBall(Graphics g) {if(direction==1) g.clearRect(x, y-5,20,20);elseg.clearRect(x,y+5,20,20);

g.setColor(color);

g.fillOval(x,y,20,20);

}publicBall(intx,inty,intballcount) {this.x=x;this.y=y;this.direction=1;this.color=colors[ballcount%7];

}

}publicvoidpaint(Graphics g) {for(inti=0;i

Ball b=null;

b=balls.get(i);

b.drawBall(g);

}

}//双缓冲用的@Overridepublicvoidupdate(Graphics g) {if(offScreenImage==null){

offScreenImage=this.createImage(BOARD_WIDTH, BOARD_HEIGHT);

}

Graphics gOffScreen=offScreenImage.getGraphics();

Color c=gOffScreen.getColor();

gOffScreen.setColor(Color.LIGHT_GRAY);

gOffScreen.fillRect(0,0, BOARD_WIDTH, BOARD_HEIGHT);

gOffScreen.setColor(c);

paint(gOffScreen);

g.drawImage(offScreenImage,0,0,null);for(inti=0;iBOARD_HEIGHT-Ball.BALLSIZE) balls.get(i).direction=0;if(balls.get(i).y

}

}//重画privateclassPaintThreadimplementsRunnable {publicvoidrun() {try{while(true){

repaint();

Thread.sleep(50);

}

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}privateclassMouseMonitorextendsMouseAdapter{publicvoidmouseClicked(MouseEvent e) {intx=e.getX();inty=e.getY();

Ball b=newBall(x,y,balls.size());

balls.add(b);

}

}publicvoidlaunchFrame()throwsException{//界面初始化this.setLocation(100,50);this.setSize(BOARD_WIDTH, BOARD_HEIGHT);this.setResizable(false);this.setBackground(Color.LIGHT_GRAY);

setVisible(true);//重画newThread(newPaintThread()).start();this.addMouseListener(newMouseMonitor());this.addWindowListener(newWindowAdapter(){publicvoidwindowClosing (WindowEvent e){

System.exit(0);

}

});

}publicstaticvoidmain(String[] args)throwsException {newBounceBall().launchFrame();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值