package pdp.scrabble.game.impl;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.io.Serializable;
import org.jdom.Attribute;
import org.jdom.Element;
import pdp.scrabble.game.Bag;
import pdp.scrabble.game.BoardCase;
import pdp.scrabble.game.Letter;
import static pdp.scrabble.Factory.FACTORY;
public class LetterImpl implements Letter, Serializable {
private static final long serialVersionUID = 1L;
/** Letter name. */
private char name = 0;
/** Letter value, its points. */
private int value = 0;
/** Joker character. */
private char joker = 0;
/** Letter ID. */
private int id = 0;
/** Letter color. */
private Color color = null;
public LetterImpl(char name, int value, int id) {
this.name = name;
this.value = value;
this.joker = Bag.NON_JOKER;
this.id = id;
this.color = Letter.BACKGROUND_COLOR;
}
@Override
public void render(Graphics g, int x, int y) {
Font font = g.getFont();
// Render letter background
g.setColor(this.color);
g.fill3DRect(x, y, BoardCase.CASE_SIZE, BoardCase.CASE_SIZE, true);
// Render letter character
g.setColor(Color.BLACK);
g.setFont(Letter.FONT_LETTER);
g.drawString(String.valueOf(this.getName()), x + 14, y + 24);
// Render letter value
g.setFont(Letter.FONT_VALUE);
g.drawString(String.valueOf(this.getValue()), x + 26, y + 32);
g.setFont(font);
}
@Override
public void clear() {
this.name = 0;
this.joker = 0;
this.value = 0;
}
@Override
public void setJokerChar(char name) {
this.joker = name;
}
@Override
public char getJokerChar() {
return this.joker;
}
@Override
public void setName(char name) {
this.name = name;
}
@Override
public char getName() {
if (this.joker >= 65 && this.joker <= 90) {
return this.joker;
}
else {
return this.name;
}
}
@Override
public void setValue(int value) {
this.value = value;
}
@Override
public int getValue() {
return this.value;
}
@Override
public int getID() {
return this.id;
}
@Override
public void setColor(Color color) {
this.color = color;
}
@Override
public void save(Element root) {
Element e = new Element("Letter");
root.addContent(e);
e.setAttribute(new Attribute("name", String.valueOf(this.getName())));
e.setAttribute(new Attribute("value", String.valueOf(this.getValue())));
e.setAttribute(new Attribute("id", String.valueOf(this.getID())));
e.setAttribute(new Attribute("color", String.valueOf(this.color.getRGB())));
}
@Override
public void load(Element root) {
this.name = root.getAttributeValue("name").charAt(0);
this.value = Integer.parseInt(root.getAttributeValue("value"));
this.id = Integer.parseInt(root.getAttributeValue("id"));
this.color = new Color(Integer.parseInt(root.getAttributeValue("color")));
}
@Override
public boolean equals(Letter letter) {
if (letter == null) {
return false;
}
else {
return this.getName() == letter.getName();
// && this.getValue() == letter.getValue()
// && this.getJokerChar() == letter.getJokerChar();
// && this.getID() == letter.getID());
}
}
public boolean equals(Object o) {
if (o instanceof LetterImpl)
return equals((LetterImpl)o);
else return false;
}
@Override
public Letter clone() {
Letter letter = FACTORY.createLetter(
this.getName(), this.getValue(), this.getID());
letter.setJokerChar(this.getJokerChar());
letter.setColor(this.color);
return letter;
}
public String toString() {
return ""+this.getName();
}
}