小城盱眙网
标题: 小时候经常玩的打地鼠游戏如何用java实现你造吗 [打印本页]
作者: ohnoma 时间: 2016-6-24 10:18
标题: 小时候经常玩的打地鼠游戏如何用java实现你造吗
《打地鼠游戏》是一款敏捷类游戏。我想大家小时候应该都玩过这个游戏吧,这是一款非常有趣的打地鼠游戏。游戏的规则很简单,把冒出头来的地鼠给全部打下去就算成功。那么你知道如何用java来实现这个游戏吗?下面甲骨文OAEC就来为大家讲解java课程的这个经典案例:利用Java实现打地鼠游戏。
代码:
public DaDiShu() {
addWindowListener(new WindowAdapter(){ //窗口监听事件;
public void windowActivated(WindowEvent e) { //实现出现地鼠;
do_this_windowActivated(e);}
public void windowClosing(WindowEvent e){ //实现统计打击的地鼠数目;
do_this_windowClosing(e);
}
});
/*
addMouseListener(new MouseAdapter() { //实现鼠标点击事件的监听;
public void mouseClicked(MouseEvent e){
do_this_mouseClicked(e);
}
});
*/
setTitle("\打\地\鼠\游\戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon icon1=new ImageIcon("src/images/background.jpg");
setBounds(100, 100,icon1.getIconWidth(),icon1.getIconHeight());
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.setOpaque(false);
setContentPane(contentPane);
JLabel backlabel = new JLabel("");
contentPane.add(backlabel, BorderLayout.CENTER);
//ImageIcon background = newImageIcon(DaDiShu.class.getResource("/images/background.jpg"));
backlabel.setBounds(0, 0, icon1.getIconWidth(),icon1.getIconHeight());
backlabel.setIcon(icon1);
getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE));
icon2=new ImageIcon("src/images/mouse.png");
label[0] = new JLabel(" ");
label[0].setBounds(236, 253,icon2.getIconWidth(),icon2.getIconHeight());
//label[0].setIcon(icon2);
contentPane.add(label[0]);
label[1] = new JLabel(" ");
label[1].setBounds(334, 242,icon2.getIconWidth(),icon2.getIconHeight());
contentPane.add(label[1]);
label[2] = new JLabel(" ");
label[2].setBounds(244, 293,icon2.getIconWidth(),icon2.getIconHeight());
contentPane.add(label[2]);
label[3] = new JLabel(" ");
label[3].setBounds(382, 289,icon2.getIconWidth(),icon2.getIconHeight());
contentPane.add(label[3]);
label[4] = new JLabel(" ");
label[4].setBounds(355, 356,icon2.getIconWidth(),icon2.getIconHeight());
contentPane.add(label[4]);
label[5] = new JLabel(" ");
label[5].setBounds(185, 347,icon2.getIconWidth(),icon2.getIconHeight());
contentPane.add(label[5]);
label[6] = new JLabel(" ");
label[6].setBounds(94, 320, icon2.getIconWidth(),icon2.getIconHeight());
//label[6].setIcon(icon2);
contentPane.add(label[6]);
for(int i = 0; i < 7; i++)
{
label.addMouseListener(new MouseAdapter() { //实现鼠标点击事件的监听;
public void mouseClicked(MouseEvent e){
do_this_mouseClicked(e);
}
});
}
}
protected voiddo_this_windowActivated(WindowEvent e){
Thread thread=new Thread(new DadishuDemo());
thread.start();
}
protected void do_this_windowClosing(WindowEvente){
JOptionPane.showMessageDialog(this, "You catch "+number+"mouses in sum!", "Congratulation!",JOptionPane.QUESTION_MESSAGE);
}
private class DadishuDemo implements Runnable{
public void run(){
while(true){
try {
Thread.sleep(1000);
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i=new Random().nextInt(7);
label.setIcon(icon2);
try {
Thread.sleep(1000);
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
label.setIcon(null);
}
}
}
protected void do_this_mouseClicked(MouseEvent e){
Object source=e.getSource();
JLabel mouse=(JLabel)source;
mouse.setIcon(null);
number++;
}
}
你学会了吗?如果还不是很明白建议观看我们java免费学习视频,可以到百度传课或者腾讯视频搜索海文IT教育观看哦。
更多精彩课程尽在上海海文信息技术有限公司,http://www.oracleoaec.net/
欢迎光临 小城盱眙网 (http://www.xcxy.net/) |
Powered by Discuz! X3.2 |