Foro de elhacker.net

Programación => Java => Mensaje iniciado por: Camiloher1961 en 9 Junio 2021, 20:18 pm



Título: Ayuda por favor Proyecto java
Publicado por: Camiloher1961 en 9 Junio 2021, 20:18 pm
Buenas tardes amigos quiero saber como puedo leer un fichero de texto con JFilechooser e imprima una laberinto esto es lo que llevo de mi proyecto solo me falta el leer y que me imprima un laberinto desde un archivo txt
package juego;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;


/**
 *
 * @author oneiber
 */
public class Juego {
   
    Sonidos sonido = new Sonidos();
    static JFrame ventana;
   
    //presentacion
    JPanel panelPresentacion;
    JButton iniciar;
    JLabel fondoPresentacion;
    ImageIcon imagenFondoPres;
   
    //menu
    JPanel panelMenu;
    JButton botones[];
    JLabel fondoMenu;
    ImageIcon imagenFondoMenu;
     
    //juego
   
    static JPanel panelJuego;
    JLabel fondoJuego;
    ImageIcon imagenFondoJuego;
    static int mat[][];
    static JLabel matriz [][];
    int px;
    int py;
    String jugador;
    JLabel nombre;
    int puntos;
    JLabel records;
    int abajo;
    int arriba;
    int izq;
    int der;
    Timer timer;
   
