Package clueless.view

Source Code of clueless.view.AccusationView$SelectCard

package clueless.view;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.text.MaskFormatter;

import clueless.controller.GameController;
import clueless.events.MakeAccusationEvent;
import clueless.events.ServerClientInfoEvent;
import clueless.main.Main;
import clueless.model.Location;
import clueless.model.Location.LocationType;
import clueless.model.Player;
import clueless.model.Character;
import clueless.model.Room;
import clueless.model.Room.RoomName;
import clueless.model.Theory;
import clueless.model.Weapon;
import clueless.model.Weapon.WeaponType;
import clueless.model.decks.Card;
import clueless.model.decks.CharacterCard;
import clueless.model.decks.CharacterDeck;
import clueless.model.decks.LocationCard;
import clueless.model.decks.LocationDeck;
import clueless.model.decks.WeaponCard;
import clueless.model.decks.WeaponDeck;

import java.net.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.io.*;


/**
* SetupView
* Game setup window
* @author G2
*/

public class AccusationView extends AbstractView {
 
  /** Handle to the game controller. */
  GameController controller = null;
 
  /** Panel to hold character card buttons */
  private JPanel characterPanel;
 
  /** Panel to hold weapon card buttons */
  private JPanel weaponPanel;
 
  /** Panel to hold location card buttons */
  private JPanel locationPanel;

  /** Currently selected weapon */
  private Weapon selectedWeapon = null;
 
  /** Currently selected room */
  private Room selectedRoom = null;
 
  /** Currently selected character */
  private Character selectedCharacter = null;
 
