Created
May 26, 2019 15:04
-
-
Save ahmednasserpro/fe15a51dd1ed0479c85f5c07295da8b4 to your computer and use it in GitHub Desktop.
Animation self-avoiding random walk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class AnimationSelfAvoidingRandomWalk extends JFrame { | |
private static final int N = 16; | |
private Point[][] lattice = new Point[N + 1][N + 1]; | |
private int i = (N + 1) / 2; | |
private int j = (N + 1) / 2; | |
private JButton start = new JButton("Start"); | |
private Timer timer = new Timer(100, new TimerListener()); | |
AnimationSelfAvoidingRandomWalk() { | |
add(new P()); | |
JPanel panel = new JPanel(); | |
panel.setBorder(BorderFactory.createLineBorder(Color.lightGray)); | |
panel.add(start); | |
add(panel, BorderLayout.SOUTH); | |
start.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
timer.stop(); | |
getPath(); | |
timer.start(); | |
} | |
}); | |
} | |
private void getPath() { | |
// Refresh the lattics | |
for (int i = 0; i < lattice.length; i++) { | |
for (int j = 0; j < lattice[i].length; j++) { | |
lattice[i][j] = null; | |
} | |
} | |
i = (N + 1) / 2; | |
j = (N + 1) / 2; | |
} | |
private class TimerListener implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (i > 0 && i < N && j > 0 && j < N) { | |
if (lattice[i - 1][j] != null && lattice[i + 1][j] != null | |
&& lattice[i][j - 1] != null && lattice[i][j + 1] != null) { | |
return; | |
} | |
double r = Math.random(); | |
if (r < 0.25 && lattice[i][j + 1] == null) { | |
lattice[i][j] = new Point(i, j + 1); // Right | |
j++; | |
} else if (r < 0.5 && lattice[i + 1][j] == null) { | |
lattice[i][j] = new Point(i + 1, j); // Down | |
i++; | |
} else if (r < .75 && lattice[i][j - 1] == null) { | |
lattice[i][j] = new Point(i, j - 1); // Left | |
j--; | |
} else if (r < 1.0 && lattice[i - 1][j] == null) { | |
lattice[i][j] = new Point(i - 1, j); // Up | |
i--; | |
} | |
repaint(); | |
} | |
} | |
} | |
private class P extends JPanel { | |
@Override | |
protected void paintComponent(Graphics g) { | |
int hGap = getWidth() / N; | |
int vGap = getHeight() / N; | |
// Draw the lattics | |
g.setColor(Color.lightGray); | |
for (int i = 0; i < lattice.length; i++) { | |
g.drawLine(0, i * vGap, getWidth(), i * vGap); | |
g.drawLine(i * hGap, 0, i * hGap, getHeight()); | |
} | |
int i = (N + 1) / 2; | |
int j = (N + 1) / 2; | |
// Draw the path | |
g.setColor(Color.BLACK); | |
while (lattice[i][j] != null) { | |
Point p = lattice[i][j]; | |
g.drawLine(i * hGap, j * vGap, p.x * hGap, p.y * vGap); | |
i = p.x; | |
j = p.y; | |
} | |
} | |
} | |
public static void main(String[] args) { | |
JFrame frame = new AnimationSelfAvoidingRandomWalk(); | |
frame.setSize(400, 200); | |
frame.setLocationRelativeTo(null); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment