import java.awt.Graphics;
import java.util.ArrayList; import javax.swing.JFrame;
import javax.swing.JPanel; public class PlaneMain extends JPanel { public static void main(String[] args) {
new PlaneMain();
} private ArrayList<View> list; public PlaneMain() {
list = new ArrayList<View>();
View background = new View("background.jpg", 0, -60, 700, 460, 2, this);
list.add(background);
initUI();
} private void initUI() {
JFrame frame = new JFrame("飞机大战");
frame.setSize(700, 400);
frame.setDefaultCloseOperation(3);
frame.setLocationRelativeTo(null);
frame.setResizable(false); frame.add(this); frame.setVisible(true); AddListener al = new AddListener(this, list); this.addMouseListener(al); Thread t = new Thread(al);
t.start();// 启动线程
} /**
* 重写JPanel的重绘方法
*/
public void paint(Graphics g) {
super.paint(g); for (int i = 0; i < list.size(); i++) {
View v = list.get(i);
g.drawImage(v.getBackground(), v.getX(), v.getY(), v.getWidth(),
v.getHeight(), this);
}
} }
import java.awt.Image;
import java.util.ArrayList; import javax.swing.ImageIcon;
import javax.swing.JPanel; public class View { private Image background;
private int x = 0, y = -60, moveY, width, height;
private JPanel panel;
private String imageName;
private boolean flag;
/**
* 构造方法
*
* @param background背景图片的对象
* @param x起始X坐标
* @param y起始Y坐标
*/
public View(String imageName, int x, int y, int width, int height,
int moveY, JPanel panel) {
this.imageName = imageName;
this.background = new ImageIcon(this.getClass().getResource(imageName))
.getImage();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.moveY = moveY;
this.panel = panel;
}
/**
* 构造方法
*
* @param background背景图片的对象
* @param x起始X坐标
* @param y起始Y坐标
*/
public View(String imageName, int x, int y, int width, int height,
int moveY, JPanel panel, boolean flag) {
this.imageName = imageName;
this.background = new ImageIcon(this.getClass().getResource(imageName))
.getImage();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.moveY = moveY;
this.panel = panel;
this.flag = flag;
} public int getWidth() {
return width;
} public void setWidth(int width) {
this.width = width;
} public int getHeight() {
return height;
} public void setHeight(int height) {
this.height = height;
} public Image getBackground() {
return background;
} public void setBackground(Image background) {
this.background = background;
} 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 getMoveY() {
return moveY;
} public void setMoveY(int moveY) {
this.moveY = moveY;
} public JPanel getPanel() {
return panel;
} public void setPanel(JPanel panel) {
this.panel = panel;
} public void move(ArrayList<View> list) {
if (imageName.equals("background.jpg")) {
y += moveY;
if (y == 0)
y = -60;
} else if (imageName.equals("bullet.png")) {
if (flag) {//地方的子弹
y += moveY;
if (y >= 400)
y = 0;
}else {
y -= moveY;
if (y <= 0)
list.remove(this);
}
} else if (imageName.equals("plane.jpg")) {
y -= moveY;
if (y <= 0)
y = 400;
}
} /**
* 碰撞方法
*/
public void collisions(ArrayList<View> list) {
for (int i = 1; i < list.size(); i++) {
View v = list.get(i);
if (this != v) {
double distance = Math.sqrt((this.x - v.x) * (this.x - v.x)
+ Math.pow(this.y - v.y, 2));
if (distance <= this.height + v.height) {
System.out.println(v.imageName + "和" + this.imageName
+ "发生了碰撞");
}
}
}
} }
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Timer; import javax.swing.JPanel; public class AddListener extends MouseAdapter implements Runnable { private int count = 0; private JPanel panel;
private ArrayList<View> list; public AddListener(JPanel panel, ArrayList<View> list) {
this.panel = panel;
this.list = list;
} public void mouseReleased(MouseEvent e) {
if (count == 0) {
View plane = new View("plane.jpg", e.getX(), e.getY(), 50, 50, 3,
panel, false);
list.add(plane); //实例化定时任务类的对象(任务对象)
BulletAI task = new BulletAI(panel, list, plane);
//创建时间类的对象
Timer timer = new Timer();
//启动任务,第一次执行时在2秒之后,之后每一次执行都间隔2秒
timer.schedule(task, 2000, 2000); //timer.cancel();//取消时间对象中所有定时任务 //bai.start();
count++;
} else {
View bullet = new View("bullet.png", e.getX(), e.getY(), 10, 20, 5,
panel, true);
list.add(bullet);
}
} public void run() {
while (true) {
for (int i = 0; i < list.size(); i++) {
View v = list.get(i);
v.move(list);
if (i != 0)
v.collisions(list);
} panel.repaint(); try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} }
} }
import java.util.ArrayList;
import java.util.TimerTask; import javax.swing.JPanel; public class BulletAI extends TimerTask { // Thread { private JPanel panel;// 绘制飞机和子弹的面板对象
private ArrayList<View> list;// 存储飞机和子弹的数组队列
private View plane;// 发生子弹的飞机对象 public BulletAI(JPanel panel, ArrayList<View> list, View plane) {
this.panel = panel;
this.list = list;
this.plane = plane;
} public void run() {
//
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
//
// while (true) {
View v = new View("bullet.png", plane.getX(), plane.getY(), 10, 20, 5,
panel, false);// 最后一个参数false表示的是我方飞机发射的子弹 list.add(v); // try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
//
// }
} }

