Newer
Older
hanabi / src / game / SwingGame.java
package game;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import card.Card;
import player.AbstractPlayer;
import player.SwingPlayer;

public class SwingGame extends BaseGame {
	//UI STUFF
	JFrame mainFrame;
	JPanel gamePanel;
	JPanel hintPanel;
	JPanel thunderPanel;
	JPanel buttonPanel;
	JPanel deckPanel;
	JLabel[][] deckUI;
	JButton colorButton;
	JButton valueButton;
	JButton nextMove;
	JButton placeCardButton;
	JButton trashCardButton;
	JLabel[] hintUI;
	JLabel[] thunderUI;
	JPanel[] playersUI;
	
	Font font;
	
	public static void main(String[] args){
		SwingGame game = new SwingGame();
		game.getPlayer(game.currentPlayer).deactiveCards();
		game.printMessage("Player " + game.currentPlayer + "'s turn", MSG_TYPES.INFORMATION);
	}

	@Override
	protected void createPlayers(){
		super.createPlayers();
		for(int i=0; i<N_PLAYERS; ++i){
			players[i] = new SwingPlayer(CARDS_PER_PLAYER, this, i);
		}
	}

	@Override
	protected void createUI() {
		mainFrame = new JFrame("Hanabi");
		mainFrame.setSize(1000, 650);
		mainFrame.setLocation(0, 0);
		mainFrame.setLayout(new BorderLayout());
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.getContentPane().setBackground(Color.black);
		mainFrame.setVisible(true);
		gamePanel = new JPanel();
		gamePanel.setLayout(new BoxLayout(gamePanel, BoxLayout.Y_AXIS));
		GridLayout deckLayout = new GridLayout(COLORS, 5);
		deckPanel = new JPanel(deckLayout);
		deckPanel.setBackground(Color.black);
		deckUI = new JLabel[COLORS][5];
		for(int i=0; i<COLORS; ++i){
			for(int j=0; j<5; ++j){
				JLabel tmp = new JLabel("Not set");
				tmp.setBackground(Color.black);
				tmp.setForeground(Color.white);
				tmp.setFont(font);
				deckUI[i][j] = tmp;
				deckPanel.add(deckUI[i][j]);
			}
		}
		hintPanel = new JPanel();
		thunderPanel = new JPanel();
		buttonPanel = new JPanel();
		colorButton = new JButton("Color");
		valueButton = new JButton("Value");
		nextMove = new JButton("Next");
		placeCardButton = new JButton("Place");
		trashCardButton = new JButton("Trash");
		colorButton.setFont(font);
		valueButton.setFont(font);
		nextMove.setFont(font);
		placeCardButton.setFont(font);
		trashCardButton.setFont(font);
		buttonPanel.add(colorButton);
		buttonPanel.add(valueButton);
		buttonPanel.add(nextMove);
		buttonPanel.add(placeCardButton);
		buttonPanel.add(trashCardButton);
		mainFrame.add(gamePanel, BorderLayout.CENTER);
		gamePanel.add(hintPanel);
		gamePanel.add(thunderPanel);
		gamePanel.add(buttonPanel);
		gamePanel.add(deckPanel);
		hintUI = new JLabel[MAX_HINTS];
		for(int i=0; i<MAX_HINTS; ++i){
			JLabel tmp = new JLabel();
			tmp.setText("1");
			tmp.setBackground(Color.white);
			tmp.setName("Hint_" + i);
			tmp.setFont(font);
			hintPanel.add(tmp);
			hintUI[i] = tmp;			
		}
		thunderUI = new JLabel[MAX_THUNDERS];
		for(int i=0;i<MAX_THUNDERS; ++i){
			JLabel tmp = new JLabel();
			tmp.setText("0");
			tmp.setBackground(Color.white);
			tmp.setName("Thunder_" + i);
			tmp.setFont(font);
			thunderPanel.add(tmp);
			thunderUI[i] = tmp;
		}
		colorButton.addMouseListener(new MouseListener(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				onColor();
			}			
			@Override
			public void mousePressed(MouseEvent e) {
			}			
			@Override
			public void mouseExited(MouseEvent e) {
			}			
			@Override
			public void mouseEntered(MouseEvent e) {
			}			
			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
		valueButton.addMouseListener(new MouseListener(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				onValue();
			}			
			@Override
			public void mousePressed(MouseEvent e) {
			}			
			@Override
			public void mouseExited(MouseEvent e) {
			}			
			@Override
			public void mouseEntered(MouseEvent e) {
			}			
			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
		nextMove.addMouseListener(new MouseListener(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				onNext();
			}			
			@Override
			public void mousePressed(MouseEvent e) {
			}			
			@Override
			public void mouseExited(MouseEvent e) {
			}			
			@Override
			public void mouseEntered(MouseEvent e) {
			}			
			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
		trashCardButton.addMouseListener(new MouseListener(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				if(e.isControlDown()) showTrash();
				else onTrash();
			}			
			@Override
			public void mousePressed(MouseEvent e) {
			}			
			@Override
			public void mouseExited(MouseEvent e) {
			}			
			@Override
			public void mouseEntered(MouseEvent e) {
			}			
			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
		placeCardButton.addMouseListener(new MouseListener(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				onPlace();
			}			
			@Override
			public void mousePressed(MouseEvent e) {
			}			
			@Override
			public void mouseExited(MouseEvent e) {
			}			
			@Override
			public void mouseEntered(MouseEvent e) {
			}			
			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
	}
	
	@Override
	protected void dealCards(){
		super.dealCards();
		/*
		 * PLAYER 0: SOUTH
		 * PLAYER 1: EAST
		 * PLAYER 2: NORTH
		 * PLAYER 3: WEST
		 */
		for(int i=0; i<N_PLAYERS; ++i){
			JPanel playerContainer = ((SwingPlayer) players[i]).getContainer();
			BoxLayout containerLayout = null;
			String playerSide = null;
			switch(i){
			case 0:
				playerSide = BorderLayout.SOUTH;
				containerLayout = new BoxLayout(playerContainer, BoxLayout.X_AXIS);
				break;
			case 1:
				playerSide = BorderLayout.EAST;
				containerLayout = new BoxLayout(playerContainer, BoxLayout.Y_AXIS);
				break;
			case 2:
				playerSide = BorderLayout.NORTH;
				containerLayout = new BoxLayout(playerContainer, BoxLayout.X_AXIS);
				break;
			case 3:
				if(N_PLAYERS == 5){
					playerSide = BorderLayout.NORTH;
					containerLayout = new BoxLayout(playerContainer, BoxLayout.X_AXIS);
				}
				else{
					playerSide = BorderLayout.WEST;
					containerLayout = new BoxLayout(playerContainer, BoxLayout.Y_AXIS);
				}
				break;
			case 4:
				playerSide = BorderLayout.WEST;
				containerLayout = new BoxLayout(playerContainer, BoxLayout.Y_AXIS);
				break;
			}
			playerContainer.setLayout(containerLayout);
			playerContainer.setBackground(Color.black);
			for(int j=0; j<CARDS_PER_PLAYER; ++j){
				AbstractPlayer curr = players[i];
				curr.setName(j, i + "_card" + j);
				curr.setSize(j, 50, 50);
			}
			mainFrame.add(playerContainer, playerSide);
		}
	}

	@Override
	protected void setNoCard(int index) {
		SwingPlayer card = (SwingPlayer) players[currentPlayer];
		card.setColor(index, 255, 122, 0);
		card.setText(index, "No Cards left");
		card.unsetListeners(index);
	}

	@Override
	public void loadFont() {
		font = new Font("Shanghai", Font.PLAIN, fontSize);
	}

	@Override
	public void printMessage(String msg, MSG_TYPES type) {
		String title = "";
		int jType = 0;
		switch(type){
		case ERROR:
			jType = JOptionPane.ERROR_MESSAGE;
			title = "Error";
			break;
		case WARNING:
			jType = JOptionPane.WARNING_MESSAGE;
			title = "Warning";
			break;
		case INFORMATION:
			jType = JOptionPane.INFORMATION_MESSAGE;
			title = "Information";
			break;
		case QUESTION:
			jType = JOptionPane.QUESTION_MESSAGE;
			title = "Question";
			break;
		}
		JOptionPane.showMessageDialog(mainFrame,
		    msg,
		    title,
		    jType);
	}
	
	@Override
	protected void removeHint(){
		super.removeHint();
		hintUI[hints].setText("0");
	}
	
	@Override
	protected void addHint(){
		super.addHint();
		hintUI[hints-1].setText("1");
	}
	
	@Override
	protected void addThunder(){
		super.addThunder();
		thunderUI[thunders-1].setText("1");
	}
	
	@Override
	protected void placeCard(Card c, int color, int value){
		deckUI[color][value].setText(value + "");
		deckUI[color][value].setForeground(c.getColor());
	}

	@Override
	protected void onNext(){
		super.onNext();
	}
	
	@Override
	protected void onColor(){
		super.onColor();
	}
	
	@Override
	protected void onValue(){
		super.onValue();
	}
	
	@Override
	protected void onTrash(){
		super.onTrash();
	}
	
	@Override
	protected void onPlace(){
		super.onPlace();
	}
	
	@Override
	protected void showTrash(){
		JPanel panel = new JPanel();
		panel.setBackground(Color.black);
		for(Card c : trash){
			JLabel label = new JLabel(c.getValue() + "");
			label.setForeground(c.getColor());
			panel.add(label);
		}
		panel.setVisible(true);
		JOptionPane.showMessageDialog(mainFrame, panel);
	}
	
	@Override
	public Object getFont() {
		return font;
	}
}