一个有趣的程序-暂时没有看懂
来源:互联网 发布:淘宝达人怎么申请入驻 编辑:程序博客网 时间:2024/05/21 11:10
package test;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import java.util.Random;
public class Testsync extends JFrame implements Runnable {
private int size;
private int squarePixels;
private boolean[][] pointsOccupied;
private int[] currentPosition = new int[2], nextPosition = new int[2];
private Thread thread;
private Random random;
public Testsync(int gridSize, int gridSquarePixels) {
super("Self Avoiding Random Walk");
size = gridSize;
squarePixels = gridSquarePixels;
setSize(size*squarePixels, size*squarePixels);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pointsOccupied = new boolean[size][];
for (int i = 0; i < size; i++)
pointsOccupied[i] = new boolean[size];
thread = new Thread(this);
random = new Random();
reset();
}
public void reset() {
currentPosition[0] = nextPosition[0] = currentPosition[1] = nextPosition[1] = size/2;
for (int i = 0; i < size; i++)
java.util.Arrays.fill(pointsOccupied[i], false);
pointsOccupied[size/2][size/2] = true;
}
public void draw() {
thread.start();
}
public void run() {
while(true) {
if (!randomChooseNext())
break;
repaint();
try {
Thread.sleep(1*100);
} catch(Exception e) { }
}
if (JOptionPane.showConfirmDialog(this, "Path ends. Still want to see another one?") == JOptionPane.YES_OPTION) {
getGraphics().clearRect(0, 0, size*squarePixels, size*squarePixels);
reset();
run();
}
System.exit(0);
}
public void paint(Graphics g) {
g.drawLine(currentPosition[0]*squarePixels, currentPosition[1]*squarePixels, nextPosition[0]*squarePixels, nextPosition[1]*squarePixels);
currentPosition[0] = nextPosition[0];
currentPosition[1] = nextPosition[1];
}
private boolean randomChooseNext() {
int dirsAvailable = 4;
int newX, newY;
int dir;
while (dirsAvailable>0) {
newX = currentPosition[0];
newY = currentPosition[1];
dir = random.nextInt(4);
switch(dir) {
case 0: // left
newX--;break;
case 1: // top
newY--; break;
case 2: // right
newX++; break;
case 3: // bottom
newY++; break;
}
if (newX <0 || newY <0 || newX>size-1 || newY>size-1 || pointsOccupied[newX][newY])
dirsAvailable--;
else {
nextPosition[0] = newX;
nextPosition[1] = newY;
pointsOccupied[newX][newY] = true;
return true;
}
}
return false;
}
public static void main(String[] args) {
String input;
int size, squarePixels;
while (true) {
input = JOptionPane.showInputDialog("Please tell me the size of the grid you want to create (between 10 to 100):");
try {
size = Integer.parseInt(input);
if (size < 10 || size > 100)
throw new Exception();
break;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Sorry, input unacceptable. Please try again.");
}
}
while (true) {
input = JOptionPane.showInputDialog("Now please tell me the number of pixels of each grid square (between 5 to 10):");
try {
squarePixels = Integer.parseInt(input);
if (squarePixels < 5 || squarePixels > 10)
throw new Exception();
break;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Sorry, input unacceptable. Please try again.");
}
}
Testsync frame = new Testsync(size, squarePixels);
frame.draw();
}
}
- 一个有趣的程序-暂时没有看懂
- 一个有趣的程序
- 一个有趣的程序
- 一个有趣的C程序
- 一个有趣的并行程序
- 记录一个暂时没有答案的race condition问题
- 看懂一个程序的3个步骤
- 如何看懂一个程序
- 如何看懂一个程序
- 关于程序的一个有趣的问题
- 一个控制鼠标的有趣的程序
- 一个有趣的打印菱形的程序
- JQuery的一个有趣的小程序
- 一个有趣的GDI图形测试程序
- 一个有趣的死循环程序
- 一个有趣的自动刷新程序
- 展示编写的一个有趣C程序
- 一个控制电脑关机的有趣程序
- ASP.Net 实战项目 之 《图书商城》 第三天
- 关于cvLoadImage 的泄露问题
- 使用ZeroClipboard解决跨浏览器复制到剪贴板的问题
- 网站SEO优化重点是什么
- 开发中遇到的问题和解决方法
- 一个有趣的程序-暂时没有看懂
- SEO。坚持就是王道
- AppFabric 使用
- Java单例模式的三种情况
- Apache ActiveMQ介绍
- Hive 0.9.0编译过程
- 三款工作流引擎比较:WWF、netBPM 和 ccflow
- 在Emacs中使用magit管理Git
- 谷歌面试题:1024! 末尾有多少个0?