Main.java

Categorías: CategoryJava | CategoryProgramacion

   1 package savetheearthsk;
   2 
   3 import java.awt.event.ActionEvent;
   4 import sun.audio.*;
   5 import java.awt.Button;
   6 import java.awt.FlowLayout;
   7 import java.awt.Frame;
   8 import java.awt.Label;
   9 import java.awt.event.ActionListener;
  10 import java.io.BufferedReader;
  11 import java.io.FileInputStream;
  12 import java.io.FileNotFoundException;
  13 import java.io.IOException;
  14 import java.io.InputStream;
  15 import java.io.InputStreamReader;
  16 import java.io.ObjectInputStream;
  17 import java.util.ArrayList;
  18 import java.util.HashMap;
  19 import java.util.Iterator;
  20 import java.util.Map;
  21 import java.util.logging.Level;
  22 import java.util.logging.Logger;
  23 import javax.sound.sampled.AudioFormat;
  24 import javax.sound.sampled.AudioInputStream;
  25 import javax.sound.sampled.SourceDataLine;
  26 import javax.swing.JButton;
  27 import javax.swing.JFrame;
  28 import javax.swing.JOptionPane;
  29 
  30 /**
  31  * Aquella clase que muestra e inicializa  la ventana del servidor
  32  *@author  George Sebastin Parra Macias
  33  * @author Luisa Fernanda Rueda Herrera
  34  */
  35 public class Main {
  36 
  37     private static Calaveragg1 calaveragg1;
  38     //private static Vector<EsPuntaje> espuntaje;
  39     private static Pared[] paredes;
  40     private static Pared[] paredes1;
  41     private static Pared[] paredes2;
  42     private static Pared[] paredes3;
  43     AudioFormat audioFormat;
  44     AudioInputStream audioInputStream;
  45     SourceDataLine sourceDataLine;
  46     JButton play = new JButton();
  47 
  48     /**
  49      * Aqui se define todo el codigo logico que va dentro del main
  50      * @param args
  51      * @throws FileNotFoundException
  52      * @throws IOException
  53      */
  54     public static void main(String[] args) throws FileNotFoundException, IOException {
  55         Configuracion infoconf = new Configuracion();
  56 
  57 
  58 
  59         HashMap<Integer, Puntaje> puntajes = new HashMap<Integer, Puntaje>();
  60 
  61         // Leer nombre del jugador
  62 
  63 
  64         String nombreJugador = JOptionPane.showInputDialog("digite el nombre del jugador:");
  65 
  66         // Leer archivo de puntajes
  67         /**
  68          * excepcion pera leer el archivo de puntajes
  69          */
  70         try {
  71             ObjectInputStream archivo = new ObjectInputStream(new FileInputStream("juego.dat"));
  72             puntajes = (HashMap<Integer, Puntaje>) archivo.readObject();
  73             archivo.close();
  74         } catch (ClassNotFoundException ex) {
  75             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  76         } catch (FileNotFoundException e) {
  77         } catch (IOException e) {
  78             System.err.println("error al leer archivo");
  79             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
  80 
  81         }
  82         /**
  83          * excepcion para leere el archivo de configuracion
  84          */
  85         try {
  86             ObjectInputStream config = new ObjectInputStream(new FileInputStream("configuracion.dat"));
  87             infoconf = (Configuracion) config.readObject();
  88             config.close();
  89         } catch (ClassNotFoundException ex) {
  90             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  91         } catch (FileNotFoundException f) {
  92             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, f);
  93         } catch (IOException eio) {
  94             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, eio);
  95         }
  96 
  97 
  98         final FrameConfig configuracion = new FrameConfig(infoconf);
  99 
 100 
 101 
 102 
 103         System.out.println("Puntajes");
 104         for (Iterator<Map.Entry<Integer, Puntaje>> it1 = puntajes.entrySet().iterator(); it1.hasNext();) {
 105             Map.Entry<Integer, Puntaje> p = it1.next();
 106 
 107             System.out.println(p.getValue());
 108         }
 109 
 110 
 111         Frame marco = new Frame("SaveTheEarthSk");
 112         JFrame marcoPuntajes = new JFrame("Puntajes");
 113         marcoPuntajes.setLayout(new FlowLayout());
 114         marcoPuntajes.setSize(190, 400);
 115         JButton button = new JButton(" Iniciar Juego  ");
 116         JButton config = new JButton(" Configuracion ");
 117 
 118         config.addActionListener(new ActionListener() {
 119 
 120             public void actionPerformed(ActionEvent e) {
 121                 configuracion.setSize(500, 300);
 122                 configuracion.setVisible(true);
 123             }
 124         });
 125 
 126         for (Puntaje p : puntajes.values()) {
 127             marcoPuntajes.add(new Label("" + p));
 128         }
 129 
 130         marcoPuntajes.add(button);
 131         marcoPuntajes.add(config);
 132         button.addActionListener(new EscuchaBoton(marco, marcoPuntajes));
 133 
 134         marco.setSize(1280, 1000);
 135         Lienzo lienzo = new Lienzo();
 136         Canon canon;
 137         canon = new Canon(100, 650, 100, 140);
 138         lienzo.add(canon);
 139         marco.add(lienzo);
 140         CanonCliente canoncliente = new CanonCliente(50, 650, 100, 140);
 141         lienzo.add(canoncliente);
 142         Puntaje puntaje = new Puntaje(650, 720, 180, 150, nombreJugador);
 143         lienzo.add(puntaje);
 144         Thread hiloCalaveragg1 = new Thread(calaveragg1);
 145         hiloCalaveragg1.start();
 146 
 147         marco.add(lienzo);
 148 
 149 
 150 
 151 
 152 
 153         paredes = new Pared[7];
 154         for (int i = 0; i < paredes.length; i++) {
 155             paredes[i] = new Pared(i * 200, 0, 250, 40);
 156             lienzo.add(paredes[i]);
 157 
 158         }
 159         paredes1 = new Pared[7];
 160         for (int i = 0; i < paredes.length; i++) {
 161             paredes1[i] = new Pared(i * 200, 800, 200, 50);
 162             lienzo.add(paredes1[i]);
 163 
 164 
 165         }
 166         paredes2 = new Pared[7];
 167         for (int i = 0; i < paredes.length; i++) {
 168             paredes2[i] = new Pared(i * 0, 0, 30, 800);
 169             lienzo.add(paredes2[i]);
 170 
 171 
 172         }
 173         paredes3 = new Pared[7];
 174         for (int i = 0; i < paredes.length; i++) {
 175             paredes3[i] = new Pared(i * 1030, 0, 30, 800);
 176             lienzo.add(paredes3[i]);
 177 
 178 
 179         }
 180 
 181 
 182         ArrayList<Calaveragg1> calaveras = new ArrayList<Calaveragg1>();
 183 
 184         Calaveragg1 calaveragg;
 185         for (int i = 0; i < 4; i++) {
 186             for (int j = 0; j < 12; j++) {
 187                 calaveragg = new Calaveragg1(j * 80 + 30, i * 80 + 50, 70, 70, Constantes.colores[(int) (Math.random() * 3)], paredes, paredes1, paredes2, paredes3, lienzo, calaveras, puntaje, infoconf);
 188                 calaveras.add(calaveragg);
 189                 lienzo.add(calaveragg);
 190             }
 191         }
 192 
 193 
 194 
 195 
 196         marco.addKeyListener(new EscuchaTeclas(lienzo, paredes, paredes1, paredes2, paredes3, calaveras, puntaje, canon, calaveragg1, infoconf));
 197         marco.setSize(1050, 850);
 198         marco.addWindowListener(new EscuchaVentana(puntajes, puntaje));
 199 
 200         //imprimir ventanita del juego
 201 
 202 
 203 
 204         //marco.setVisible(true);
 205         EscuchaMoverMouse escuchaMoverMouse = new EscuchaMoverMouse(canon, lienzo, calaveragg1, infoconf);
 206         lienzo.addMouseMotionListener(escuchaMoverMouse);
 207         marcoPuntajes.setVisible(true);
 208         lienzo.addMouseListener(new EscuchaMouse(lienzo, paredes, paredes1, paredes2, paredes3, calaveras, puntaje, canon, calaveragg1, infoconf, escuchaMoverMouse));
 209 
 210         // ventanita.setVisible(true);
 211 
 212 
 213         if (infoconf.sonido) {
 214             InputStream in = new FileInputStream("to_zanarkand.mid");
 215 
 216             AudioStream as = new AudioStream(in);
 217 
 218             AudioPlayer.player.start(as);
 219         }
 220 
 221         Thread threadServidor = new Thread(new Servidor(canoncliente, lienzo));
 222         threadServidor.start();
 223 
 224     }
 225 }

Java/Programas/SaveTheEarthII/Main.java (last edited 2010-10-28 12:22:20 by GeorgeParra)