You are on page 1of 11

I want a java program that generates random mazes for users to solve and allows them to specify

the size of the maze and the solution to the generated maze.

Quiero un programa Java swing que genere laberintos aleatorios para que los usuarios los
resuelvan y les permita especificar el tamaño del laberinto y la solución del laberinto generado.

Contraseña del sitio de programación: C8Awg2ggb.vC5$T


OTRO

package Laberinto1;

import java.awt.*;

/**

* @author Brandon

*/

public class Laberinto1 {

static Frame Ventana = new Frame("LABERINTO");

static Button btnIniciar = new Button();

static Button btnDetener = new Button();

static Dibujar objDibujar = new Dibujar();

static TextField txtTexto = new TextField(20);

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

Ventana.setSize(1000,1000);

btnIniciar.setLabel("Iniciar o Continuar");

btnDetener.setLabel("Detener");

Ventana.add(btnIniciar, BorderLayout.NORTH);

Ventana.add(btnDetener, BorderLayout.SOUTH);

Ventana.add(objDibujar);

Ventana.setVisible(true);
txtTexto.setText("No se detecto colision");

txtTexto.setBounds(100, 380, 250, 30);

Ventana.add(txtTexto);

Ventana.addWindowListener

(new java.awt.event.WindowAdapter(){

public void windowClosing (java.awt.event.WindowEvent e){

System.exit(0);

);

btnIniciar.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

objDibujar.Iniciar();

);

btnDetener.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

objDibujar.Detener();

);

static void ValidaColision(){

if(objDibujar.ChequeaColision())

txtTexto.setText("Hubo colision");

else

txtTexto.setText("No se detecta colision");

}
}

