Java小游戏--舒尔特方格

一、逻辑部分

public class GridCalculator {

	private int row = 0;
	private int column = 0;
	private int nextScore = 0;
	private long startTime = 0;
	private int costTime = 0;
	private boolean isTrainning = false;
	private boolean isFinished = false;
	
	private Timer timer;

	GridCalculator(int rows, int columns) {
		this.row = rows;
		this.column = columns;
		
	}

	public List generateSequence() {

		ArrayList list = new ArrayList();

		for (int i = 1; i <= row * column; i++) {

			list.add(i);
		}

		Collections.shuffle(list);
		return list;
	}

	public void startTrain() {
		this.costTime = 0;
		this.startTime = new Date().getTime();
		this.isTrainning = true;
		this.isFinished = false;
		this.nextScore = 1;
		
		this.timer = new  Timer();
		this.timer.schedule(new  TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				costTime = (int) ((new Date().getTime() - startTime) / 1000);
			}
		}, 0, 500);
	}

	public void stopTrain() {
		this.isTrainning = false;
		//this.costTime = (int) ((new Date().getTime() - this.startTime) / 1000);
		this.timer.cancel();
	}

	public boolean trySocre(int next) {
		boolean success = false;

		if (isTrainning && next == this.nextScore) {
			if (next == this.row * this.column) {
				// finish
				//this.costTime = (int) ((new Date().getTime() - this.startTime) / 1000);
				this.isFinished = true;
				this.isTrainning = false;
				this.timer.cancel();
			}

			this.nextScore++;
			success = true;
		}

		return success;
	}

	public boolean isFinishTrain() {
		return this.isFinished;
	}

	public int getCostTime() {
		if (isFinished || isTrainning) {
			return this.costTime;
		} else {
			//return Integer.MAX_VALUE;
			return 0;
		}
	}
}


二、页面部分

