java小游戏第二弹 打地鼠

/*打地鼠
 * 本版只是单纯的图形接界面运用
 * 图片编程要用到的API----应用程序接口
 * 1, JLabel: JLabel(Icon image)
 * 2, Icon接口---可以封装固定大小的小图片,通常用于装饰界面组件
 * 3, ImageIcon----是Icon接口的实现类,当然可以当作Icon来用。
 *     ImageIcon(String filename)---说明该对象可以通过文件名来生成
   4, Toolkit---抽象类,我们不能new对象,因此通过它的工厂方法获取---getDefaultTookit
      public abstract Image createImage(String filename)---可以通过文件名创建一个图片
      public Cursor createCustomCursor(Image cursor,Point hotSpot,String name)---创建自定义光标
   5,Window类(JFrame的父类)
      public void setCursor(Cursor cursor)-----设置光标
   6, JFrame类中的方法
       public JLayeredPane getLayeredPane() ----返回窗体的 layeredPane对象----窗体分层显示
   
 *

 */

java小游戏第二弹 打地鼠_第1张图片

package cn.hncu.games;

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.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;


public class HitMouse extends JFrame{
	//图片路径
	private final String dir="./images_HitMouse/";
	private JLabel jlbMouse=new JLabel();
	
	private boolean isHit;
	
	private Timer timer;
	private int delay;
	
	private JLabel[] jlb;
	private int[] number;
	