    Gatos gato1;
    Gatos gato2;
    Gatos gato3;
    static int matAux[][];
   
   
    public Juego(){
   
        ventana = new JFrame("LABERINTO");
        ventana.setSize(700, 700);
        ventana.setLayout(null);
        ventana.setLocationRelativeTo(null);
        ventana.setResizable(false);
        ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        panelPresentacion = new JPanel();
        panelPresentacion.setLayout(null);
        panelPresentacion.setBounds(0,0,ventana.getWidth(),ventana.getHeight());
        panelPresentacion.setVisible(true);
        panelPresentacion.setBackground(Color.red);
       
        iniciar = new JButton("Iniciar");
        iniciar.setBounds(ventana.getWidth()-120, 20, 100, 30);
        iniciar.setVisible(true);
        iniciar.setBackground(Color.white);
        panelPresentacion.add(iniciar,0);
       
        fondoPresentacion = new JLabel();
        fondoPresentacion.setBounds(0, 0, ventana.getWidth(), ventana.getHeight());
        imagenFondoPres = new ImageIcon("imagenes/fondoPresentacion.jpg");
        imagenFondoPres = new ImageIcon(imagenFondoPres.getImage().getScaledInstance(ventana.getWidth(), ventana.getHeight(), Image.SCALE_DEFAULT));
        fondoPresentacion.setIcon(imagenFondoPres);
        fondoPresentacion.setVisible(true);
        panelPresentacion.add(fondoPresentacion,0);
       
       
        botones = new JButton[4];
        for (int i = 0; i < botones.length; i++) {
            botones = new JButton();
        }
       
       
     
       
       
        iniciar.addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e){
                 System.out.println("iniciar");
                 menu();
                 eventoMenu();
            }
       
        });
       
       
       
         mat = new int[15][15];
         mat = Laberinto(1);
         
         matriz = new JLabel[15][15];
         matAux = new int[15][15];
         for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat.length; j++) {
                matriz[j] = new JLabel();
                matAux[j] = mat[j];
            }
           
        }
       
       
        px = 1;
        py = 1;
        mat[px][py] = 3;
       
       
       
       
        abajo = 0;
        arriba = 0;
        izq = 0;
        der = 0;
       
       
       
 
        ventana.add(panelPresentacion);     
       
        ventana.setVisible(true);
       
   
    }
   
    public void jugar(){
       
        panelMenu.setVisible(false);
        panelJuego = new JPanel();
        panelJuego.setLayout(null);
        panelJuego.setBounds(0,0,ventana.getWidth(),ventana.getHeight());
        panelJuego.setVisible(true);
       
        fondoJuego = new JLabel();
        fondoJuego.setBounds(0, 0, ventana.getWidth(), ventana.getHeight());
        imagenFondoJuego = new ImageIcon("imagenes/fondoJugar.jpg");
        imagenFondoJuego = new ImageIcon(imagenFondoMenu.getImage().getScaledInstance(ventana.getWidth(), ventana.getHeight(), Image.SCALE_DEFAULT));
        fondoJuego.setIcon(imagenFondoJuego);
        fondoJuego.setVisible(true);
        panelJuego.add(fondoJuego,0);
       
        for (int i = 0; i < mat.length; i++) {
                for (int j = 0; j < mat.length; j++) {
                    matriz[j].setIcon(new ImageIcon("imagenes/"+mat[j]+".png"));
                    matriz[j].setBounds(120+(i*30), 120+(j*30), 30, 30);
                    matriz[j].setVisible(true);
                    panelJuego.add(matriz[j],0);
            }
        }
       
        nombre = new JLabel("JUGADOR: "+ jugador);
        nombre.setBounds(20, 20, 150, 30);
        nombre.setForeground(Color.BLACK);
        nombre.setVisible(true);
        panelJuego.add(nombre,0);
       
       
        puntos = 0;
        records = new JLabel("Puntos: "+puntos);
        records.setBounds(ventana.getWidth()-(150+20), 20, 150, 30);
        records.setVisible(true);
        records.setForeground(Color.BLACK);
        panelJuego.add(records,0);
        mover();
        gato1 = new Gatos(10 ,10 );
        gato2 = new Gatos(5 ,13 );
        gato3 = new Gatos(13 ,12 );
        ventana.add(panelJuego);
               
    }
   
    public static void pintarMatriz(){
        for (int i = 0; i < mat.length; i++) {
                for (int j = 0; j < mat.length; j++) {
                    matriz[j].setIcon(new ImageIcon("imagenes/"+mat[j]+".png"));
                    matriz[j].setBounds(120+(i*30), 120+(j*30), 30, 30);
                    matriz[j].setVisible(true);
                    panelJuego.add(matriz[j],0);
            }
        }
    }
   
    public void mover(){
       
       
        timer = new Timer (300, new ActionListener ()
        {
            public void actionPerformed(ActionEvent e)
            {
              if( arriba == 1 && (mat[px][py-1]==1 || mat[px][py-1]==0)){
                    if(mat[px][py-1]==1){
                        puntos = puntos + 5;
                        records.setText("Puntos: "+puntos);
                    }
                    mat[px][py] = 0;
                    matAux[px][py] = mat[px][py];
                    py = py-1;
                    mat[px][py] = 3;
                    pintarMatriz();
                               
              }
              if( abajo == 1 && (mat[px][py+1]==1 || mat[px][py+1]==0)){
                    if(mat[px][py+1]==1){
                        puntos = puntos + 5;
                        records.setText("Puntos: "+puntos);
                    }
                    mat[px][py] = 0;
                    matAux[px][py] = mat[px][py];
                    py = py+1;
                    mat[px][py] = 3;
                    pintarMatriz();
                     
              }
              if( izq == 1 && (mat[px-1][py]==1 || mat[px-1][py]==0)){
                    if(mat[px-1][py]==1){
                        puntos = puntos + 5;
                        records.setText("Puntos: "+puntos);
                    }
                    mat[px][py] = 0;
                    matAux[px][py] = mat[px][py];
                    px = px-1;
                    mat[px][py] = 3;
                    pintarMatriz();
                     
              }
              if( der == 1 && (mat[px+1][py]==1 || mat[px+1][py]==0)){
                    if(mat[px+1][py]==1){
                        puntos = puntos + 5;
                        records.setText("Puntos: "+puntos);
                    }
                    mat[px][py] = 0;
                    matAux[px][py] = mat[px][py];
                    px = px+1;
                    mat[px][py] = 3;
                    pintarMatriz();
                   
              }
                int queso = 0;
                for (int i = 0; i < mat.length && queso == 0; i++) {
                    for (int j = 0; j < mat.length && queso == 0; j++) {
                          if(mat[j]==1)
                             queso =1;
                    }
                }
                if(queso == 0){
                    sonido.PlayGanar();
                    sonido.PausarMuerte();
                    sonido.PausarJuego();
                    JOptionPane.showMessageDialog(ventana, "Saliste del laberinto, Felicidades ganaste");
                   
                    panelJuego.setVisible(false);
                    panelMenu.setVisible(true);
                    timer.stop();
                    System.exit(0);
                }
                if(  mat[px][py+1] == 7 || mat[px][py-1] == 7 || mat[px-1][py] == 7 || mat[px+1][py] == 7 ){
                   
                    sonido.PlayMuerte();
                    sonido.PausarJuego();
                    gato1.timer.stop();
                    gato2.timer.stop();
                    gato3.timer.stop();
                    JOptionPane.showMessageDialog(ventana, "ESTAS MUERTO");
                    sonido.PausarMuerte();
                    panelJuego.setVisible(false);
                    panelMenu.setVisible(true);
                    timer.stop();
                    System.exit(0);
                   
                   
                }
        }});
        timer.start();
        ventana.addKeyListener(new KeyListener(){

            @Override
            public void keyTyped(KeyEvent e) {
               // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void keyPressed(KeyEvent e) {
               
                if(e.getKeyCode() == KeyEvent.VK_UP){
                    System.out.println("tecla hacia arriba");
                    if(mat[px][py-1]==1 || mat[px][py-1]==0 ){
                        arriba = 1;
                        abajo = 0;
                        izq = 0;
                        der = 0;
                    }   
                }
                if(e.getKeyCode() == KeyEvent.VK_DOWN){
                    System.out.println("tecla hacia abajo");
                    if(mat[px][py+1]==1 || mat[px][py+1]==0 ){
                        arriba = 0;
                        abajo = 1;
                        izq = 0;
                        der = 0;
                    }   
                }
                if(e.getKeyCode() == KeyEvent.VK_LEFT){
                    System.out.println("tecla hacia izquierda");
                    if(mat[px-1][py]==1 || mat[px-1][py]==0 ){
                        arriba = 0;
                        abajo = 0;
                        izq = 1;
                        der = 0;
                    }   
                }
                if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                    System.out.println("tecla hacia derecha");
                    if(mat[px+1][py]==1 || mat[px+1][py]==0 ){
                        arriba = 0;
                        abajo = 0;
                        izq = 0;
                        der = 1;
                    }
                }
               

                 //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void keyReleased(KeyEvent e) {
                //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
   
   
    });
   
   
    }
   
    public int[][] Laberinto(int opcion){
   
        int[][]aux1 = new int[15][15];
        if( opcion == 1){
           
            int aux[][] = {
                    {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                    {2,3,0,0,0,0,0,2,0,0,0,0,0,0,2},
                    {2,2,2,2,0,2,0,2,0,2,2,0,2,0,2},
                    {2,0,2,0,0,2,0,0,0,0,2,0,2,0,2},
                    {2,0,0,0,2,2,2,0,2,0,0,0,0,0,2},
                    {2,0,2,0,0,0,0,0,2,2,2,0,2,0,2},
                    {2,0,2,2,0,2,2,0,0,2,2,0,0,0,2},
                    {2,0,0,0,0,0,2,2,0,0,0,0,2,2,2},
                    {2,0,2,0,2,0,2,2,2,0,2,0,2,0,2},
                    {2,0,0,0,2,0,0,0,2,0,0,0,0,0,2},
                    {2,0,2,0,0,0,2,2,2,0,2,0,2,0,1},
                    {2,0,0,0,2,0,2,0,0,0,0,0,2,2,2},
                    {2,0,2,0,2,0,2,0,2,0,2,0,2,0,2},
                    {2,0,0,0,2,0,0,0,0,0,0,0,0,0,2},
                    {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                  };
           
            return aux;
        }
        if( opcion == 2){
            int aux[][] = {
                    {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                    {2,3,0,0,0,0,0,2,0,0,0,0,0,0,2},
                    {2,0,2,0,0,2,0,2,0,2,2,0,2,0,2},
                    {2,0,2,0,2,2,0,0,0,0,2,0,2,0,2},
                    {2,0,0,0,2,2,0,2,2,0,0,0,0,0,2},
                    {2,0,2,0,0,0,0,0,2,2,2,0,2,2,2},
                    {2,0,2,2,0,2,2,0,0,2,2,0,0,0,2},
                    {2,0,0,0,0,0,2,2,0,0,0,0,2,2,2},
                    {2,2,2,0,2,0,2,2,2,0,2,0,0,0,2},
                    {2,0,0,0,2,0,0,0,0,0,0,0,0,0,2},
                    {2,0,2,0,0,0,2,2,2,0,2,0,2,0,2},
                    {2,0,0,0,2,0,2,0,0,0,0,0,2,0,2},
                    {2,0,2,0,2,0,2,0,2,0,2,0,2,0,2},
                    {2,0,0,0,2,0,0,0,0,0,0,0,0,0,2},
                    {2,2,2,2,2,2,1,2,2,2,2,2,2,2,2},
                  };
            return aux;
        }
        if( opcion == 3){
            int aux[][] = {
                    {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                    {2,3,0,0,0,0,0,2,0,0,0,0,0,0,2},
                    {2,0,2,2,0,2,0,0,0,2,2,0,2,0,2},
                    {2,0,2,0,0,2,0,0,0,0,2,0,2,0,2},
                    {2,0,0,0,2,0,2,0,2,0,0,0,0,0,2},
                    {2,0,2,0,0,0,0,0,2,2,2,2,2,2,2},
                    {2,0,2,2,0,2,2,0,0,2,2,0,0,0,2},
                    {2,0,0,0,0,0,2,2,0,0,0,0,2,0,2},
                    {2,2,2,0,2,0,2,2,2,2,2,0,2,0,2},
                    {2,0,0,0,2,0,0,0,0,0,0,0,0,0,2},
                    {2,0,2,0,0,0,2,2,2,0,2,0,2,0,2},
                    {2,0,0,0,2,0,2,0,0,0,0,0,0,0,2},
                    {1,0,2,0,2,0,2,0,2,0,2,0,2,0,2},
                    {2,0,0,0,0,0,0,0,0,2,0,0,0,0,2},
                    {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                  };
             return aux;
        }
        return aux1;
    }
   
   

    public void menu(){
       
        panelPresentacion.setVisible(false);
        panelMenu = new JPanel();
        panelMenu.setLayout(null);
        panelMenu.setBounds(0,0,ventana.getWidth(),ventana.getHeight());
        panelMenu.setVisible(true);
       
        fondoMenu = new JLabel();
        fondoMenu.setBounds(0, 0, ventana.getWidth(), ventana.getHeight());
        imagenFondoMenu = new ImageIcon("imagenes/fondoMenu.jpg");
        imagenFondoMenu = new ImageIcon(imagenFondoMenu.getImage().getScaledInstance(ventana.getWidth(), ventana.getHeight(), Image.SCALE_DEFAULT));
        fondoMenu.setIcon(imagenFondoMenu);
        fondoMenu.setVisible(true);
        panelMenu.add(fondoMenu,0);
       
        botones[0].setText("JUGAR");
        botones[1].setText("Elegir laberinto");
        botones[2].setText("Leer laberinto");
   botones[3].setText("SALIR");
   
       
        for (int i = 0; i < botones.length; i++) {
            botones.setBounds(ventana.getWidth()-(200+50), (i+1)*50, 200, 40);
            botones.setVisible(true);
            botones.setBackground(Color.WHITE);
            panelMenu.add(botones,0);
        }
       
        ventana.add(panelMenu);
       
       
   
    }//fin del menu
   
    public void eventoMenu(){
       
        //boton jugar
        botones[0].addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e){
                 System.out.println("jugar");
                 
                jugador = JOptionPane.showInputDialog(ventana, "Nombre del jugador", "Escribe aqui" );     
                while(jugador == null || jugador.compareTo("Escribe aqui")==0 || jugador.compareTo("")==0){
                    jugador = JOptionPane.showInputDialog(ventana, "Debes ingresar usuario","Escribe aqui");
                }
                 jugar();
                sonido.PlayJuego();
            }
       
        });
       
        //boton crear tablero
        botones[1].addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e){
                 System.out.println("Elegir laberinto");
                 elegirLaberinto();
            }
       
        });
   
   botones[2].addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e){
                 System.out.println("Leer laberinto");
                 
            }
       
        });
       
        botones[3].addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e){
                 System.out.println("SALIR");
                 System.exit(0);
            }
       
        });
       
    }
   
    public void elegirLaberinto() {
       
        int opcion;
        String op;
       
            op=JOptionPane.showInputDialog(null,
                            "       Elija un laberinto      \n"+
                            "1.- Laberinto1\n"+
                            "2.- Laberinto2\n"+
                            "3.- Laberinto3\n"+
                            "4.- Salir\n");
                    opcion=Integer.parseInt(op);

                    switch (opcion){
                        case 1: mat = Laberinto(1);{
                            jugar();
                            sonido.PlayJuego();
                           
                        }
                        break;
                        case 2: mat = Laberinto(2);
                            jugar();
                            sonido.PlayJuego();
                        break;
                        case 3: mat = Laberinto(3);
                            jugar();
                            sonido.PlayJuego();
                        break;
                        case 4: JOptionPane.showMessageDialog(null, "adios");System.exit(0);break;
                        default:JOptionPane.showMessageDialog(null, "Elija una opcion valida\n","Opcion erronea",JOptionPane.WARNING_MESSAGE);
                    }
    }
   
}


Título: Re: Ayuda por favor Proyecto java
Publicado por: Mudereded401 en 9 Junio 2021, 20:48 pm
 ¿Que contiene el archivo de texto que quieres convertir en un laberinto? ¿Con ese contenido, cómo sería  ese laberinto?

   Suerte a los que traten de ayudarlo, pues me parece un código muy largo... Yo lo hubiera resumido hasta la parte que necesitaba entender.... digo yo...


Título: Re: Ayuda por favor Proyecto java
Publicado por: rub'n en 9 Junio 2021, 23:17 pm
@Camiloher1961 doc que hay,  

Para la proxima vez intenta no solo copiar codigo, sino sabes que quieres e intentas hacer.

El bean Sonidos y Gatos no estan aqui...

Me imagino que el codigo este imprime o crea un laberinto, si lo creas ya tienes todo bueno un 95%

Arregla esos beans socio, y pasalos aqui, no busques que te hagan la tarea completa, lo del JFileChooser es fácil.

Usa Geshi tambien con Java

(https://3.bp.blogspot.com/-O6qDroNEzts/W8sWHPnV3MI/AAAAAAAADJw/mypp_PTYxbA_uKr7E-O-x0epDjSUdk3mwCK4BGAYYCw/s1600/geshi.gif)