Guest User

Untitled

a guest
Nov 11th, 2013
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.36 KB | None | 0 0
  1. package gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14.  
  15. import data.Administrador;
  16.  
  17. public class GUIJuego extends JFrame{
  18.  
  19.    
  20.     private JLabel labelConstrucciones = new JLabel("Construcciones");
  21.     private JButton botonConstruirEscuela = new JButton("Escuela");
  22.     private JButton botonConstruirComisaria = new JButton("Comisaria");
  23.     private JButton botonConstruirCuartel = new JButton("Cuartel de Bomberos");
  24.    
  25.     private JButton botonConstruirArbol = new JButton("Arbol");
  26.    
  27.     private JButton botonConstruirCasa = new JButton("Casa");
  28.     private JButton botonConstruirComercio = new JButton("Comercio");
  29.     private JButton botonConstruirIndustria = new JButton("Industria");
  30.    
  31.     private JButton botonEliminarConstruccion = new JButton("Eliminar");
  32.    
  33.     private Tile[][] mapaTiles = new Tile[25][25];
  34.    
  35.     private JLabel arcaLabel;
  36.     private JLabel puntosBellezaLabel;
  37.     private JLabel habitantesLabel;
  38.    
  39.     private int tipoConstruccion = -1;
  40.    
  41.     //private Administrador administrador;
  42.    
  43.     public GUIJuego(){
  44.        
  45.         JPanel panelConstruccion = new JPanel(new GridLayout(8,1));
  46.         JPanel panelDatosCiudad = new JPanel(new GridLayout(1,4));
  47.         JPanel panelMapa = new JPanel(new GridLayout(25,25));
  48.  
  49.         add(panelConstruccion, BorderLayout.WEST);
  50.         panelConstruccion.add(botonConstruirEscuela);
  51.         panelConstruccion.add(botonConstruirComisaria);
  52.         panelConstruccion.add(botonConstruirCuartel);
  53.         panelConstruccion.add(botonConstruirArbol);
  54.         panelConstruccion.add(botonConstruirCasa);
  55.         panelConstruccion.add(botonConstruirComercio);
  56.         panelConstruccion.add(botonConstruirIndustria);
  57.         panelConstruccion.add(botonEliminarConstruccion);
  58.        
  59.         // #### ActionListeners de Botones Costrucción
  60.        
  61.         botonEliminarConstruccion.addActionListener(new ActionListener(){
  62.             public void actionPerformed(ActionEvent ev){
  63.                 setTipoConstruccion(0);
  64.             }
  65.         });
  66.        
  67.         botonConstruirComisaria.addActionListener(new ActionListener(){
  68.             public void actionPerformed(ActionEvent ev){
  69.                 setTipoConstruccion(1);
  70.             }
  71.         });
  72.        
  73.         botonConstruirCuartel.addActionListener(new ActionListener(){
  74.             public void actionPerformed(ActionEvent ev){
  75.                 setTipoConstruccion(2);
  76.             }
  77.         });
  78.        
  79.         botonConstruirEscuela.addActionListener(new ActionListener(){
  80.             public void actionPerformed(ActionEvent ev){
  81.                 setTipoConstruccion(3);
  82.             }
  83.         });
  84.        
  85.         botonConstruirArbol.addActionListener(new ActionListener(){
  86.             public void actionPerformed(ActionEvent ev){
  87.                 setTipoConstruccion(4);
  88.             }
  89.         });
  90.        
  91.         botonConstruirCasa.addActionListener(new ActionListener(){
  92.             public void actionPerformed(ActionEvent ev){
  93.                 setTipoConstruccion(5);
  94.             }
  95.         });
  96.        
  97.         botonConstruirComercio.addActionListener(new ActionListener(){
  98.             public void actionPerformed(ActionEvent ev){
  99.                 setTipoConstruccion(6);
  100.             }
  101.         });
  102.        
  103.         botonConstruirIndustria.addActionListener(new ActionListener(){
  104.             public void actionPerformed(ActionEvent ev){
  105.                 setTipoConstruccion(7);
  106.             }
  107.         });
  108.        
  109.         // ### Fin ActionListeners de Botones Construcción
  110.        
  111.         add(panelDatosCiudad, BorderLayout.NORTH);
  112.         panelDatosCiudad.add(labelConstrucciones);
  113.         panelDatosCiudad.add(arcaLabel);
  114.         panelDatosCiudad.add(puntosBellezaLabel);
  115.         panelDatosCiudad.add(habitantesLabel);
  116.        
  117.         add(panelMapa, BorderLayout.CENTER);
  118.        
  119.         cargarTiles(panelMapa);
  120.        
  121.         //administrador = new Administrador();
  122.        
  123.         //arcaLabel.setText("Arca: " + administrador.getCiudad().getArca());
  124.         //puntosBellezaLabel.setText("Puntos de Belleza: " + administrador.getCiudad().getPuntosBelleza());
  125.         //habitantesLabel.setText("Habitantes: " + administrador.getCiudad().getCantidadHabitantes() + " / " +  administrador.getCiudad().getCantidadHabitantesDisponibles());
  126.        
  127.     }
  128.    
  129.     private void cargarTiles(JPanel panel){
  130.        
  131.         for(int i = 0; i < 25; i++){
  132.             for(int j = 0; j < 25; j++){
  133.                 mapaTiles[i][j] = new Tile(i, j, 0);
  134.                 asignarTile(mapaTiles[i][j], panel);
  135.                 setMyTile(mapaTiles[i][j]);
  136.             }
  137.         }
  138.        
  139.     }
  140.    
  141.     private void asignarTile(Tile tile, JPanel panel){
  142.  
  143.         switch(tile.getTipo()){
  144.         case 0:
  145.             setTileImage("pasto.png", tile, panel);
  146.             break;
  147.         case 1:
  148.             setTileImage("comisaria.png", tile, panel);
  149.             break;
  150.         case 2:
  151.             setTileImage("cuartel.png", tile, panel);
  152.             break;
  153.         case 3:
  154.             setTileImage("escuela.png", tile, panel);
  155.             break;
  156.         case 4:
  157.             setTileImage("arbol.png", tile, panel);
  158.             break;
  159.         case 5:
  160.             setTileImage("casa.png", tile, panel);
  161.             break;
  162.         case 6:
  163.             setTileImage("comercio.png", tile, panel);
  164.             break;
  165.         case 7:
  166.             setTileImage("industria.png", tile, panel);
  167.             break;
  168.         default:
  169.             setTileImage("pasto.png", tile, panel);
  170.             break;
  171.         }
  172.            
  173.     }
  174.    
  175.     private void setTileImage(String s, Tile tile, JPanel panel){
  176.        
  177.         try{
  178.            
  179.             tile.setIcon(new StretchIcon(s, false));
  180.             panel.add(tile);
  181.            
  182.         }catch(Exception e){
  183.            
  184.             e.printStackTrace();
  185.            
  186.         }
  187.        
  188.        
  189.     }
  190.  
  191.     private int getTipoConstruccion() {
  192.         return tipoConstruccion;
  193.     }
  194.  
  195.     private void setTipoConstruccion(int tipoConstruccion) {
  196.         this.tipoConstruccion = tipoConstruccion;
  197.     }
  198.    
  199.     private void setMyTile(Tile t){
  200.        
  201.         t.addMouseListener(new MouseListener(){
  202.  
  203.             @Override
  204.             public void mouseClicked(MouseEvent me) {
  205.                
  206.                 Tile tile = ((Tile)me.getSource());
  207.                
  208.                 if((getTipoConstruccion() > 0 || getTipoConstruccion() <= 7) && tile.getTipo() == 0){
  209.                    
  210.                     tile.setTipo(getTipoConstruccion());
  211.                     asignarTile(tile, (JPanel) tile.getParent());
  212.                     setTipoConstruccion(-1);
  213.                    
  214.                 } else if( getTipoConstruccion() == 0 && tile.getTipo() != 0){
  215.                    
  216.                     tile.setTipo(getTipoConstruccion());
  217.                     asignarTile(tile, (JPanel) tile.getParent());
  218.                     setTipoConstruccion(-1);
  219.                    
  220.                 }
  221.         }
  222.  
  223.             @Override
  224.             public void mouseEntered(MouseEvent e) {
  225.                 // TODO Auto-generated method stub
  226.                
  227.             }
  228.  
  229.             @Override
  230.             public void mouseExited(MouseEvent e) {
  231.                 // TODO Auto-generated method stub
  232.                
  233.             }
  234.  
  235.             @Override
  236.             public void mousePressed(MouseEvent e) {
  237.                 // TODO Auto-generated method stub
  238.                
  239.             }
  240.  
  241.             @Override
  242.             public void mouseReleased(MouseEvent e) {
  243.                 // TODO Auto-generated method stub
  244.                
  245.             }});
  246.        
  247.     }
  248.    
  249. }
Advertisement
Add Comment
Please, Sign In to add comment