A labyrinth in your mind?…coding and graphics

How to create labyrinths without paths with Java?

How to create labyrinths without paths with Java?

Let's put into practice the previous lessons:

Create a bistate or tristate matrix, i.e. with 2 or 3 random values. Create the graphic class that draws a horizontal, vertical or diagonal line depending on the state in the ij element of the matrix…

here is the code:

Main:

package maze;
import javax.swing.*;
public class Maze {
public static void main(String[] args) {

JFrame t= new JFrame(“Maze”);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Tela p = new Tela();

t.add(p);
t.setSize(800, 1000);
t.setVisible(true);
}
}

package labirinto;
import javax.swing.*;
public class Labirinto {
public static void main(String[] args) {

JFrame t= new JFrame(“Labirinto”);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Tela p = new Tela();
p.Riempire();
t.add(p);
t.setSize(800, 1000);
t.setVisible(true);
}
}

Object file TELA:

package maze;
import java.awt.*;;

import javax.swing.*;
public class Tela extends JPanel{
public int M[][]=new int [100][100];

public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.black);
// g.drawRect(10,10,700,700);
g.setColor(Color.black);
M=inizializzaMatriceRandom(M,4);
int x=0,y=0;
int px=10;
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
x=x+px;

 if(M[i][j]==0) { g.drawLine(x, y, x, y+10); //g.fillRect(10+20*j,10+20*i, 2, 20); } if(M[i][j]==1) { g.drawLine(x, y, x+10, y); //g.fillRect(10+20*j,30+20*i, 20, 2); } if(M[i][j]==2) { g.drawLine(x, y, x+10, y+10); // g.drawArc(x, y, 15, 15, 90, -90); //g.fillRect(10+20*j,30+20*i, 20, 2); } if(M[i][j]==3) { g.drawLine(x, y+10, x+10, y); // g.drawArc(x, y, 15, 15, 90, 90); //g.fillRect(10+20*j,30+20*i, 20, 2); } } x=0; y=y+px; }

}

public int[][] inizializzaMatriceRandom(int m[][],int numerostati)
{ for(int i=0;i<m.length;i++){
for(int j=0;j<m.length;j++){
m[i][j]=(int)(Math.random()*numerostati);
System.out.print(” “+ m[i][j]);
}
System.out.println();
}
return m;

 }

}

here is the javascript version of the code

https://js.do/mpalladino/labirinto