  /**
   * AccusationView() : Default Constructor
   */
  public AccusationView(GameController controller)  {
    this.controller = controller;
    controller.addView(this);

    jfrm = new JFrame("Make an Accusation");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(1200, 600);
    jfrm.setResizable(false);
    jfrm.setAlwaysOnTop(true);
    jfrm.setLocation((screen.width - jfrm.getWidth())/2,
        (screen.height - jfrm.getHeight())/2);
   
    //Panel for Character cards
    characterPanel = new JPanel();
   
    //Panel for Weapon cards
    weaponPanel = new JPanel();
   
    //Panel for Location cards
    locationPanel = new JPanel();
   
    JButton btnMakeAccusation = new JButton();
    btnMakeAccusation.setText("Make Accusation");
    btnMakeAccusation.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        //Check that 1 of each card is selected
        if(selectedCharacter == null || selectedWeapon == null || selectedRoom == null)
        {
          JOptionPane.showMessageDialog(jfrm, "You must select one of each card type in order to make an accusation (Character, Weapon, Room).");
          return;
        }
        else
        {
          submitAccusation();
          jfrm.dispose();
        }
      }
    });
   
    JButton btnCancel = new JButton();
    btnCancel.setText("Cancel");
    btnCancel.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        jfrm.dispose();
      }
    });
   
    JPanel buttonPanel = new JPanel();
    buttonPanel.setPreferredSize(new Dimension(1200, 100));
    buttonPanel.add(btnMakeAccusation);
    buttonPanel.add(btnCancel);
    jfrm.add(characterPanel);
    jfrm.add(weaponPanel);
    jfrm.add(locationPanel);
    jfrm.add(buttonPanel);
    return;
  }


  /**
   * displayCards() : Allows user to make an accusation by selecting cards
   */
  public void displayCards()
  {

    //If repainting, clear each panel first
    characterPanel.removeAll();
    weaponPanel.removeAll();
    locationPanel.removeAll();
   
    //All possible types of cards
    clueless.model.Character.CharacterName[] characterTypes = clueless.model.Character.CharacterName.values();
    WeaponType[] weaponTypes = WeaponType.values();
    RoomName[] roomNames = RoomName.values();
   
    //Loop through hand to display cards
    for(int i=0; i<weaponTypes.length; i++)
    {
      BufferedImage buttonIcon = null;
      URL resource = null;
      /** Load respective images based on card type */
      switch(weaponTypes[i])
      {
        case CANDLESTICK:
          resource = Main.class.getResource("/clueless/main/resources/weapons/Candlestick_card.gif");
          break;
        case KNIFE:
          resource = Main.class.getResource("/clueless/main/resources/weapons/Knife_card.gif");
          break;
        case LEAD_PIPE:
          resource = Main.class.getResource("/clueless/main/resources/weapons/Pipe_card.gif");
          break;
        case REVOLVER:
          resource = Main.class.getResource("/clueless/main/resources/weapons/Revolver_card.gif");
          break;
        case ROPE:
          resource = Main.class.getResource("/clueless/main/resources/weapons/Rope_card.gif");
          break;
        case WRENCH:
          resource = Main.class.getResource("/clueless/main/resources/weapons/Wrench_card.gif");
          break;
      }
     
      /** get URL to image */
      try {
        buttonIcon = ImageIO.read(resource);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.print(e.toString());
      }
     
      //Scale it to the UI size
      ImageIcon icon = new ImageIcon(buttonIcon.getScaledInstance(96, 150, 0));
     
      //Instantiate image button
      WeaponButton button = new WeaponButton(icon, weaponTypes[i]);
      button.setBorder(new LineBorder(Color.BLACK, 5));
      button.setBorderPainted(true);
      //add actionEvent to Button
      button.addActionListener(new SelectCard());
     
      //add to weapon panel
      weaponPanel.add(button);
     
    }//for weapon types
    for(int i=0; i<characterTypes.length; i++)
    {
      BufferedImage buttonIcon = null;
      URL resource = null;
      /** Load respective images based on card type */
      switch(characterTypes[i])
      {
        case COL_MUSTARD:
          resource = Main.class.getResource("/clueless/main/resources/characters/ColMustard.gif");
          break;
        case MISS_SCARLET:
          resource = Main.class.getResource("/clueless/main/resources/characters/MissScarlett.gif");
          break;
        case MR_GREEN:
          resource = Main.class.getResource("/clueless/main/resources/characters/MrGreen.gif");
          break;
        case MRS_PEACOCK:
          resource = Main.class.getResource("/clueless/main/resources/characters/MissPeacock.gif");
          break;
        case MRS_WHITE:
          resource = Main.class.getResource("/clueless/main/resources/characters/MrsWhite.gif");
          break;
        case PROF_PLUM:
          resource = Main.class.getResource("/clueless/main/resources/characters/ProfPlum.gif");
          break;
      }
      /** get URL to image */
      try {
        buttonIcon = ImageIO.read(resource);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.print(e.toString());
      }
     
      //Scale it to the UI size
      ImageIcon icon = new ImageIcon(buttonIcon.getScaledInstance(96, 150, 0));
     
      //Instantiate image button
      CharacterButton button = new CharacterButton(icon, characterTypes[i]);
      button.setBorder(new LineBorder(Color.BLACK, 5));
      button.setBorderPainted(true);
      //add actionEvent to Button
      button.addActionListener(new SelectCard());
     
      //add to weapon panel
      characterPanel.add(button);
     
    }   
    for(int i=0; i<roomNames.length; i++)
    {
      BufferedImage buttonIcon = null;
      URL resource = null;
      /** Load respective images based on card type */
      switch(roomNames[i])
      {
        case BALLROOM:
          resource = Main.class.getResource("/clueless/main/resources/rooms/Ballroom.gif");
          break;
        case BILLIARD_ROOM:
          resource = Main.class.getResource("/clueless/main/resources/rooms/BilliardRoom.gif");
          break;
        case CONSERVATORY:
          resource = Main.class.getResource("/clueless/main/resources/rooms/Conservatory.gif");
          break;
        case DINING_ROOM:
          resource = Main.class.getResource("/clueless/main/resources/rooms/DiningRoom.gif");
          break;
        case HALL:
          resource = Main.class.getResource("/clueless/main/resources/rooms/Hall.gif");
          break;
        case KITCHEN:
          resource = Main.class.getResource("/clueless/main/resources/rooms/Kitchen.gif");
          break;
        case LIBRARY:
          resource = Main.class.getResource("/clueless/main/resources/rooms/Library.gif");
          break;
        case LOUNGE:
          resource = Main.class.getResource("/clueless/main/resources/rooms/Lounge.gif");
          break;
        case STUDY:
          resource = Main.class.getResource("/clueless/main/resources/rooms/Study.gif");
          break;
      }//location card type swtich
     
      /** get URL to image */
      try {
        buttonIcon = ImageIO.read(resource);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.print(e.toString());
      }
     
      //Scale it to the UI size
      ImageIcon icon = new ImageIcon(buttonIcon.getScaledInstance(96, 150, 0));
     
      //Instantiate image button
      RoomButton button = new RoomButton(icon, roomNames[i]);
      button.setBorder(new LineBorder(Color.BLACK, 5));
      button.setBorderPainted(true);
      //add actionEvent to Button
      button.addActionListener(new SelectCard());
     
      //add to weapon panel
      locationPanel.add(button);
     
    }//location for
    jfrm.setVisible(true);
  }
 
 
  private class SelectCard implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent ae) {
      //Get panel button that fired event is part of
      JButton button = (JButton)ae.getSource();
      JPanel parent = (JPanel)button.getParent();
     
      //Disable selected card, enable all others
      for(int i = 0; i<parent.getComponentCount(); i++)
      {
        JButton buttonToRemoveBorder = (JButton)parent.getComponent(i);
        //LineBorder border = new LineBorder(null, i, false);
        buttonToRemoveBorder.setBorder(new LineBorder(Color.BLACK, 5));
        //buttonToRemoveBorder.setBorderPainted(false);
        buttonToRemoveBorder.repaint();
       
      }
      LineBorder border = new LineBorder(Color.GREEN, 5);
      button.setBorder(border);
      button.setBorderPainted(true);
      button.repaint();
     
      //Store what card is selected
      if(button instanceof CharacterButton)
      {
        CharacterButton character = (CharacterButton)button;
        selectedCharacter = new Character(character.getType());
      }
      else if(button instanceof WeaponButton)
      {
        WeaponButton weapon = (WeaponButton)button;
        selectedWeapon = new Weapon(weapon.getType());
      }
      else if(button instanceof RoomButton)
      {
        RoomButton location = (RoomButton)button;
        selectedRoom = new Room(location.getType());
      }
    }
   
   
  }
 
  private void submitAccusation()
  {
    Theory theory;
    //TODO:  Do we really need to pass which player suggested it or can the server deduce that?
    theory = new Theory(Theory.Type.ACCUSATION, selectedWeapon, selectedCharacter, selectedRoom, controller.getCurrentPlayer());
    //TODO: CC - check if is server, then send the MakeAccusation message?
    controller.fireMakeAccusationEvent(new MakeAccusationEvent(this, theory));
  }
}
TOP

Related Classes of clueless.view.AccusationView$SelectCard

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.