1.如何自动生成子弹
使用线程来控制子弹的生成。
BulletAI.java

2.Java的定时任务
TimerTask implements Runnable
Timer
启动定时任务

java飞机大战之子弹的自动生成的更多相关文章

  1. Java飞机大战MVC版

    PlaneWar Java飞机大战MVC版 //无聊时偷的雷霆战机素材写了一个飞机大战,本意是练习mvc,但写得还是不清晰 github下载:https://github.com/dejavudwh/ ...

  2. java如何在eclipse编译时自动生成代码

    用eclipse写java代码,自动编译时,如何能够触发一个动作,这个动作是生成本项目的代码,并且编译完成后,自动生成的代码也编译好了, java编辑器中就可以做到对新生成的代码的自动提示? 不生成代 ...

  3. Java飞机大战源代码

    刚学不久java,做了一个飞机大战的小小小小游戏,现在把这个思路总结以及代码分享出来.大佬别吐槽(emmmmmm .....开发环境:jdk1.7 开发工具:eclipese PlanelJPanel ...

  4. JAVA入门[7]-Mybatis generator(MBG)自动生成mybatis代码

    一.新建测试项目 新建Maven项目MybatisDemo2,修改pom.xml引入依赖.dependencies在上节基础上新增 <dependency> <groupId> ...

  5. Java工具类_表结构自动生成对应的实体类、Mapper.xml文件、Dao类

    import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWrit ...

  6. 【Java MyBatis Generator】使用generator自动生成Dao,Mapping和实体文件

    具体请参照: http://blog.csdn.net/fengshizty/article/details/43086833 按照上面博客地址,下载Generator的依赖包: 如下是我的配置文件: ...

  7. Html飞机大战(八):子弹的移动和管理

    好家伙,这应该是这个小游戏最难的几个点之一了 现在我们要做出子弹射击的效果我们应该如何处理? 1.首先我们要确定几个变量和方法的关系 变量: 子弹  bullet  弹夹(用来装子弹的东西)bulle ...

  8. Mybatis上路_05-使用命令行自动生成

    http://my.oschina.net/vigiles/blog/125127 目录[-] 1.数据准备: 1)建库: 2)建表: 3)预设数据: 2.编写Generator执行配置文件: 3.搭 ...

  9. Mybatis上路_05-使用命令行自动生成【转】

    http://my.oschina.net/vigiles/blog/125127 Mybatis上路_05-使用命令行自动生成   1人收藏此文章, 我要收藏 发表于1个月前(2013-04-24 ...

随机推荐

  1. spring: 创建环绕通知

    package ch2.test; public interface Performance { void perform(); } package ch2.test; import org.aspe ...

  2. 绑定当前对象例子——Tag="{Binding}"

    <TreeView Margin="10,5,0,0" HorizontalAlignment="Left"  VerticalAlignment=&qu ...

  3. 51nod 1040 欧拉函数

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1040 1040 最大公约数之和 题目来源: rihkddd 基准时间限制 ...

  4. Linux中的固件加载例子

    AP6335模块(BCM4339)在上电运行时,是需要刷入固件的,其在普通WIFI模式和AP模式之间切换时,也是需要加载不同的固件的,其位于/system/etc/firmware/下面:fw_bcm ...

  5. s3cmd 安装使用指南

    https://wangyan.org/blog/s3cmd-how-to-use.html s3cmd 安装使用指南 s3cmd 是一款 Amazon S3 命令行工具.它不仅能上传.下载.同步,还 ...

  6. linux 日常使用命令

    ●安装和登录命令:login.shutdown.halt.reboot.mount.umount.chsh ●文件处理命令:file.mkdir.grep.dd.find.mv.ls.diff.cat ...

  7. LeetCode OJ:Sort Colors(排序颜色)

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. yum search/intall, Error: xz compression not available

    转自:http://blog.hexu.org/archives/2060.shtml 遇到这个问题情景: 下午第一台系统是Centos7, 安装配置完成后,接着一台是Centos 6 系统,由于疏忽 ...

  9. C#托管代码、非托管代码及回收机制

    网上找了下相关文字,发现一些很不错的,转过来,方便以后查看 托管代码 托管代码就是Visual Basic .NET和C#编译器编译出来的代码.编译器把代码编译成中间语言(IL),而不是能直接在你的电 ...

  10. python的编解码问题

    http://blog.chinaunix.net/uid-27838438-id-4227131.html