public class GridPage extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Map gridButtonMap;
	private List list;
	private GridCalculator calculator;

	private JButton btStart;
	private JButton btStop;
	private JLabel lbInfo;
	private JLabel lbUsedTime;
	private List btList;

	private Timer trainTimer;
	private Color defaultColor;
	
	private void initGridButton(List btList) {
		this.calculator = new GridCalculator((int) Math.sqrt(btList.size()),
				(int) Math.sqrt(btList.size()));
		this.list = calculator.generateSequence();
		this.gridButtonMap = new HashMap();

		if (this.list.size() != btList.size()) {
			lbInfo.setText("舒尔特方格规格错误!");

			return;
		} else {
			for (int i = 0; i < btList.size(); i++) {
				this.gridButtonMap.put(btList.get(i), this.list.get(i));
				btList.get(i).setText(this.list.get(i).toString());
			}
			lbInfo.setText("成功刷新舒尔特方格!");
			return;
		}
	}
	
	private void refreshGridButton(List btList) {
		this.list = calculator.generateSequence();
		this.gridButtonMap.clear();

		if (this.list.size() != btList.size()) {
			lbInfo.setText("舒尔特方格规格错误!");

			return;
		} else {
			for (int i = 0; i < btList.size(); i++) {
				this.gridButtonMap.put(btList.get(i), this.list.get(i));
				btList.get(i).setText(this.list.get(i).toString());
			}
			lbInfo.setText("成功刷新舒尔特方格!");
			return;
		}
	}

	private void tryToScoreAndFInishTrain(JButton bt) {

		if (this.calculator.trySocre(this.gridButtonMap.get(bt))) {
			if (this.calculator.isFinishTrain()) {
				this.lbInfo.setText("训练结束,共计用时" + this.calculator.getCostTime()
						+ "秒,继续加油!");
				this.btStart.setEnabled(true);
				this.btStop.setEnabled(false);
				stopShowTime();
			} else {
				this.lbInfo.setText("加油!请继续。");
			}
			
			bt.setBackground(Color.LIGHT_GRAY);

		} else {
			this.lbInfo.setText("抱歉!请点击下一个数字。");
			bt.setBackground(Color.RED);
		}
		
		new Timer().schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				bt.setBackground(defaultColor);
			}
		}, 1000);
	}

	private void startShulteTrain() {
		refreshGridButton(btList);

		this.calculator.startTrain();
		btStart.setEnabled(false);
		btStop.setEnabled(true);
	}

	private void startShowTime() {
		trainTimer = new Timer();
		trainTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				lbUsedTime.setText("计时: " + String.valueOf(calculator.getCostTime()) + " S");
			}
		}, 0, 500);
		
	}

	private void stopShowTime() {
		
		trainTimer.cancel();
		lbUsedTime.setText("计时:0 S ");
	}

	private void stopShulteTrain() {

		this.calculator.stopTrain();
		btStart.setEnabled(true);
		btStop.setEnabled(false);
	}

	public GridPage() {
		setTitle("\u8212\u5C14\u7279\u65B9\u683C");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// this.calculator = new GridCalculator(5, 5);
		// this.list = calculator.generateSequence();
		// this.iterator = list.iterator();
		// this.gridButtonMap = new HashMap();
		//

		btList = new ArrayList();
		
		
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(0, 0));
		panel.setLocation(new Point(10, 10));

		btStart = new JButton("\u5F00\u59CB\u8BA1\u65F6");
		btStart.setFocusable(false);
		btStart.addMouseListener(new MouseListener() {

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				startShulteTrain();
				startShowTime();
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

		});

		btStop = new JButton("\u6682\u505C");
		btStop.setFocusable(false);
		btStop.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				stopShulteTrain();
				stopShowTime();
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		btStop.setEnabled(false);
		btStop.setPreferredSize(new Dimension(81, 23));

		lbInfo = new JLabel("");

		lbUsedTime = new JLabel("\u8BA1\u65F6\uFF1A 0 S");
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(20)
							.addComponent(btStart)
							.addGap(51)
							.addComponent(btStop, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(82)
							.addComponent(lbInfo))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(88)
							.addComponent(lbUsedTime))
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 367, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(148, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 342, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(lbUsedTime)
					.addGap(42)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btStart)
						.addComponent(btStop, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(lbInfo)
					.addContainerGap(22, Short.MAX_VALUE))
		);

		JButton bt31 = new JButton("");
		bt31.setMaximumSize(new Dimension(100, 100));

		btList.add(bt31);
		defaultColor = bt31.getBackground();
		
		bt31.setPreferredSize(new Dimension(50, 50));
		bt31.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});

		JButton bt11 = new JButton("");
		bt11.setMaximumSize(new Dimension(100, 100));
		bt11.setPreferredSize(new Dimension(50, 50));

		panel.setLayout(new GridLayout(0, 5, 0, 0));
		btList.add(bt11);
		panel.add(bt11);

		JButton bt12 = new JButton("");

		btList.add(bt12);

		bt12.setPreferredSize(new Dimension(50, 50));
		panel.add(bt12);

		JButton bt13 = new JButton("");

		btList.add(bt13);
		bt13.setPreferredSize(new Dimension(50, 50));
		panel.add(bt13);

		JButton bt14 = new JButton("");

		btList.add(bt14);
		bt14.setPreferredSize(new Dimension(50, 50));
		panel.add(bt14);

		JButton bt15 = new JButton("");

		btList.add(bt15);
		bt15.setPreferredSize(new Dimension(50, 50));
		panel.add(bt15);

		JButton bt21 = new JButton("");
		bt21.setMaximumSize(new Dimension(100, 100));

		btList.add(bt21);
		bt21.setPreferredSize(new Dimension(50, 50));
		panel.add(bt21);

		JButton bt22 = new JButton("");

		btList.add(bt22);
		bt22.setPreferredSize(new Dimension(50, 50));
		panel.add(bt22);

		JButton bt23 = new JButton("");

		btList.add(bt23);
		bt23.setPreferredSize(new Dimension(50, 50));
		panel.add(bt23);

		JButton bt24 = new JButton("");

		btList.add(bt24);
		bt24.setPreferredSize(new Dimension(50, 50));
		panel.add(bt24);

		JButton bt25 = new JButton("");

		btList.add(bt25);
		bt25.setPreferredSize(new Dimension(50, 50));
		panel.add(bt25);
		panel.add(bt31);

		JButton bt32 = new JButton("");

		btList.add(bt32);
		bt32.setPreferredSize(new Dimension(50, 50));
		panel.add(bt32);

		JButton bt33 = new JButton("");

		btList.add(bt33);
		bt33.setPreferredSize(new Dimension(50, 50));
		panel.add(bt33);

		JButton bt34 = new JButton("");

		btList.add(bt34);
		bt34.setPreferredSize(new Dimension(50, 50));
		panel.add(bt34);

		JButton bt35 = new JButton("");

		btList.add(bt35);
		bt35.setPreferredSize(new Dimension(50, 50));
		panel.add(bt35);

		JButton bt41 = new JButton("");
		bt41.setMaximumSize(new Dimension(100, 100));

		btList.add(bt41);
		bt41.setPreferredSize(new Dimension(50, 50));
		panel.add(bt41);

		JButton bt42 = new JButton("");

		btList.add(bt42);
		bt42.setPreferredSize(new Dimension(50, 50));
		panel.add(bt42);

		JButton bt43 = new JButton("");

		btList.add(bt43);
		bt43.setPreferredSize(new Dimension(50, 50));
		panel.add(bt43);

		JButton bt44 = new JButton("");

		btList.add(bt44);
		bt44.setPreferredSize(new Dimension(50, 50));
		panel.add(bt44);

		JButton bt45 = new JButton("");

		btList.add(bt45);
		bt45.setPreferredSize(new Dimension(50, 50));
		panel.add(bt45);

		JButton bt51 = new JButton("");
		bt51.setMaximumSize(new Dimension(100, 100));

		btList.add(bt51);
		bt51.setPreferredSize(new Dimension(50, 50));
		panel.add(bt51);

		JButton bt52 = new JButton("");

		btList.add(bt52);
		bt52.setPreferredSize(new Dimension(50, 50));
		panel.add(bt52);

		JButton bt53 = new JButton("");

		btList.add(bt53);
		bt53.setPreferredSize(new Dimension(50, 50));
		panel.add(bt53);

		JButton bt54 = new JButton("");

		btList.add(bt54);
		bt54.setPreferredSize(new Dimension(50, 50));
		panel.add(bt54);

		JButton bt55 = new JButton("");

		btList.add(bt55);
		bt55.setPreferredSize(new Dimension(50, 50));
		panel.add(bt55);
		getContentPane().setLayout(groupLayout);

		initGridButton(btList);
		
		for (JButton bt : btList){
			bt.setFocusable(false);
			bt.setFont(new Font("微软雅黑", Font.PLAIN, 20));
			bt.addMouseListener(new MouseListener() {
				
				@Override
				public void mouseReleased(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mousePressed(MouseEvent e) {
					// TODO Auto-generated method stub
					tryToScoreAndFInishTrain(bt);
				}
				
				@Override
				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mouseClicked(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
			});
		}

	}
}


三、程序入口

public class ShulteGrid {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame mainPage = new GridPage();
		
		mainPage.setResizable(true);
		mainPage.setExtendedState(Frame.MAXIMIZED_BOTH);
		mainPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		mainPage.setVisible(true);
	}

}
四、效果图
Java小游戏--舒尔特方格_第1张图片

你可能感兴趣的:(Java小游戏--舒尔特方格)