2012-12-08 13:49:19Morris
[JAVA][模仿失敗作] 隨機
demo: http://140.115.216.220/test/
原本是想去做一個無序隨機到有序的模擬,教授展示的真的很不錯,一開始一團,但之後就自動走出一條路了。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class RandomFrame extends JFrame {
boolean init = true;
int x, y, lastdir = 0;
int[][] board = new int[1000/6][1000/6];
public RandomFrame() {
this.setSize(1000, 1000);
this.setResizable(false);
this.setTitle("INORDER TO ORDER");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = 1000/6/2;
y = 1000/6/2;
board[x][y] = 1;
Timer timer = new Timer(10, new TimerListener());
timer.start();
}
public void paint(Graphics g) {
if(init) {
g.setColor(Color.gray);
for(int i = 0; i < 1000; i++)
g.drawLine(0, i, 1000, i);
g.setColor(Color.black);
for(int i = 0; i < 1000; i += 6) {
for(int j = 0; j < 1000; j += 6) {
g.fillRect(i, j, 5, 5);
}
}
init = false;
} else {
if(board[x][y] == 0) {
board[x][y] = 1;
g.setColor(Color.red);
} else {
board[x][y] = 0;
g.setColor(Color.black);
}
g.fillRect(x*6, y*6, 5, 5);
}
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int dir;
dir = (int)(Math.random()*4);
if(dir == 0) {
if(x+1 < 1000/6) {
x++;
if(board[x][y] == 1) {
if(y+1 < 1000/6)
y++;
}
}
}
if(dir == 2) {
if(x-1 >= 0) {
x--;
if(board[x][y] == 1) {
if(y-1 >= 0)
y--;
}
}
}
if(dir == 1) {
if(y+1 < 1000/6) {
y++;
if(board[x][y] == 1) {
if(x-1 >= 0)
x--;
}
}
}
if(dir == 3) {
if(y-1 >= 0) {
y--;
if(board[x][y] == 1) {
if(x+1 < 1000/6)
x++;
}
}
}
lastdir = dir;
repaint();
}
}
public static void main(String[] args) {
RandomFrame gui = new RandomFrame();
gui.setVisible(true);
}
}
JApplet
網頁版本。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class RandomApp extends JApplet {
public void init() {
RandomFrame p = new RandomFrame();
this.getContentPane().add(p);
repaint();
this.setSize(1000, 1000);
}
boolean init = true;
int x, y;
int[][] board = new int[1000/6][1000/6];
public void paint(Graphics g) {
if(init) {
g.setColor(Color.gray);
for(int i = 0; i < 1000; i++)
g.drawLine(0, i, 1000, i);
g.setColor(Color.black);
for(int i = 0; i < 1000; i += 6) {
for(int j = 0; j < 1000; j += 6) {
g.fillRect(i, j, 5, 5);
}
}
init = false;
} else {
if(board[x][y] == 0) {
board[x][y] = 1;
g.setColor(Color.red);
} else {
board[x][y] = 0;
g.setColor(Color.black);
}
g.fillRect(x*6, y*6, 5, 5);
}
}
class RandomFrame extends JPanel {
public RandomFrame() {
this.setSize(1000, 1000);
this.setBounds(0, 0, 1000, 1000);
x = 1000/6/2;
y = 1000/6/2;
board[x][y] = 1;
Timer timer = new Timer(30, new TimerListener());
timer.start();
}
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int dir;
dir = (int)(Math.random()*4);
if(dir == 0) {
if(x+1 < 1000/6) {
x++;
if(board[x][y] == 1) {
if(y+1 < 1000/6)
y++;
}
}
}
if(dir == 2) {
if(x-1 >= 0) {
x--;
if(board[x][y] == 1) {
if(y-1 >= 0)
y--;
}
}
}
if(dir == 1) {
if(y+1 < 1000/6) {
y++;
if(board[x][y] == 1) {
if(x-1 >= 0)
x--;
}
}
}
if(dir == 3) {
if(y-1 >= 0) {
y--;
if(board[x][y] == 1) {
if(x+1 < 1000/6)
x++;
}
}
}
repaint();
}
}
}