Created
February 8, 2018 12:12
-
-
Save cesco89/06ace99e6ea2a74bbcdecfbde39a572d to your computer and use it in GitHub Desktop.
A random java Maze generator
This file contains hidden or 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
public class Line { | |
public int posx; | |
public int posy; | |
public int dim1; | |
public int dim2; | |
public Line (int _posx, int _posy, int _dim1, int _dim2) { | |
this.posx = _posx; | |
this.posy = _posy; | |
this.dim1 = _dim1; | |
this.dim2 = _dim2; | |
} | |
} |
This file contains hidden or 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 javax.swing.*; | |
import java.awt.*; | |
import java.util.ArrayList; | |
public class LineFrame extends JPanel { | |
private double range = 0.5; | |
private int spacing = 12; | |
private int posX = 0; | |
private int posY = 0; | |
private static int width = Toolkit.getDefaultToolkit().getScreenSize().width; | |
private static int height = Toolkit.getDefaultToolkit().getScreenSize().height; | |
private ArrayList<Line> lines; | |
public LineFrame() { | |
lines = new ArrayList<>(); | |
//setSize(width,height); | |
} | |
void drawLines(Graphics g) { | |
Graphics2D g2d = (Graphics2D) g; | |
if(Math.random() < range) { | |
lines.add(new Line(posX, posY, posX + spacing, posY + spacing)); | |
}else { | |
lines.add(new Line(posX, posY + spacing, posX + spacing, posY)); | |
} | |
for(Line l : lines) { | |
Stroke stroke = new BasicStroke(2f); | |
g2d.setStroke(stroke); | |
g2d.setColor(Color.BLACK); | |
g2d.drawLine(l.posx, l.posy, l.dim1, l.dim2); | |
} | |
/** | |
* Draw lines by column | |
* | |
posY = posY+spacing; | |
if(posY > height) { | |
posY = 0; | |
posX = posX+spacing; | |
} | |
if(posX < width) { | |
this.repaint(); | |
} | |
**/ | |
/** | |
* Draw lines by row - default | |
*/ | |
posX = posX+spacing; | |
if(posX > width) { | |
posX = 0; | |
posY = posY+spacing; | |
} | |
if(posY < height) { | |
this.repaint(); | |
System.out.println("LINES ON SCREEN: "+lines.size()); | |
}else{ | |
System.out.println("TOTAL: " + lines.size()); | |
} | |
} | |
public void paint(Graphics g) { | |
super.paint(g); | |
drawLines(g); | |
} | |
} |
This file contains hidden or 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 javax.swing.*; | |
import java.awt.*; | |
public class Main { | |
public static void main(String... args) { | |
EventQueue.invokeLater(() -> { | |
JFrame frame = new JFrame("MAZE GENERATOR"); | |
LineFrame lFrame = new LineFrame(); | |
lFrame.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize()); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.add(lFrame); | |
frame.pack(); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment