VENTANAS.cargaArchivo.java Source code

Java tutorial

Introduction

Here is the source code for VENTANAS.cargaArchivo.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package VENTANAS;

import TABLERO.*;
import OTROS.listaDiccionario;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 *
 * @author Ottoniel
 */
public class cargaArchivo extends javax.swing.JFrame {

    /**
     * Creates new form cargaArchivo
     */
    public cargaArchivo() {
        initComponents();

    }

    tablero tb = new tablero();
    listaDiccionario listadoP = new listaDiccionario();

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("CARGAR TABLERO");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("SIGUIENTE");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(24, 24, 24)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 154,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32).addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 163,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(27, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap(57, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(55, 55, 55)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        JFileChooser jf = new JFileChooser();
        jf.showOpenDialog(this);
        File f = jf.getSelectedFile();
        try {
            System.out.println(f.getAbsolutePath());
            leerXML(f.getAbsolutePath());
        } catch (JDOMException ex) {
            Logger.getLogger(cargaArchivo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(cargaArchivo.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.jButton2.setEnabled(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        agregarJugadores juegan = new agregarJugadores(tb, listadoP);
        juegan.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    private void leerXML(String path) throws JDOMException, IOException {
        SAXBuilder parser = new SAXBuilder();
        File archivo = new File(path);
        Document documento = (Document) parser.build(archivo);
        Element raiz = documento.getRootElement();
        int tamanio = Integer.parseInt(raiz.getChildTextTrim("dimension"));
        tb.crearMatriz(tamanio);

        Element dobles = raiz.getChild("dobles");

        java.util.List<Element> listaDobles = dobles.getChildren("casilla");

        for (int i = 0; i < listaDobles.size(); i++) {
            System.out.println("Punto de verificacion en primer for");
            Element doble = (Element) listaDobles.get(i);
            int x = Integer.parseInt(doble.getChildTextTrim("x"));
            int y = Integer.parseInt(doble.getChildTextTrim("y"));
            tb.modificarNodo(x, y, "Puntos X 2");

        }
        Element triples = (Element) raiz.getChild("triples");
        java.util.List<Element> listaTriples = triples.getChildren("casilla");
        for (int i = 0; i < listaTriples.size(); i++) {
            Element doble = (Element) listaTriples.get(i);
            int x = Integer.parseInt(doble.getChildTextTrim("x"));
            int y = Integer.parseInt(doble.getChildTextTrim("y"));
            tb.modificarNodo(x, y, "Puntos X 3");

        }
        Element diccio = (Element) raiz.getChild("diccionario");
        java.util.List<Element> listaPalabras = diccio.getChildren("palabra");
        for (int i = 0; i < listaPalabras.size(); i++) {

            String pa = listaPalabras.get(i).getTextTrim();
            listadoP.agregarPalabra(pa);
        }

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration//GEN-END:variables
}