Newer
Older
hanabi / src / player / SwingPlayer.java
package player;
import game.Game;

import java.awt.Color;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.JPanel;

import card.Card;
import card.SwingCard;

public class SwingPlayer extends AbstractPlayer {
	private final static String HTML_BEGIN = "<html>";
	private final static String HTML_END = "</html>";
	private final static String HTML_NEWLINE = "<br/>";
	//private Card[] swingCards;
	private JLabel[] labels;
	private JPanel container;
	public SwingPlayer(int nrOfCards, Game g, int Id){
		super(nrOfCards, g, Id);
		labels = new JLabel[nrOfCards];
		container = new JPanel();
		for(int i=0; i<nrOfCards; ++i){
			cards[i] = new SwingCard(0, 0, Id, g);
			labels[i] = new JLabel();
			((SwingCard) cards[i]).setFace(labels[i]);
			container.add(((SwingCard) cards[i]).getFace());
		}
	}
	
	public JPanel getContainer(){
		return container;
	}
	
	public JLabel getUiCard(int pos){
		return ((SwingCard) cards[pos]).getFace();
	}
	
	@Override
	public void setCard(int pos, Card card) {
		super.setCard(pos, card);
		SwingCard sc = ((SwingCard) cards[pos]);		
		sc.getFace().setText(card.getValue() + "");
		sc.getFace().setForeground(card.getColor());
		sc.getFace().setBackground(Color.black);
		sc.getFace().setOpaque(true);
	}
	@Override
	public Card getCard(int pos) {
		return super.getCard(pos);
	}
	@Override
	public void setCardInfo(int pos, String info) {
		super.setCardInfo(pos, info);
		if(g.getDifficulty() != 0) return;
		info = info.replaceAll("\\n", HTML_NEWLINE);
		info = HTML_BEGIN + info + HTML_END;
		((SwingCard) cards[pos]).getFace().setToolTipText(info);
	}
	@Override
	public String getCardInfo(int pos) {
		return super.getCardInfo(pos);
	}
	
	@Override
	public void print(){
		System.out.println("swing");
	}

	@Override
	public void setName(int index, String name) {
		((SwingCard) cards[index]).getFace().setName(name);
	}

	@Override
	public String getName(int index) {
		return ((SwingCard) cards[index]).getFace().getName();
	}

	@Override
	public void setSize(int index, int width, int height) {
		((SwingCard) cards[index]).getFace().setSize(width, height);
	}

	@Override
	public void setColor(int index, int r, int g, int b) {
		((SwingCard) cards[index]).getFace().setForeground(new Color(r, g, b));
	}

	@Override
	public int[] getColor(int index) {
		int[] color = new int[3];
		Color c = ((SwingCard) cards[index]).getFace().getForeground();
		color[0] = c.getRed();
		color[1] = c.getGreen();
		color[2] = c.getBlue();
		return color;
	}

	@Override
	public void setBackground(int index, int r, int g, int b) {
		((SwingCard) cards[index]).getFace().setBackground(new Color(r, g, b));
	}

	@Override
	public int[] getBackground(int index) {
		int[] color = new int[3];
		Color c = ((SwingCard) cards[index]).getFace().getBackground();
		color[0] = c.getRed();
		color[1] = c.getGreen();
		color[2] = c.getBlue();
		return color;
	}

	@Override
	public void setText(int index, String text) {
		((SwingCard) cards[index]).getFace().setText(text);
	}

	@Override
	public String getText(int index) {
		return ((SwingCard) cards[index]).getFace().getText();
	}

	@Override
	public void unsetListeners(int index) {
		MouseListener[] listeners = ((SwingCard) cards[index]).getFace().getMouseListeners();
		for(int i=0; i<listeners.length; ++i){
			((SwingCard) cards[index]).getFace().removeMouseListener(listeners[i]);
		}
	}

	@Override
	public void deactiveCards(){
		for(int i=0; i<cards.length; ++i){
			((SwingCard) cards[i]).getFace().setText("?");
			//swingCards[i].getFace().setEnabled(false);
			((SwingCard) cards[i]).getFace().setForeground(Color.gray);
		}
	}

	@Override
	public void activeCards(){
		for(int i=0; i<cards.length; ++i){
			((SwingCard) cards[i]).getFace().setText(getCard(i).getValue() + "");
			//swingCards[i].getFace().setEnabled(true);
			((SwingCard) cards[i]).getFace().setBackground(getCard(i).getColor());
		}
	}
}