蚂蚁爬杆之动态演示


/*
 * 游戏输入说明:
 * 蚂蚁爬杆有32种方法。输入是【0,32)有效的
 * 黑色的方框表示蚂蚁。这个你懂得。
 */


///*
// * 游戏输入说明:
// * 蚂蚁爬杆有32种方法,输入是【0,32)有效的
// * 黑色的方框表示蚂蚁,这个你懂得。
// */

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

///*
// * 有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、18厘米、23厘米这五个位置上各有一仅仅蚂蚁。
// * 木杆非常细,不能同一时候通过两仅仅蚂蚁。
// * 開始时,蚂蚁的头朝左还是朝右是随意的。它们仅仅会朝前走或调头,但不会后退。

// * 当随意两仅仅蚂蚁碰头时,两仅仅蚂蚁会同一时候调头朝反方向走。

// * 如果蚂蚁们每秒钟能够走一厘米的距离。

// * 编敲代码,求所有蚂蚁都离开木杆的最小时间和最大时间。 // * 要求:用类模拟出蚂蚁的行为特性, // * 进而模拟出五仅仅蚂蚁在木杆上的执行过程来编程求解。 // * 不能通过数学的方式直接用公式计算。

// */ public class Ants_Run extends JFrame { private static final long serialVersionUID = 1L;// serialVersionUID唯一的可串行化的版本号 private static boolean[][] dirs = new boolean[32][5]; private static int[] pos = { 3, 7, 11, 18, 23 }; private ControlJPanel controlJPanel = null; private PaintActiveAntsJPanel paintActiveAntsJPanel = null; private boolean isSuspend = false;// isSuspend是否暂停 private int[][] ants = new int[26][29];// 最长时间24,杆长27,就有27个位置。

