You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
2.6 KiB
136 lines
2.6 KiB
package solitaer;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Graphics;
|
|
import java.util.Objects;
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
enum Symbol {
|
|
Herz, Karo, Kreuz, Pik
|
|
}
|
|
|
|
public class Card extends JPanel {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
private int nr;
|
|
private Symbol symbol;
|
|
|
|
private boolean faceUp;
|
|
|
|
private final int IMAGE_WIDTH = 72;
|
|
private final int IMAGE_HIGHT = 96;
|
|
|
|
public Card(int _nr, Symbol _symbol, boolean _faceUp) {
|
|
this.setSize(IMAGE_WIDTH,IMAGE_HIGHT);
|
|
this.nr = _nr;
|
|
this.symbol = _symbol;
|
|
this.faceUp = _faceUp;
|
|
}
|
|
|
|
public boolean isFaceUp() {
|
|
return faceUp;
|
|
}
|
|
|
|
public void setFaceUp(boolean _faceUp) {
|
|
this.faceUp = _faceUp;
|
|
}
|
|
|
|
public int getNr() {
|
|
return nr;
|
|
}
|
|
|
|
public boolean isred() {
|
|
return this.symbol == Symbol.Herz
|
|
|| this.symbol == Symbol.Karo;
|
|
}
|
|
|
|
public boolean isblack() {
|
|
return this.symbol == Symbol.Kreuz
|
|
|| this.symbol == Symbol.Pik;
|
|
}
|
|
|
|
private String getNrText() {
|
|
if (this.nr < 10) {
|
|
return "0" + this.nr;
|
|
} else {
|
|
return "" + this.nr;
|
|
}
|
|
}
|
|
|
|
public Symbol getSymbol() {
|
|
return symbol;
|
|
}
|
|
|
|
private String getSymboleText() {
|
|
switch (this.symbol) {
|
|
case Herz:
|
|
return "Herz";
|
|
case Karo:
|
|
return "Karo";
|
|
case Pik:
|
|
return "Pik";
|
|
case Kreuz:
|
|
return "Kreuz";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
//return "Card [nr=" + nr + ", symbol=" + symbol + ", faceUp=" + faceUp + "]";
|
|
return "Card NR="+nr+" | Symbole="+symbol+" | Face="+faceUp;
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(faceUp, nr, symbol);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj)
|
|
return true;
|
|
if (obj == null)
|
|
return false;
|
|
if (getClass() != obj.getClass())
|
|
return false;
|
|
Card other = (Card) obj;
|
|
return faceUp == other.faceUp && nr == other.nr && symbol == other.symbol;
|
|
}
|
|
|
|
@Override
|
|
protected void paintComponent(Graphics g) {
|
|
// TODO Auto-generated method stub
|
|
super.paintComponent(g);
|
|
paintCard(g);
|
|
|
|
}
|
|
|
|
|
|
private void paintCard(Graphics g) {
|
|
if (faceUp) {
|
|
g.setColor(Color.white);
|
|
g.fillRect(0, 0, this.getWidth(), this.getHeight());
|
|
|
|
g.setColor(Color.black);
|
|
g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
|
|
|
|
if (this.isred())
|
|
g.setColor(Color.red);
|
|
else
|
|
g.setColor(Color.black);
|
|
|
|
String text = "" + this.getNrText() + " " + this.getSymboleText();
|
|
g.drawString(text, 3, 15);
|
|
} else {
|
|
g.setColor(Color.blue);
|
|
g.fillRect(0, 0, this.getWidth(), this.getHeight());
|
|
|
|
g.setColor(Color.black);
|
|
g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
|
|
}
|
|
}
|
|
|
|
}
|