Browse Source

Fix Card and StartStack DRAW

feature_solitaer_StartStack
Alexander Hartung 3 years ago
parent
commit
7d0878431e
  1. 54
      src/main/java/solitaer/Card.java
  2. 12
      src/main/java/solitaer/StartStack.java

54
src/main/java/solitaer/Card.java

@ -2,10 +2,8 @@ package solitaer;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Image;
import java.util.Objects; import java.util.Objects;
import javax.swing.ImageIcon;
import javax.swing.JPanel; import javax.swing.JPanel;
enum Symbol { enum Symbol {
@ -20,9 +18,6 @@ public class Card extends JPanel {
private boolean faceUp; private boolean faceUp;
private Image Up;
private Image back;
private final int IMAGE_WIDTH = 72; private final int IMAGE_WIDTH = 72;
private final int IMAGE_HIGHT = 96; private final int IMAGE_HIGHT = 96;
@ -31,13 +26,6 @@ public class Card extends JPanel {
this.nr = nr; this.nr = nr;
this.symbol = symbol; this.symbol = symbol;
this.faceUp = faceUp; this.faceUp = faceUp;
String cardName = "" + getFileNr() + getFileSymbole();
ImageIcon iiup = new ImageIcon(getClass().getResource("images/" + cardName + ".png"));
Up = iiup.getImage();
ImageIcon iiback = new ImageIcon(getClass().getResource("images/back.png"));
back = iiback.getImage();
} }
public boolean isFaceUp() { public boolean isFaceUp() {
@ -52,7 +40,7 @@ public class Card extends JPanel {
return nr; return nr;
} }
private String getFileNr() {
private String getNrText() {
if (this.nr < 10) { if (this.nr < 10) {
return "0" + this.nr; return "0" + this.nr;
} else { } else {
@ -64,7 +52,7 @@ public class Card extends JPanel {
return symbol; return symbol;
} }
private String getFileSymbole() {
private String getSymboleText() {
switch (this.symbol) { switch (this.symbol) {
case Herz: case Herz:
return "Herz"; return "Herz";
@ -79,14 +67,6 @@ public class Card extends JPanel {
} }
} }
public Image getImage() {
if (faceUp) {
return Up;
} else {
return back;
}
}
@Override @Override
public String toString() { public String toString() {
return "Card [nr=" + nr + ", symbol=" + symbol + ", faceUp=" + faceUp + "]"; return "Card [nr=" + nr + ", symbol=" + symbol + ", faceUp=" + faceUp + "]";
@ -113,9 +93,33 @@ public class Card extends JPanel {
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
super.paintComponent(g); super.paintComponent(g);
g.drawImage(getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
g.setColor(Color.black);
g.drawRect(0, 0, this.getWidth()-1, this.getHeight()-1);
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.symbol == Symbol.Herz || this.symbol == Symbol.Karo)
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);
}
} }
} }

12
src/main/java/solitaer/StartStack.java

@ -1,16 +1,14 @@
package solitaer; package solitaer;
import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JPanel; import javax.swing.JPanel;
public class StartStack extends JPanel { public class StartStack extends JPanel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private Image im;
private ArrayList<Card> stack = new ArrayList<Card>(); private ArrayList<Card> stack = new ArrayList<Card>();
private final int OFFSET_VALUE = 20; private final int OFFSET_VALUE = 20;
@ -71,9 +69,11 @@ public class StartStack extends JPanel {
// TODO Auto-generated method stub // TODO Auto-generated method stub
super.paintComponent(g); super.paintComponent(g);
if (stack.size() == 0) { if (stack.size() == 0) {
ImageIcon ii = new ImageIcon(getClass().getResource("images/empty.png"));
im = ii.getImage();
g.drawImage(im, 0, 0, this.getWidth(), 96, this);
g.setColor(Color.gray);
g.fillRect(0, 0, this.getWidth(), 96);
g.setColor(Color.black);
g.drawRect(0, 0, this.getWidth()-1, 96-1);
} }
} }

Loading…
Cancel
Save