一.实验代码
//Plane.Java
package sxau_rjxy;
import java.awt.Color;
import java.awt.Graphics;
//敌机
public class Plane {
public Plane(int x, int y, int r) {
super();
this.x = x;
this.y = y;
this.r = r;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
private int x;
private int y;
private int r;
//自定义的paint()方法,名字随意
public void paint(Graphics g) {
// TODO Auto-generated method stub
Color c = g.getColor();
g.setColor(Color.black);//敌机黑色
g.fillOval(x-r, y-r, r^2,r^2);
g.setColor(c);
}
}
//Hero.java
package sxau_rjxy;
import java.awt.Color;
import java.awt.Graphics;
//我方飞机
public class Hero {
public Hero(int x, int y, int r) {
super();
this.x = x;
this.y = y;
this.r = r;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
private int x;
private int y;
private int r;
//自定义的paint()方法,名字随意
public void paint(Graphics g) {
// TODO Auto-generated method stub
Color c = g.getColor();
g.setColor(Color.blue);//我机蓝色
g.fillOval(x-r, y-r, r^2,r^2);
g.setColor(c);
}
}
//Bullet.java
package sxau_rjxy;
import java.awt.Color;
import java.awt.Graphics;
public class Bullet {
public Bullet(int x, int y, int r) {
super();
this.x = x;
this.y = y;
this.r = r;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
private int x;
private int y;
private int r;
//自定义的paint()方法,名字随意
public void paint(Graphics g) {
// TODO Auto-generated method stub
Color c = g.getColor();
g.setColor(Color.white);//子弹白色
g.fillOval(x-r, y-r, r^2,r^2);
g.setColor(c);
}
}
//MyJPanel
package sxau_rjxy;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JPanel;
public class MyJPanel extends JPanel implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
/*while(true){
int y = p.getY();
p.setY(y++);
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
int y;
int count =0;
while(true){
//使子弹慢下来产生
if(count%10==0){
Bullet bullet = new Bullet(hero.getX(),hero.getY()-hero.getR(),5);
bullets.add(bullet);
}
count++;
//让飞机动起来
for(int i= 0;i<planes.size();i++){
Plane p=planes.get(i);
y=p.getY()+1;
p.setY(y);
if(y>bgy){
p.setY(0);//开始循环了,但运行出来是由于横坐标没变,显示一样
p.setX((int)(Math.random()*(bgx-60)+30));//两个一起则图案显示不同,随机
}
}
//让子弹动起来
for(int i= 0;i<bullets.size();i++){
Bullet b=bullets.get(i);
y=b.getY()-2;
b.setY(y);
}
//bullet.setY(bullet.getY()-1);//让子弹动起来,即坐标变化
try {
Thread.sleep(20);//如果没sleep,那么它屏幕上没圈了,因为太快
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();//没它之前,运行出的结果是静态的,因为没有重新画它
}//这是一个死循环,while(true)
}
//Plane p = new Plane(200,200,50);
//Plane[] planes = new Plane[10];
ArrayList<Plane> planes = new ArrayList<Plane>();//保存飞机的引用,才能画它;定义了容器
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
//Plane hero= new Plane(bgx, bgy, 30);//不对,不能共用敌机的类
Hero hero;
Bullet bullet;
@Override
//重写的paint()方法,系统调用
public void paint(Graphics g) {
// TODO Auto-generated method stub
//super.paint(g);
//Color c = g.getColor();
//g.setColor(Color.red);
//g.fillOval(p.getX()-p.getR(), p.getY()-p.getR(), p.getR()^2,p.getR());
//g.setColor(c);
super.paint(g);
//p.paint(g);
/*画敌机
* for(int i =0;i<planes.size();i++){
Plane p= planes.get(i);
p.paint(g);
}
*/
//画敌机
Iterator<Plane> it = planes.iterator();
while(it.hasNext()){
Plane p = it.next();
p.paint(g);
}
//画子弹
for(int i =0;i<bullets.size();i++){
Bullet b=bullets.get(i);
b.paint(g);
}
//画hero
hero.paint(g);
//bullet.paint(g);
}
private int bgx;
private int bgy;
public MyJPanel (Dimension dim){
bgx =dim.width;
bgy =dim.height;
this.setBackground(Color.lightGray);
for(int i = 0;i<=10;i++){
Plane p= new Plane((int)(Math.random()*(bgx-60)+30),(int)(Math.random()*(bgy-60)+30),30);
planes.add(p);
}
hero= new Hero(bgx/2, bgy-60, 30);
/*
* Hero hero = new Hero(bgx/2, bgy-60, 30);
* 因为前面已经定义过Hero hero ;
* 所以运行出错,空白一片;
*/
bullet = new Bullet(hero.getX(),hero.getY()-hero.getR(),5);
this.addMouseMotionListener(new MyMonitor());
this.addKeyListener(new MyMonitor2());
this.setFocusable(true);//必须要这,监听键盘时必须加,不然没反应
new Thread(this).start();//线程启动第二种方法
}
//内部类
private class MyMonitor extends MouseMotionAdapter{
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
//super.mouseDragged(e);
hero.setX(e.getX());
hero.setY(e.getY());
}
}
private class MyMonitor2 extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
super.keyPressed(e);
//System.out.println("ss");//用来测试键盘事件是否响应了
int code = e.getKeyCode();
if(code==KeyEvent.VK_LEFT){
hero.setX(hero.getX()-10);
}
if(code==KeyEvent.VK_RIGHT){
hero.setX(hero.getX()+10);
}
if(code==KeyEvent.VK_UP){
hero.setY(hero.getY()-10);
}
if(code==KeyEvent.VK_DOWN){
hero.setY(hero.getY()+10);
}
}
}
}
//Mygame.java
package sxau_rjxy;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Mygame {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf = new JFrame("飞机大战");//创建窗体
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jf.setBounds(0, 0, dim.width, dim.height);
//jf.setBackground(Color.red);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyJPanel panel= new MyJPanel(dim);//相当于管家
jf.add(panel);
/*
* Thread t = new Thread(panel);
* t.start();
*/
jf.setResizable(false);
jf.setVisible(true);
}
}
二.实验结果
三.实验心得
06.28
1.在定义Hero类的时候,出现了错误.后经检查,发现了原因
Hero hero;
hero=new Hero(bgx/2, bgy-60, 30);
/* 原因解释
* Hero hero = new Hero(bgx/2,bgy-60, 30);
* 因为前面已经定义过Hero hero ;
* 所以运行出错,空白一片;
*/
2.int y;
while(true){
for(int i= 0;i<planes.size();i++){
Plane p=planes.get(i);
y=p.getY()+1;
p.setY(y);
if(y>bgy){
p.setY(0);//开始循环了,但运行出来是由于横坐标没变,显示一样
p.setX((int)(Math.random()*(bgx-60)+30));
//两个一起则图案显示不同,随机
}
}
try {
Thread.sleep(20);//如果没sleep,那么它屏幕上没圈了,因为太快
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();//没它之前,运行出的结果是静态的,因为没有重新画它
}//这是一个死循环,while(true)
3.启动线程的两种方式
3.1.Thread t = new Thread(panel);
t.start();
写在主方法中
3.2.new Thread(this).start();写在MyPlane的构造方法中.
4.创建监听事件,需要创建内部类.
建一个监听类MyMonitor从NAdaper继承过来
5.键盘事件,必须有setFocusable(true);
eg:
this.addKeyListener(new MyMonitor2());
this.setFocusable(true);//必须要这,监听键盘时必须加,不然没反应
6.Bullet类,目前设置的是hero可以发射子弹,因为Bullet的对象bullet用的 是bullet =new Bullet(hero.getX(),hero.getY()-hero.getR(),5);可以通过改变bullet的x,y,r来控制发射对象.
7.//使子弹慢下来产生,如果没有这部分,运行的效果会出现连续的子弹,看上去是一条连续的线
if(count%10==0){
Bullet bullet = new Bullet(hero.getX(),hero.getY()-hero.getR(),5);
bullets.add(bullet);
}
count++;