private int count = 0;// 记录蚂蚁运动的位置 private Ant[] woods = { new Ant(), new Ant(), new Ant(), new Ant(), new Ant() }; private int time = 0;// 每一种情况执行的时间 private JLabel textTimeLabel = null, textCountLabel = null; public Ants_Run() { super("动态演示蚂蚁爬杆行为"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(200, 100, 900, 200); this.setLayout(new GridLayout(2, 1)); // new 对象 controlJPanel = new ControlJPanel(); paintActiveAntsJPanel = new PaintActiveAntsJPanel(); situation(0); this.getContentPane().add(controlJPanel); this.getContentPane().add(paintActiveAntsJPanel); setVisible(true); } // 控制面板 public class ControlJPanel extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; protected JButton jButton_Pause = null, jButton_Restart = null;// 暂停/開始button protected JTextField textSituation = null; public ControlJPanel() { this.setLayout(null);// 绝对定位 // new 对象 jButton_Pause = new JButton("Start");// 暂停 jButton_Restart = new JButton("Restart");// 暂停 textSituation = new JTextField(20); JLabel label = new JLabel("请输入:"); textTimeLabel = new JLabel(); textCountLabel = new JLabel(); // 设置位置 jButton_Pause.setBounds(350, 50, 80, 30); jButton_Restart.setBounds(450, 50, 80, 30); label.setBounds(100, 10, 70, 30); textSituation.setBounds(170, 10, 100, 30); textTimeLabel.setBounds(450, 10, 100, 30); textCountLabel.setBounds(530, 10, 100, 30); setJLabel(); // jButton_verify加监听 jButton_Pause.addActionListener(this); jButton_Restart.addActionListener(this); textSituation.addActionListener(this); // 加组件 this.add(jButton_Pause); this.add(jButton_Restart); this.add(label); this.add(textSituation); this.add(textTimeLabel); this.add(textCountLabel); } public void setJLabel() { textTimeLabel.setText("time: " + (time - 1)); textCountLabel.setText("count: " + count); } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Pause")) { jButton_Pause.setText("Start"); isSuspend = true; } if (e.getActionCommand().equals("Start")) { jButton_Pause.setText("Pause"); isSuspend = false; } if (e.getActionCommand().equals("Restart")) { paintActiveAntsJPanel.repaint(); paintActiveAntsJPanel.start(); jButton_Pause.setText("Pause"); isSuspend = false; } if (e.getSource() == textSituation) { try { int i = Integer.parseInt(textSituation.getText()); if (i < 0 || i > 26) { i = 0; } textSituation.setText("" + i); paintActiveAntsJPanel.start(); situation(i); jButton_Pause.setText("Start"); } catch (NumberFormatException e1) { e1.printStackTrace(); } } } } // PaintActiveAntsJPanel 画活动的蚂蚁 public class PaintActiveAntsJPanel extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; private int width = 30;// 设置像素为1。一个像素等于10个int protected Timer timer = null; public PaintActiveAntsJPanel() { timer = new Timer(1000, this); start(); } public void start() { count = 0;// 又一次開始 isSuspend = true; timer.restart(); timer.setDelay(1000); } public void end() { timer.stop(); controlJPanel.jButton_Pause.setText("Start"); } @Override public void paint(Graphics g) { if (count == time) { end(); } super.paint(g);// 擦掉原来的 for (int j = 1; j <= 27; j++) {// 画27个位置 g.setColor(Color.red); g.drawString(" " + j, 10 + j * width, 30); if (ants[count][j] == 0) {// 画框框 g.setColor(Color.blue); g.draw3DRect(10 + j * width, 30, width, width, false); } else {// 实框表示蚂蚁 g.setColor(Color.black); g.fill3DRect(10 + j * width, 30, width, width, true);// 35是我调节出来的 } } } @Override public void actionPerformed(ActionEvent e) { this.repaint(); controlJPanel.setJLabel(); if (isSuspend) {// 如果暂停,那么记录当前位置 return; } count++; } } public void situation(int i) { // 初始化 for (int j = 0; j < ants.length; j++) { for (int k = 0; k < ants[j].length; k++) { ants[j][k] = 0; } } for (int j = 0; j < woods.length; j++) { woods[j].setAlive(true); woods[j].setPosition(pos[j]); woods[j].setDirection(dirs[i][j]); } time = 0; // 蚂蚁移动 while (true) { // 记录数据。 要初始化后才干够记录,不然数据有问题 for (int j = 0; j < woods.length; j++) { ants[time][woods[j].position] = 1; } time++; // 移动 for (int j = 0; j < woods.length; j++) { woods[j].move(1); } // 相撞或者相遇 for (int j = 0; j < woods.length - 1; j++) { if (woods[j].isAlive && woods[j + 1].isAlive && !woods[j].direction && woods[j + 1].direction) { if (woods[j + 1].position == woods[j].position || (woods[j + 1].position - woods[j].position) == 1) { woods[j].direction = !woods[j].direction; woods[j + 1].direction = !woods[j + 1].direction; } } } // 蚂蚁是否所有掉下 boolean isEnd = true; for (int j = 0; j < woods.length; j++) { if (woods[j].isAlive) { isEnd = false; } } // 是否结束 if (isEnd) { break; } } } public static void main(String[] args) { for (int i = 0; i < dirs.length; i++) { for (int j = 0; j < dirs[i].length; j++) { dirs[i][j] = (i & (0x01 << j)) == 0;// true 左 false 右 } } new Ants_Run(); } } class Ant { protected boolean direction; protected int position; protected boolean isAlive; protected final int START = 1; protected final int END = 27;// 长度做死了 public void setDirection(boolean direction) { this.direction = direction; } public void setPosition(int position) { this.position = position; } public void setAlive(boolean isAlive) { this.isAlive = isAlive; } public void move(int n) { if (isAlive) { if (direction) { position -= n; } else { position += n; } if (position < START) { isAlive = false; } if (position > END) { isAlive = false; } } } }




posted @ 2016-02-22 10:56  blfshiye  阅读(363)  评论(0编辑  收藏  举报