谁可以帮小弟把打鸟游戏的鸟打下来啊?3Q
我的图片的鸟怎么能加上适当的监听使鼠标可以打下来鸟啊,谢谢各位大虾啊!!!!!!!!!!!!!!!
[code="java"]
package org.zergle.test.swing.bird;
import java.awt.Rectangle;
import javax.swing.Icon;
import javax.swing.JLabel;
public class Bird extends JLabel {
private static final long serialVersionUID = -7163713521913130484L;
private boolean alive;
private int dir;
/**
* 创建一只鸟
*
* @param icon
* 图片
* @param dir
* 飞的方向,1为右,-1为左
*/
public Bird(Icon icon, int dir) {
this.setIcon(icon);
this.setOpaque(true);
this.alive = true;
this.dir = dir;
}
/**
* 鸟飞
*
* @param bounds
* 窗口边界
*/
public void fly(Rectangle bounds) {
if (this.isAlive()) {
int x = this.getX();
int y = this.getY();
if (x >= bounds.width || x <= 0) {
x = dir > 0 ? 0 : bounds.width - this.getWidth();
}
this.setBounds(x + dir * 2, y, this.getWidth(), this.getHeight());
}
}
/**
* 是否活着
*
* @return
*/
public boolean isAlive() {
return alive;
}
/**
* 鸟死掉
*/
public void die() {
this.alive = false;
}
}
[/code]
[code="java"]
package org.zergle.test.swing.bird;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class Game extends JFrame {
private static final long serialVersionUID = -2286295699597291897L;
private List<Bird> birds = new Vector<Bird>();
public Game() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.init();
this.setLayout(null);
this.setTitle("Hunt Bird");
this.setSize(400, 280);
this.setResizable(false);
this.setVisible(true);
/* 守护线程更新界面 */
Thread t = new Thread(new Runnable() {
public void run() {
while (!birds.isEmpty()) {
for (Iterator<Bird> it = birds.iterator(); it.hasNext();) {
Bird b = it.next();
if (b.isAlive()) {
b.fly(getBounds());
} else {
remove(b);
it.remove();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
repaint();
}
JOptionPane.showMessageDialog(null, "Game Over");
System.exit(0);
}
});
t.setDaemon(true);
t.start();
}
/**
* 初始化鸟及其位置
*/
public void init() {
ImageIcon ico0 = new ImageIcon(this.getClass().getResource("0.gif"));
ImageIcon ico1 = new ImageIcon(this.getClass().getResource("1.gif"));
ImageIcon ico2 = new ImageIcon(this.getClass().getResource("2.gif"));
ImageIcon ico3 = new ImageIcon(this.getClass().getResource("3.gif"));
Hunter hunter = new Hunter();
Bird b0 = new Bird(ico0, -1);
b0.addMouseListener(hunter);
b0.setBounds(350, 10, ico0.getIconWidth(), ico0.getIconHeight());
Bird b1 = new Bird(ico1, 1);
b1.addMouseListener(hunter);
b1.setBounds(0, 100, ico1.getIconWidth(), ico1.getIconHeight());
Bird b2 = new Bird(ico2, -1);
b2.addMouseListener(hunter);
b2.setBounds(350, 150, ico2.getIconWidth(), ico2.getIconHeight());
Bird b3 = new Bird(ico3, 1);
b3.addMouseListener(hunter);
b3.setBounds(0, 200, ico3.getIconWidth(), ico3.getIconHeight());
this.add(b0);
this.add(b1);
this.add(b2);
this.add(b3);
}
public Component add(Component comp) {
if (comp instanceof Bird) {
this.birds.add((Bird) comp);
}
return super.add(comp);
}
/**
* 猎鸟人
*
* @author Johnson Lee
*
*/
private class Hunter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
Object target = e.getSource();
if (target instanceof Bird) {
Bird bird = (Bird) target;
bird.die();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Game();
}
});
}
}
[/code]
可以试试将,图片添加到JButton上面,再添加到JPanel上面,然后就可以加事件了
有意思,我先看看。
楼上的代码写的很好