edd.practica1.Principal.java Source code

Java tutorial

Introduction

Here is the source code for edd.practica1.Principal.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 edd.practica1;

    import java.awt.Button;
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    import javax.swing.JFileChooser;
    import org.jdom2.Document; // |
    import org.jdom2.Element; // |\ Libreras
    import org.jdom2.JDOMException; // |/ JDOM
    import org.jdom2.input.SAXBuilder; // |
    import java.io.File;
    import java.io.IOException;
    import java.util.List;

    /**
     *
     * @author Jorge Espina
     */

    public class Principal extends javax.swing.JFrame {
        File archivos;
        JFileChooser seleccionado = new JFileChooser();
    int A=0,E=0,O=0,I=0,S=0,N=0,L=0,R=0,U=0,T=0,D=0,G=0,C=0,B=0,M=0,P=0,H=0,F=0,V=0,Y=0,Q=0,J=0,=0,X=0,Z=0;
        int NumeroTotalLetras = 0;
        int num1 = 97;
        int num2 = 122;

        int Dimension;
        //ListaSimpleFichasJugador ListaSimpleFichasJugador = new ListaSimpleFichasJugador();
        public static ListasSimplesDiccionario ListasSimplesDiccionario = new ListasSimplesDiccionario();
        public static ColaFichasTotal ColaFichasTotal = new ColaFichasTotal();

        public Principal() {
            initComponents();
            setLocationRelativeTo(null);
            getContentPane().setBackground(new java.awt.Color(255, 255, 255));
        }

        /**
         * 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() {

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

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Desig Preview");
            setBackground(new java.awt.Color(255, 255, 255));

            jPanel2.setBackground(new java.awt.Color(255, 255, 255));

            jButton1.setBackground(new java.awt.Color(255, 255, 255));
            jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/Jugar.png"))); // NOI18N
            jButton1.setText("Jugar");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });

            jButton2.setBackground(new java.awt.Color(255, 255, 255));
            jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/LeerArchivo.png"))); // NOI18N
            jButton2.setText("Leer Archivo");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(jPanel2Layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addGap(52, 52, 52)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 245,
                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                            .addComponent(jButton1).addGap(54, 54, 54)));
            jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup().addGap(68, 68, 68)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jButton2).addComponent(jButton1))
                            .addContainerGap(68, Short.MAX_VALUE)));

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                    javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, Short.MAX_VALUE)));
            layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                    jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                    Short.MAX_VALUE));

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

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
            this.setVisible(false);
            //Tablero Tablero = new Tablero();
            //Tablero.setVisible(true);
            //        IngresoJugadores Jugadores= new IngresoJugadores();
            //        Jugadores.setVisible(true);
            MatrizOrtogonal MatrizOrtogonal = new MatrizOrtogonal();
            MatrizOrtogonal.Llenar(Dimension, Dimension);
            //MatrizOrtogonal.Graficar();
            MatrizOrtogonal.RecorrerMatrizGenerar();
        }//GEN-LAST:event_jButton1ActionPerformed

        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
            if (seleccionado.showDialog(null, "Abrir Archivo") == JFileChooser.APPROVE_OPTION) {
                archivos = seleccionado.getSelectedFile();

                SAXBuilder builder = new SAXBuilder();

                try {
                    Document document = (Document) builder.build(archivos);
                    Element rootNode = document.getRootElement();

                    Element Raiz = document.getRootElement();

                    List listpri = Raiz.getChildren("dimension");

                    for (int i = 0; i < listpri.size(); i++) {
                        Element camp = (Element) Raiz.getChildren("dimension").get(i);
                        String Dimensiones = camp.getTextTrim();
                        Dimension = Integer.parseInt(Dimensiones);
                        System.out.println("Dimension :" + Dimensiones);

                    }

                    List list = rootNode.getChildren("dobles");

                    for (int i = 0; i < list.size(); i++) {
                        Element scrabble = (Element) list.get(i);
                        List lista_dobles = scrabble.getChildren();
                        System.out.println("\tdobles");
                        for (int j = 0; j < lista_dobles.size(); j++) {
                            Element dobles = (Element) lista_dobles.get(j);
                            String x = dobles.getChildTextTrim("x");
                            String y = dobles.getChildTextTrim("y");
                            System.out.println("\t" + x + "\t\t" + y + "\t");
                        }
                    }

                    List listtri = rootNode.getChildren("triples");

                    for (int i = 0; i < listtri.size(); i++) {
                        Element scrabble = (Element) listtri.get(i);
                        List lista_triples = scrabble.getChildren();
                        System.out.println("\ttriples");
                        for (int j = 0; j < lista_triples.size(); j++) {
                            Element triples = (Element) lista_triples.get(j);
                            String x = triples.getChildTextTrim("x");
                            String y = triples.getChildTextTrim("y");
                            System.out.println("\t" + x + "\t\t" + y + "\t");
                        }
                    }

                    for (int i = 0; i < rootNode.getChildren("diccionario").size(); i++) {
                        Element listd = (Element) rootNode.getChildren("diccionario").get(i);
                        for (int j = 0; j < listd.getChildren().size(); j++) {
                            Element diccionario = (Element) listd.getChildren("palabra").get(j);
                            String palabra = (diccionario.getTextTrim());
                            System.out.println("\t" + palabra + "\t");
                            ListasSimplesDiccionario.IngresarNodo(palabra);
                            //ListaSimpleFichasJugador.VerLista();
                        }
                    }
                    GenerarFichasTotales();

                } catch (IOException | JDOMException io) {
                    System.out.println(io.getMessage());
                }

            }

        }//GEN-LAST:event_jButton2ActionPerformed

public void GenerarFichasTotales(){
                while(NumeroTotalLetras < 96){
      int numAleatorio = (int)Math.floor(Math.random()*(num2 -num1)+num1);
                //System.out.println("la letra generada es: " + (char)numAleatorio);
                if ((A<12) && ((char)numAleatorio==97)){
                    A+=1;
                    ColaFichasTotal.InsertarCola("A");
                    NumeroTotalLetras++;
                }else if ((E<12) && ((char)numAleatorio==101)){
                    E+=1;
                    ColaFichasTotal.InsertarCola("E");
                    NumeroTotalLetras++;
                }else if ((O<9) && ((char)numAleatorio==111)){
                    O+=1;
                    ColaFichasTotal.InsertarCola("O");
                    NumeroTotalLetras++;
                }else if ((I<6) && ((char)numAleatorio==105)){
                    I+=1;
                    ColaFichasTotal.InsertarCola("I");
                    NumeroTotalLetras++;
                }else if ((S < 6) && ((char)numAleatorio==115)){
                    S+=1;
                    ColaFichasTotal.InsertarCola("S");
                    NumeroTotalLetras++;
                }else if ((N<5) && ((char)numAleatorio==110)){
                    N+=1;
                    ColaFichasTotal.InsertarCola("N");
                    NumeroTotalLetras++;
                }else if ((L<4) && ((char)numAleatorio==108)){
                    L+=1;
                    ColaFichasTotal.InsertarCola("L");
                    NumeroTotalLetras++;
                }else if ((R<5) && ((char)numAleatorio==114)){
                    R+=1;
                    ColaFichasTotal.InsertarCola("R");
                    NumeroTotalLetras++;
                }else if ((U<5) && ((char)numAleatorio==117)){
                    U+=1;
                    ColaFichasTotal.InsertarCola("U");
                    NumeroTotalLetras++;
                }else if ((T<4) && ((char)numAleatorio==116)){
                    T+=1;
                    ColaFichasTotal.InsertarCola("T");
                    NumeroTotalLetras++;
                }else if ((D<5) && ((char)numAleatorio==100)){
                    D+=1;
                    ColaFichasTotal.InsertarCola("D");
                    NumeroTotalLetras++;
                }else if ((G<2) && ((char)numAleatorio==103)){
                    G+=1;
                    ColaFichasTotal.InsertarCola("G");
                    NumeroTotalLetras++;
                }else if ((C<4) && ((char)numAleatorio==99)){
                    C+=1;
                    ColaFichasTotal.InsertarCola("C");
                    NumeroTotalLetras++;
                }else if ((B<2) && ((char)numAleatorio==98)){
                    B+=1;
                    ColaFichasTotal.InsertarCola("B");
                    NumeroTotalLetras++;
                }else if ((M<2) && ((char)numAleatorio==109)){
                    M+=1;
                    ColaFichasTotal.InsertarCola("M");
                    NumeroTotalLetras++;
                }else if ((P<2) && ((char)numAleatorio==112)){
                    P+=1;
                    ColaFichasTotal.InsertarCola("P");
                    NumeroTotalLetras++;
                }else if ((H<2) && ((char)numAleatorio==104)){
                    H+=1;
                    NumeroTotalLetras++;
                    ColaFichasTotal.InsertarCola("H");
                }else if ((F<1) && (NumeroTotalLetras==87)){
                    F+=1;
                    ColaFichasTotal.InsertarCola("F");
                    NumeroTotalLetras++;
                }else if ((V<1) && (NumeroTotalLetras==88)){
                    V+=1;
                    ColaFichasTotal.InsertarCola("V");
                    NumeroTotalLetras++;
                }else if ((Y<1) && (NumeroTotalLetras==89)){
                    Y+=1;
                    ColaFichasTotal.InsertarCola("Y");
                    NumeroTotalLetras++;
                }else if ((Q<1) && (NumeroTotalLetras==90)){
                    Q+=1;
                    ColaFichasTotal.InsertarCola("Q");
                    NumeroTotalLetras++;
                }else if ((J<1) && (NumeroTotalLetras==91)){
                    J+=1;
                    ColaFichasTotal.InsertarCola("J");
                    NumeroTotalLetras++;
                }else if ((<1) && (NumeroTotalLetras==94)){
                    +=1;
                    ColaFichasTotal.InsertarCola("");
                    NumeroTotalLetras++;
                }else if ((X<1) && (NumeroTotalLetras==93)){
                    X+=1;
                    ColaFichasTotal.InsertarCola("X");
                    NumeroTotalLetras++;
                }else if ((Z<1) && (NumeroTotalLetras==92)){
                    Z+=1;
                    ColaFichasTotal.InsertarCola("Z");
                    NumeroTotalLetras++;
                }else if (NumeroTotalLetras == 95){
                 System.out.println("la letra generadas A : " + A);
                 System.out.println("la letra generadas E : " + E);
                 System.out.println("la letra generadas O : " + O);
                 System.out.println("la letra generadas I : " + I);
                 System.out.println("la letra generadas S : " + S);
                 System.out.println("la letra generadas N : " + N);
                 System.out.println("la letra generadas L : " + L);
                 System.out.println("la letra generadas R : " + R);
                 System.out.println("la letra generadas U : " + U);
                 System.out.println("la letra generadas T : " + T);
                 System.out.println("la letra generadas D : " + D);
                 System.out.println("la letra generadas G : " + G);
                 System.out.println("la letra generadas C : " + C);
                 System.out.println("la letra generadas B : " + B);
                 System.out.println("la letra generadas M : " + M);
                 System.out.println("la letra generadas P : " + P);
                 System.out.println("la letra generadas H : " + H);
                 System.out.println("la letra generadas F : " + F);
                 System.out.println("la letra generadas V : " + V);
                 System.out.println("la letra generadas Y : " + Y);
                 System.out.println("la letra generadas Q : " + Q);
                 System.out.println("la letra generadas J : " + J);
                 System.out.println("la letra generadas  : " + );
                 System.out.println("la letra generadas X : " + X);
                 System.out.println("la letra generadas Z : " + Z);            
                 //System.out.println(ColaFichasTotal.Imprimir());
                 break;
                }
                }
               
}

        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null,
                        ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null,
                        ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null,
                        ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null,
                        ex);
            }
            //</editor-fold>

            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Principal().setVisible(true);
                }
            });
        }

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