Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gui;
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import data.Administrador;
- public class GUIJuego extends JFrame{
- private JLabel labelConstrucciones = new JLabel("Construcciones");
- private JButton botonConstruirEscuela = new JButton("Escuela");
- private JButton botonConstruirComisaria = new JButton("Comisaria");
- private JButton botonConstruirCuartel = new JButton("Cuartel de Bomberos");
- private JButton botonConstruirArbol = new JButton("Arbol");
- private JButton botonConstruirCasa = new JButton("Casa");
- private JButton botonConstruirComercio = new JButton("Comercio");
- private JButton botonConstruirIndustria = new JButton("Industria");
- private JButton botonEliminarConstruccion = new JButton("Eliminar");
- private Tile[][] mapaTiles = new Tile[25][25];
- private JLabel arcaLabel;
- private JLabel puntosBellezaLabel;
- private JLabel habitantesLabel;
- private int tipoConstruccion = -1;
- //private Administrador administrador;
- public GUIJuego(){
- JPanel panelConstruccion = new JPanel(new GridLayout(8,1));
- JPanel panelDatosCiudad = new JPanel(new GridLayout(1,4));
- JPanel panelMapa = new JPanel(new GridLayout(25,25));
- add(panelConstruccion, BorderLayout.WEST);
- panelConstruccion.add(botonConstruirEscuela);
- panelConstruccion.add(botonConstruirComisaria);
- panelConstruccion.add(botonConstruirCuartel);
- panelConstruccion.add(botonConstruirArbol);
- panelConstruccion.add(botonConstruirCasa);
- panelConstruccion.add(botonConstruirComercio);
- panelConstruccion.add(botonConstruirIndustria);
- panelConstruccion.add(botonEliminarConstruccion);
- // #### ActionListeners de Botones Costrucción
- botonEliminarConstruccion.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ev){
- setTipoConstruccion(0);
- }
- });
- botonConstruirComisaria.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ev){
- setTipoConstruccion(1);
- }
- });
- botonConstruirCuartel.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ev){
- setTipoConstruccion(2);
- }
- });
- botonConstruirEscuela.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ev){
- setTipoConstruccion(3);
- }
- });
- botonConstruirArbol.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ev){
- setTipoConstruccion(4);
- }
- });
- botonConstruirCasa.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ev){
- setTipoConstruccion(5);
- }
- });
- botonConstruirComercio.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ev){
- setTipoConstruccion(6);
- }
- });
- botonConstruirIndustria.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ev){
- setTipoConstruccion(7);
- }
- });
- // ### Fin ActionListeners de Botones Construcción
- add(panelDatosCiudad, BorderLayout.NORTH);
- panelDatosCiudad.add(labelConstrucciones);
- panelDatosCiudad.add(arcaLabel);
- panelDatosCiudad.add(puntosBellezaLabel);
- panelDatosCiudad.add(habitantesLabel);
- add(panelMapa, BorderLayout.CENTER);
- cargarTiles(panelMapa);
- //administrador = new Administrador();
- //arcaLabel.setText("Arca: " + administrador.getCiudad().getArca());
- //puntosBellezaLabel.setText("Puntos de Belleza: " + administrador.getCiudad().getPuntosBelleza());
- //habitantesLabel.setText("Habitantes: " + administrador.getCiudad().getCantidadHabitantes() + " / " + administrador.getCiudad().getCantidadHabitantesDisponibles());
- }
- private void cargarTiles(JPanel panel){
- for(int i = 0; i < 25; i++){
- for(int j = 0; j < 25; j++){
- mapaTiles[i][j] = new Tile(i, j, 0);
- asignarTile(mapaTiles[i][j], panel);
- setMyTile(mapaTiles[i][j]);
- }
- }
- }
- private void asignarTile(Tile tile, JPanel panel){
- switch(tile.getTipo()){
- case 0:
- setTileImage("pasto.png", tile, panel);
- break;
- case 1:
- setTileImage("comisaria.png", tile, panel);
- break;
- case 2:
- setTileImage("cuartel.png", tile, panel);
- break;
- case 3:
- setTileImage("escuela.png", tile, panel);
- break;
- case 4:
- setTileImage("arbol.png", tile, panel);
- break;
- case 5:
- setTileImage("casa.png", tile, panel);
- break;
- case 6:
- setTileImage("comercio.png", tile, panel);
- break;
- case 7:
- setTileImage("industria.png", tile, panel);
- break;
- default:
- setTileImage("pasto.png", tile, panel);
- break;
- }
- }
- private void setTileImage(String s, Tile tile, JPanel panel){
- try{
- tile.setIcon(new StretchIcon(s, false));
- panel.add(tile);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- private int getTipoConstruccion() {
- return tipoConstruccion;
- }
- private void setTipoConstruccion(int tipoConstruccion) {
- this.tipoConstruccion = tipoConstruccion;
- }
- private void setMyTile(Tile t){
- t.addMouseListener(new MouseListener(){
- @Override
- public void mouseClicked(MouseEvent me) {
- Tile tile = ((Tile)me.getSource());
- if((getTipoConstruccion() > 0 || getTipoConstruccion() <= 7) && tile.getTipo() == 0){
- tile.setTipo(getTipoConstruccion());
- asignarTile(tile, (JPanel) tile.getParent());
- setTipoConstruccion(-1);
- } else if( getTipoConstruccion() == 0 && tile.getTipo() != 0){
- tile.setTipo(getTipoConstruccion());
- asignarTile(tile, (JPanel) tile.getParent());
- setTipoConstruccion(-1);
- }
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- // TODO Auto-generated method stub
- }});
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment