Package clueless.view

Source Code of clueless.view.UserHandView

package clueless.view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import clueless.controller.GameController;
import clueless.events.MessageEvent;
import clueless.events.MessageEventListener;
import clueless.main.Main;
import clueless.messaging.GameInitializationMessage;
import clueless.model.decks.Card;
import clueless.model.decks.CharacterCard;
import clueless.model.decks.LocationCard;
import clueless.model.decks.WeaponCard;


/**
* View for displaying the hand of cards dealt to the user.
*
* @author T
*/
//TODO: implement GameInitializationEventListener
public class UserHandView extends AbstractView implements MessageEventListener{
  /**
   * Singleton instance.
   */
  private static UserHandView instance = null;
 
  /**The main panel for the notebook view.
   */
  private JPanel userHandPanel = null;
 
  /** A handle to the controller. */
  GameController controller;

  /**
   * @return an instance of the UserHand view
   */
  public static UserHandView getInstance(GameController controller){
    if(instance == null){
      instance = new UserHandView(controller);
    }
    return instance;
  }
 
  /**
   * Private constructor for the singleton.
   * Adds the empty UI components for the user hand view.
   * Listens directly for game init message to display the hand
   */
  private UserHandView(GameController controller){
    if( controller == null){
      throw new NullPointerException();
    }
    this.controller = controller;
    controller.addView(this);
    controller.addMessageEventListener(this);
   
    userHandPanel = new JPanel();
    GridLayout gridLayout = new GridLayout(4, 2);
    userHandPanel.setLayout(gridLayout);
    //userHandPanel.setLayout(new FlowLayout());
    //userHandPanel.setBounds(0, 0, 300, 750);

  }
 
  public void displayHand(Vector<Card> hand)
  {
    //Loop through hand to display cards
    for(int i=0; i<hand.size(); i++)
    {
      /** Testing for adding card images */
      BufferedImage buttonIcon = null;
      URL resource = null;
      /** Load respective images based on card type */
     
      if(hand.get(i) instanceof WeaponCard)
      {
        WeaponCard weaponCard = ((WeaponCard)hand.get(i));
       
        switch(weaponCard.getWeaponType())
        {
          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;
        }
       
      }
      else if (hand.get(i) instanceof CharacterCard)
      {
        CharacterCard characterCard = ((CharacterCard)hand.get(i));
        switch(characterCard.getCharacterName())
        {
          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;
        }
      }   
      else if (hand.get(i) instanceof LocationCard)
      {
      LocationCard locationCard = ((LocationCard)hand.get(i));
        switch(locationCard.getLocationName())
        {
          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
       
     
      }//location else if
     
      /** 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));
     
      //add as button
      JButton button = new JButton(icon);
      button.setBorder(new LineBorder(Color.BLACK, 1));
      button.setBorderPainted(true);
      //button.setSize(75, 100);
      userHandPanel.add(button);
    }//for
 
  }
 
 
  /**
   * @return a handle to the Notebook view. CAN be null.
   */
  public JPanel getUserHandPanel(){
    return this.userHandPanel;
  }

  @Override
  public void handleMessageEvent(MessageEvent event) {
    if(event.getMessage() instanceof GameInitializationMessage)
    {
      GameInitializationMessage gameInit = (GameInitializationMessage)event.getMessage();
      //TODO: something is going wrong with displayHand
      displayHand(gameInit.getHand());
      //this.userHandPanel.repaint();
    }
  }
}
TOP

Related Classes of clueless.view.UserHandView

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.