	private JMenu[] jmenu;
	private JMenuItem[][] jmenuItem;
	/*
	 * 菜单处理
	 */
	private ActionListener jmenuListener=new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("newGame")){
				delay=1000;
				newGame();
				jlb[2].setText("0");
				jmenuItem[0][2].setEnabled(false);
				jmenuItem[0][1].setEnabled(true);
			}else if(e.getActionCommand().equals("break")){
				timer.stop();
				jlbMouse.removeMouseListener(jlbmouseListener);
				jmenuItem[0][1].setEnabled(false);
				jmenuItem[0][2].setEnabled(true);
			}else if(e.getActionCommand().equals("continue")){
				timer.restart();
				jlbMouse.addMouseListener(jlbmouseListener);
				jmenuItem[0][2].setEnabled(false);
				jmenuItem[0][1].setEnabled(true);
			}else if(e.getActionCommand().equals("exit")){
				if (timer!=null&&timer.isRunning()) {
					timer.stop();
				}
				//System.exit(0);
				dispose();
			}
			
		}
	};
	/*
	 * 
	 * 地鼠位置改变
	 */
	private ActionListener hitListener=new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			jlbMouse.setVisible(false);
			gameMessage(1);
			switch((new Random()).nextInt(9)){
			case 0:
				jlbMouse.setLocation(66,45);break;
			case 1:
				jlbMouse.setLocation(195,45);break;
			case 2:
				jlbMouse.setLocation(324,45);break;
			case 3:
				jlbMouse.setLocation(33,115);break;
			case 4:
				jlbMouse.setLocation(170,115);break;
			case 5:
				jlbMouse.setLocation(307,115);break;
			case 6:
				jlbMouse.setLocation(58,187);break;
			case 7:
				jlbMouse.setLocation(195,187);break;
			case 8:
				jlbMouse.setLocation(332,187);break;
			}
			setMouse(0);
			isHit=true;
			jlb[0].setText(Integer.parseInt(jlb[0].getText())+1+"");
			jlbMouse.setVisible(true);
		}
	};
	/*
	 * 地鼠监听鼠标进行击中后变化
	 */
	private MouseListener jlbmouseListener=new MouseListener() {
		@Override
		public void mouseReleased(MouseEvent e) {
			mySetCursor(0);
		}
		@Override
		public void mousePressed(MouseEvent e) {
			mySetCursor(1);
			if(!isHit)
				return;
			setMouse(1);
			Toolkit.getDefaultToolkit().beep();
			jlb[1].setText(Integer.parseInt(jlb[1].getText())+1+"");
			isHit=false;
		}
		
		@Override
		public void mouseExited(MouseEvent e) {
		}
		@Override
		public void mouseEntered(MouseEvent e) {
		}
		@Override
		public void mouseClicked(MouseEvent e) {
		}
	};

	public HitMouse() {
		super("简易版打地鼠");
		setBounds(260, 80, 445, 385);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		//菜单
		JMenuBar bar=new JMenuBar();
		
		String[] str={"游戏"};
		String[][] strs={{"新游戏","暂停","继续","退出"}};
		String[][] strs_english={{"newGame","break","continue","exit"}};
		jmenu=new JMenu[str.length];
		jmenuItem=new JMenuItem[strs.length][];
		for (int i = 0; i < jmenu.length; i++) {
			jmenu[i]=new JMenu(str[i]);
			jmenuItem[i]=new JMenuItem[strs[i].length];
			for (int j = 0; j < jmenuItem[i].length; j++) {
				jmenuItem[i][j]=new JMenuItem(strs[i][j]);
				jmenuItem[i][j].addActionListener(jmenuListener);
				jmenuItem[i][j].setActionCommand(strs_english[i][j]);
				if(jmenuItem[i][j].getActionCommand().equals("continue"))
					jmenuItem[i][j].setEnabled(false);
				if(jmenuItem[i][j].getActionCommand().equals("break"))
					jmenuItem[i][j].setEnabled(false);
				jmenu[i].add(jmenuItem[i][j]);
			}
			bar.add(jmenu[i]);
		}
		setJMenuBar(bar);
		/*
		 * 对光标设置
		 */
		addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
				mySetCursor(0);
			}
			@Override
			public void mousePressed(MouseEvent e) {
				mySetCursor(1);
			}
			@Override
			public void mouseExited(MouseEvent e) {
			}
			@Override
			public void mouseEntered(MouseEvent e) {
			}
			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
		getContentPane().setLayout(null);
		
		setBack();
		
		mySetCursor(0);
		
		isHit=false;
		jlbMouse.setLocation(170,115);
		setMouse(0);
		jlbMouse.addMouseListener(jlbmouseListener);
		getContentPane().add(jlbMouse);
		
		JPanel p=new JPanel();
		p.setOpaque(false);
		p.setBounds(0, 0, 445, 60);
		getContentPane().add(p);
		
		jlb=new JLabel[3];
		String[] str_lb={"chuxiancishu.png","dazhongcishu.png","dangqiandengji.png"};
		for (int i = 0; i < jlb.length; i++) {
			jlb[i]=new JLabel("0", new ImageIcon(dir+str_lb[i]), SwingConstants.CENTER);
			jlb[i].setFont(new Font("仿宋",Font.BOLD+Font.ITALIC,20));
			p.add(jlb[i]);
		}
		setVisible(true);
	}
	/*
	 * 新游戏初始化
	 */
	private void newGame() {
		jlb[2].setText(Integer.parseInt(jlb[2].getText())+1+"");
		jlb[0].setText("0");
		jlb[1].setText("0");
		
		jlbMouse.setVisible(false);
		
		timer=new Timer(delay, hitListener);
		timer.start();
	}
	/*
	 * 游戏结束或晋级时的提示
	 */
	private void gameMessage(int number) {
		int a=Integer.parseInt(jlb[0].getText());
		int b=Integer.parseInt(jlb[1].getText());
		if(a+number>9+number){
			timer.stop();
			if(b>7){
				int option=JOptionPane.showConfirmDialog(null, "击中率过80%,是否进入下一关?");
				if(option==JOptionPane.OK_OPTION){
					if(delay==0){
						JOptionPane.showMessageDialog(null, "恭喜通关?");
					}
					jlb[0].setText("0");
					jlb[1].setText("0");
					delay=delay-100;
					newGame();
				}else if(option==JOptionPane.NO_OPTION){
					//System.exit(0);
					dispose();
				}else{
				}
			}else{
				int option=JOptionPane.showConfirmDialog(null, "击中率不足80%,是否从新开始?", "失败",JOptionPane.YES_NO_OPTION);
				if(option==JOptionPane.OK_OPTION){
					jlb[0].setText("0");
					jlb[1].setText("0");
					delay=1000;
					newGame();
				}else{
					//System.exit(0);
					dispose();
				}
			}
		}
	}
	/*
	 * 对地鼠的设置
	 */
    private Icon mouseImg;
	private void setMouse(int mouseType) {
		switch(mouseType){
		case 0:
			mouseImg=new ImageIcon(dir+"dishu.png");
			break;
		case 1:
			mouseImg=new ImageIcon(dir+"datou.png");
		}
		jlbMouse.setIcon(mouseImg);
		jlbMouse.setSize(mouseImg.getIconWidth(), mouseImg.getIconHeight());
		if(mouseType==1){
			jlbMouse.setLocation(jlbMouse.getX()-16, jlbMouse.getY()-2);
		}
	}

	/*
	 * 光标设置
	 */
	private void mySetCursor(int cursorType) {
		Image myCursorImg=null;
		Toolkit toolkit=Toolkit.getDefaultToolkit();
		switch(cursorType){
		case 0:myCursorImg=toolkit.createImage(dir+"chui1.png");break;
		case 1:myCursorImg=toolkit.createImage(dir+"chui2.png");break;
		}
		getContentPane().setCursor(toolkit.createCustomCursor(myCursorImg, new Point(10, 10), "chui"));
		
	}
/*
 * 背景设置
 */
	private void setBack() {
		((JPanel)getContentPane()).setOpaque(false);
		Icon backImg=new ImageIcon(dir+"beijing.jpg");
		JLabel backJlable=new JLabel(backImg);
		getLayeredPane().add(backJlable,new Integer(Integer.MIN_VALUE));
		backJlable.setBounds(0, 0, backImg.getIconWidth(), backImg.getIconHeight());
	}

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

	}

}

你可能感兴趣的:(java简易小游戏)