Segunda parte
package Laberinto1;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Brandon
*/
public class Dibujar extends Canvas implements Runnable,
MouseMotionListener,
MouseListener, KeyListener {

Thread objHilo;
boolean Ejecuta = false;
int PosX=10, PosY=10;
int IncX=1, IncY=1;
int Diametro = 40;
int PosXMouse, PosYMouse;
publicDibujar(){
addMouseMotionListener(this);
addMouseListener(this);
addKeyListener(this);
}
public void run(){
while(Ejecuta){
repaint();
try{Thread.sleep(3);} catch (InterruptedException E){}
}
}
public void paint(Graphics objGrafico){
PosX+=IncX;
PosY+=IncY;
objGrafico.setColor(Color.ORANGE);
objGrafico.fillOval(PosX, PosY, Diametro, Diametro);
objGrafico.setColor(Color.green);
objGrafico.drawLine(100,350,50,350);
objGrafico.drawLine(400,400,400,350);
objGrafico.drawLine(400,350,600,350);
objGrafico.drawLine(450,400,450,450);
objGrafico.drawLine(450,450,50,450);
objGrafico.drawLine(50,650,750,650);
objGrafico.drawLine(900,500,950,500);
objGrafico.drawLine(950,500,950,100);
objGrafico.drawLine(950,100,750,100);
objGrafico.drawLine(700,200,700,50);
objGrafico.drawLine(650,250,800,250);
objGrafico.drawLine(900,250,800,250);
objGrafico.drawLine(700,450,700,600);
objGrafico.drawLine(700,600,300,600);
objGrafico.drawLine(100,600,100,500);
objGrafico.drawLine(400,300,400,250);
objGrafico.drawLine(400,250,550,250);
objGrafico.drawLine(350,350,150,350);
objGrafico.drawLine(150,350,150,100);
objGrafico.drawLine(450,450,500,450);
objGrafico.drawLine(850,450,500,450);
objGrafico.setFont(new Font("Arial",Font.BOLD,20));
objGrafico.setColor(Color.GREEN);
objGrafico.drawString("Inicio Suerte",10,50);
objGrafico.drawLine(450,450,500,450);
objGrafico.setFont(new Font("Arial",Font.BOLD,20));
objGrafico.setColor(Color.green);
objGrafico.drawString("gano",920,600);

if(PosXMouse < PosX) IncX=-1; else IncX=1;


if(PosYMouse < PosY) IncY=-1; else IncY=1;
}
public void update(Graphics objGrafico){
Graphics NoVisible;
Image Imagen = null;
Rectangle Rectangulo = objGrafico.getClipRect();
Imagen = createImage(Rectangulo.width, Rectangulo.height);
NoVisible=Imagen.getGraphics();
NoVisible.setColor(getBackground());
NoVisible.fillRect(0,0 ,Rectangulo.width, Rectangulo.height);
NoVisible.setColor(getForeground());
NoVisible.translate(-Rectangulo.x, -Rectangulo.y);
paint(NoVisible);
objGrafico.drawImage(Imagen, 0 ,0 ,this);
}
public void Iniciar(){
if(Ejecuta == false){
objHilo = new Thread(this);
Ejecuta= true;objHilo.start();
}
}

public void Detener(){

Ejecuta= false;

public void mouseMoved(MouseEvent objMouse){

PosXMouse= (int) objMouse.getPoint().getX();

PosYMouse= (int) objMouse.getPoint().getY();

public void mouseDragged (MouseEvent objMouse){}

public void mousePressed (MouseEvent objMouse){}

public void mouseClicked (MouseEvent objMouse){}

public void mouseEntered (MouseEvent objMouse){}

public void mouseExited (MouseEvent objMouse){}


public void mouseReleased (MouseEvent objMouse){}

public void keyTyped (KeyEvent e){}

public void keyPressed (KeyEvent e){}

public void keyReleased(KeyEvent e){}

boolean ChequeaColision(){

return false;

Una vez que ninguna clase nos marque algún error ya podremos ejecutar elprograma y poder jugar
con el laberinto que acabamos de crear.

OTROOO

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class LaberintoGenerator extends JFrame {

private int tamañoLaberinto;

private char[][] laberinto;

private JLabel[][] casillas;

public LaberintoGenerator(int tamañoLaberinto) {

this.tamañoLaberinto = tamañoLaberinto;

this.laberinto = new char[tamañoLaberinto][tamañoLaberinto];

this.casillas = new JLabel[tamañoLaberinto][tamañoLaberinto];

setTitle("Laberinto Generator");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setResizable(false);
setLayout(new GridLayout(tamañoLaberinto, tamañoLaberinto));

generarLaberinto();

dibujarLaberinto();

mostrarLaberinto();

pack();

setLocationRelativeTo(null);

setVisible(true);

private void generarLaberinto() {

// Genera el laberinto aleatoriamente

// Aquí puedes implementar tu algoritmo de generación de laberinto

private void dibujarLaberinto() {

// Dibuja el laberinto en el arreglo de casillas

for (int i = 0; i < tamañoLaberinto; i++) {

for (int j = 0; j < tamañoLaberinto; j++) {

JLabel casilla = new JLabel();

casilla.setOpaque(true);

casilla.setPreferredSize(new Dimension(30, 30));

casilla.setHorizontalAlignment(JLabel.CENTER);

if (laberinto[i][j] == 'P') {

casilla.setBackground(Color.GREEN);

} else if (laberinto[i][j] == 'F') {

casilla.setBackground(Color.RED);
} else {

casilla.setBackground(Color.WHITE);

casillas[i][j] = casilla;

add(casilla);

private void mostrarLaberinto() {

// Muestra el laberinto en la ventana

for (int i = 0; i < tamañoLaberinto; i++) {

for (int j = 0; j < tamañoLaberinto; j++) {

casillas[i][j].setText(String.valueOf(laberinto[i][j]));

public static void main(String[] args) {

int tamañoLaberinto = Integer.parseInt(JOptionPane.showInputDialog("Ingrese el tamaño del


laberinto:"));

LaberintoGenerator laberintoGenerator = new LaberintoGenerator(tamañoLaberinto);

You might also like