码迷,mamicode.com
首页 > 编程语言 > 详细

打地鼠 java

时间:2015-06-15 18:41:39      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:java   图片处理   小程序   打地鼠   

技术分享


//简单的图片处理技术

所有图片连接:http://yunpan.cn/cQyWXfIhIUDqX (提取码:2db4)

import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class moleAttack extends JFrame implements ActionListener, MouseListener {
	private String strDir = "./image/moleAttackImage/"; // 图片所在的路径
	JLabel jlbMole, HitNumber, appearNumber, gradeNumber;// 图片标签
	Timer timer; // 定时器
	Random random; // 随机数
	int delay = 1000;
	int hit = 0, apper = 0, grade = 0; // 分别是打中次数、出现次数、等级

	public moleAttack() {
		this.setTitle("打地鼠");
		setBack(); // 设置背景
		setGrade(); // 设置打中次数、出现次数、等级
		this.getContentPane().setLayout(null); // 因为要把图片放在自己定的位置,所以布局得取消掉

		// 设置光标
		Toolkit tk = Toolkit.getDefaultToolkit();
		Image image = tk.createImage(strDir + "hammer.png");
		Cursor myCursor = tk.createCustomCursor(image, new Point(10, 10), "xh");
		this.setCursor(myCursor);

		// 设置出现的地鼠
		ImageIcon imageMole = new ImageIcon(strDir + "mole.png");
		jlbMole = new JLabel(imageMole);
		jlbMole.setSize(80, 80);
		this.getContentPane().add(jlbMole);
		jlbMole.setVisible(false);
		this.getContentPane().addMouseListener(this);//对面板进行监听
		jlbMole.addMouseListener(this);// 监听地鼠是否被打到

		// 定时器
		timer = new Timer(delay, this);
		timer.start();

		random = new Random();

		this.setLocation(200, 200);
		this.setSize(438, 375);
		this.setResizable(false);
		this.setVisible(true);
	}

	// 设置背景
	private void setBack() {
		((JPanel) (this.getContentPane())).setOpaque(false);// 使getContentPane()透明
		ImageIcon bgImage = new ImageIcon(strDir + "backGround.jpg");
		JLabel bgLabel = new JLabel(bgImage);
		bgLabel.setBounds(0, 0, bgImage.getIconWidth(), bgImage.getIconHeight());
		this.getLayeredPane().add(bgLabel, new Integer(Integer.MIN_VALUE));// 这里需要把背景图片的优先级调到最低,才能使别的图片在其上面显示

	}

	// 设置打中次数、出现次数、等级
	private void setGrade() {
		// 设置出现次数
		ImageIcon imageNumber = new ImageIcon(strDir + "chuxiancishu.png");
		JLabel Number = new JLabel(imageNumber);
		Number.setSize(200, 50);
		Number.setLocation(-30, 10);
		this.getContentPane().add(Number);
		// 显示数据
		appearNumber = new JLabel();
		appearNumber.setText("" + apper);
		appearNumber.setLocation(122, 10);
		appearNumber.setFont(new Font("宋体", Font.BOLD, 19));
		appearNumber.setSize(50, 50);
		this.getContentPane().add(appearNumber);

		// 设置击中次数
		ImageIcon imageHitTime = new ImageIcon(strDir + "dazhongcishu.png");
		JLabel HitTime = new JLabel(imageHitTime);
		HitTime.setSize(200, 50);
		HitTime.setLocation(105, 10);
		this.getContentPane().add(HitTime);

		// 显示数据
		HitNumber = new JLabel();
		HitNumber.setText("" + hit);
		HitNumber.setLocation(260, 10);
		HitNumber.setFont(new Font("宋体", Font.BOLD, 19));
		HitNumber.setSize(50, 50);
		this.getContentPane().add(HitNumber);

		// 实置当前等级
		ImageIcon imageGrade = new ImageIcon(strDir + "dangqiandengji.png");
		JLabel Grade = new JLabel(imageGrade);
		Grade.setSize(200, 50);
		Grade.setLocation(245, 10);
		this.getContentPane().add(Grade);

		// 显示数据
		gradeNumber = new JLabel();
		gradeNumber.setText("" + grade);
		gradeNumber.setLocation(402, 10);
		gradeNumber.setFont(new Font("宋体", Font.BOLD, 19));
		gradeNumber.setSize(50, 50);
		this.getContentPane().add(gradeNumber);
	}

	public static void main(String[] args) {
		new moleAttack();
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		appearNumber.setText("" + (++apper));//每出现一次地鼠,出现次数加一

		//每出现十次地鼠,等级加一(出现频率变快)
		if (apper % 10 == 0) {
			gradeNumber.setText("" + (++grade));
			timer.setDelay((int) (delay * 0.9));
		}
		
		//防止大众后不变会原来的样子
		ImageIcon imagetIconHit = new ImageIcon(strDir + "mole.png");
		jlbMole.setIcon(imagetIconHit);
		jlbMole.setVisible(true);
		
		//随机出现地鼠的位置
		int r = random.nextInt(9);
		switch (r + 1) {
		case 1:
			jlbMole.setLocation(54, 63);
			break;
		case 2:
			jlbMole.setLocation(320, 204);
			break;
		case 3:
			jlbMole.setLocation(180, 204);
			break;
		case 4:
			jlbMole.setLocation(45, 203);
			break;
		case 5:
			jlbMole.setLocation(295, 133);
			break;
		case 6:
			jlbMole.setLocation(160, 133);
			break;
		case 7:
			jlbMole.setLocation(20, 133);
			break;
		case 8:
			jlbMole.setLocation(310, 63);
			break;
		case 9:
			jlbMole.setLocation(183, 63);
			break;
		}
		jlbMole.setVisible(true);

	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		//光标点击地鼠后变成打击的样子
		Toolkit tk = Toolkit.getDefaultToolkit();
		Image image = tk.createImage(strDir + "hit.png");
		Cursor myCursor = tk
				.createCustomCursor(image, new Point(10, 10), "yyy");
		this.setCursor(myCursor);
		
		//判断是否击中
		if (e.getSource() == jlbMole) {
			//改变地鼠的样子
			ImageIcon imagetIconHit = new ImageIcon(strDir + "touch.png");
			jlbMole.setIcon(imagetIconHit);
			jlbMole.setVisible(true);

			HitNumber.setText("" + (++hit));//击中后击中次数加一
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		//光标松开后,回复原来的样子
		Toolkit tk = Toolkit.getDefaultToolkit();
		Image image = tk.createImage(strDir + "hammer.png");
		Cursor myCursor = tk
				.createCustomCursor(image, new Point(10, 10), "yyy");
		this.setCursor(myCursor);

	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

}



打地鼠 java

标签:java   图片处理   小程序   打地鼠   

原文地址:http://blog.csdn.net/xionghui2013/article/details/46506213

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!