⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 marmot.java

📁 小游戏打地鼠 使用到ava的事件监听机制、Java的多线程机制
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class BtnMouse extends JButton{
	public static final int green = 0;
	public static final int grey = 1;
	public static final int red = 2;
	long time;
	int color;
}

public class Marmot extends JFrame implements ActionListener{
	int size = 7;
	int square = size * size;
	int count = 0;
	public int liveStayTime;
	public int deadStayTime;
	int groundTotal = 0;
	int maxGroundTotal = 0;
	public long beginTime;
	int elastTime;
	Daemon daemon;
	Container con = this.getContentPane();
	BtnMouse[] aButton = new BtnMouse[square];
	JComboBox comboLiveStayTime = new JComboBox();
	JComboBox comboDeadStayTime = new JComboBox();
	JRadioButton rbutton1 = new JRadioButton("1只");
	JRadioButton rbutton2 = new JRadioButton("2只");
	JRadioButton rbutton3 = new JRadioButton("3只");
	JComboBox comboGameTime = new JComboBox();
	JProgressBar progBar = new JProgressBar();
	JButton buttonStart = new JButton("开始");
	JButton buttonTerminate = new JButton("终止");
	JTextField fieldAmount = new JTextField();
	Marmot(){
		con.setLayout(new BorderLayout(0, 35));
		JPanel panelControl = new JPanel();
		JPanel panelRectangle = new JPanel();
		con.add(BorderLayout.NORTH, panelControl);
		con.add(BorderLayout.CENTER, panelRectangle);
		
		panelControl.setLayout(new GridLayout(4, 1, 0, 10));
		JPanel panelStayTime = new JPanel();
		JPanel panelMiceAmount = new JPanel();
		JPanel panelGameTime = new JPanel();
		JPanel panelOperate = new JPanel();
		panelControl.add(panelStayTime);
		panelControl.add(panelMiceAmount);
		panelControl.add(panelGameTime);
		panelControl.add(panelOperate);
		
		panelStayTime.setLayout(new GridLayout(1, 4, 10, 0));
		panelStayTime.add(new JLabel("活鼠地面滞留时间(毫秒):"));
		panelStayTime.add(comboLiveStayTime);
		panelStayTime.add(new JLabel("死鼠地面滞留时间(毫秒):"));
		panelStayTime.add(comboDeadStayTime);
		
		panelMiceAmount.setLayout(new GridLayout(1, 2, 15, 0));
		panelMiceAmount.add(new JLabel("同一时间地面上土鼠的最大数量:"));
		JPanel panelMice123 = new JPanel();
		panelMiceAmount.add(panelMice123);
		
		panelMice123.setLayout(new GridLayout(1, 3));
		panelMice123.add(rbutton1);
		panelMice123.add(rbutton2);
		panelMice123.add(rbutton3);
		
		panelGameTime.setLayout(new BorderLayout(15, 0));
		JPanel panelGameTimePrompt = new JPanel();
		panelGameTime.add(BorderLayout.WEST, panelGameTimePrompt);
		panelGameTime.add(BorderLayout.CENTER, progBar);
		
		panelGameTimePrompt.setLayout(new GridLayout(1, 2));
		panelGameTimePrompt.add(new JLabel("游戏持续时间(秒):"));
		panelGameTimePrompt.add(comboGameTime);
		
		panelOperate.setLayout(new GridLayout(1, 4, 25, 0));
		panelOperate.add(buttonStart);
		panelOperate.add(buttonTerminate);
		panelOperate.add(new JLabel("击中土鼠总数:"));
		panelOperate.add(fieldAmount);
		fieldAmount.setEnabled(false);
		
		buttonStart.addActionListener(this);
		buttonTerminate.addActionListener(this);
		for (int i=0; i<square; i++)
			aButton[i] = new BtnMouse();
		panelRectangle.setLayout(new GridLayout(size, size, 15, 15));
		for (int i=0; i<square; i++){
			panelRectangle.add(aButton[i]);
			aButton[i].addActionListener(this);
			aButton[i].setBackground(Color.GREEN);
			aButton[i].color = BtnMouse.green;
		}
				
		for (int i=0; i<30; i++)
			comboLiveStayTime.addItem(new Integer(100 + 100*i));
		comboLiveStayTime.setSelectedIndex(4);
		for (int i=0; i<30; i++)
			comboDeadStayTime.addItem(new Integer(100 + 100*i));
		comboDeadStayTime.setSelectedIndex(4);
		ButtonGroup group = new ButtonGroup();
		group.add(rbutton1);
		group.add(rbutton2);
		group.add(rbutton3);
		rbutton2.setSelected(true);
		for (int i=0; i<30; i++)
			comboGameTime.addItem(new Integer(5 + 5*i));
		for (int i=0; i<square; i++){
			aButton[i].color = BtnMouse.green;
			aButton[i].setBackground(Color.GREEN);
		}
		fieldAmount.setText("0");
		buttonTerminate.setEnabled(false);
		
		this.setTitle("小游戏“打地鼠”--北京林业大学信息学院软件教研室王建新2008年12月制作");
		this.setBounds(30, 30, 850, 700);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(false);
	}
	public void actionPerformed(ActionEvent ae){
		Object obj = ae.getSource();
		long now = System.currentTimeMillis();
		if (obj == buttonStart){
			liveStayTime = ((Integer)(comboLiveStayTime.getSelectedItem())).intValue();
			deadStayTime = ((Integer)(comboDeadStayTime.getSelectedItem())).intValue();
			count = 0;
			if (rbutton1.isSelected())
				maxGroundTotal = 1;
			else if (rbutton2.isSelected())
				maxGroundTotal = 2;
			else
				maxGroundTotal = 3;
			elastTime = 1000 * ((Integer)(this.comboGameTime.getSelectedItem())).intValue();
			progBar.setValue(0);
			beginTime = now;
			comboLiveStayTime.setEnabled(false);
			comboDeadStayTime.setEnabled(false);
			rbutton1.setEnabled(false);
			rbutton2.setEnabled(false);
			rbutton3.setEnabled(false);
			comboGameTime.setEnabled(false);
			
			buttonStart.setEnabled(false);
			buttonTerminate.setEnabled(true);
			fieldAmount.setText("0");
			for (int i=0; i<square; i++){
				aButton[i].setBackground(Color.GREEN);
				aButton[i].color = BtnMouse.green;
			}
			groundTotal = 0;
			daemon = new Daemon();
			daemon.start();
		}
		if (obj == buttonTerminate){
			daemon.done = true;
			terminate();
		}
		for (int i=0; i<square; i++){
			if (daemon.done)
				return;
			if (obj == aButton[i]){
				if (aButton[i].color == BtnMouse.grey){
					aButton[i].color = BtnMouse.red;
					aButton[i].setBackground(Color.RED);
					aButton[i].time = now;
					count++;
					String show = String.valueOf(count);
					fieldAmount.setText(show);
				}
			}
		}
	}
	void terminate(){
		comboLiveStayTime.setEnabled(true);
		comboDeadStayTime.setEnabled(true);
		rbutton1.setEnabled(true);
		rbutton2.setEnabled(true);
		rbutton3.setEnabled(true);
		comboGameTime.setEnabled(true);
		buttonStart.setEnabled(true);
		buttonTerminate.setEnabled(false);
	}
	class Daemon extends Thread{
		boolean done = false;
		public void run(){
			while (!done){
				long now = System.currentTimeMillis();
				int span = (int)(now - beginTime);
				int percent = (int)((double)span / elastTime * 100);
				progBar.setValue(percent);
				if (beginTime + elastTime < now){
					done = true;
					terminate();
					return;
				}
				try{
				Thread.sleep(50);
				}catch(InterruptedException ie){}
				for (int i=0; i<square; i++){
					if (aButton[i].color == BtnMouse.grey){
						if (aButton[i].time + liveStayTime < now){
							aButton[i].color = BtnMouse.green;
							aButton[i].setBackground(Color.GREEN);
							groundTotal--;
						}
					}
					else if (aButton[i].color == BtnMouse.red){
						if (aButton[i].time + deadStayTime < now){
							aButton[i].color = BtnMouse.green;
							aButton[i].setBackground(Color.GREEN);
							groundTotal--;
						}
					}
				}
				if (groundTotal >= maxGroundTotal)
					continue;
				int which = (int)(Math.random() * square);
				if (aButton[which].color == BtnMouse.green){
					aButton[which].time = now;
					aButton[which].color = BtnMouse.grey;
					aButton[which].setBackground(Color.DARK_GRAY);
					groundTotal++;
				}
			}
		}
	}
	public static void main(String[] args){
		Marmot marmot = new Marmot();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -