From 7d0878431ed5b959c3fa6887396ac1e8773665ad Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Wed, 26 Jan 2022 22:25:26 +0100 Subject: [PATCH] Fix Card and StartStack DRAW --- src/main/java/solitaer/Card.java | 54 ++++++++++++++------------ src/main/java/solitaer/StartStack.java | 12 +++--- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/main/java/solitaer/Card.java b/src/main/java/solitaer/Card.java index 9eeec05..1933d7a 100644 --- a/src/main/java/solitaer/Card.java +++ b/src/main/java/solitaer/Card.java @@ -2,10 +2,8 @@ package solitaer; import java.awt.Color; import java.awt.Graphics; -import java.awt.Image; import java.util.Objects; -import javax.swing.ImageIcon; import javax.swing.JPanel; enum Symbol { @@ -19,9 +17,6 @@ public class Card extends JPanel { private Symbol symbol; private boolean faceUp; - - private Image Up; - private Image back; private final int IMAGE_WIDTH = 72; private final int IMAGE_HIGHT = 96; @@ -31,13 +26,6 @@ public class Card extends JPanel { this.nr = nr; this.symbol = symbol; 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() { @@ -52,7 +40,7 @@ public class Card extends JPanel { return nr; } - private String getFileNr() { + private String getNrText() { if (this.nr < 10) { return "0" + this.nr; } else { @@ -64,7 +52,7 @@ public class Card extends JPanel { return symbol; } - private String getFileSymbole() { + private String getSymboleText() { switch (this.symbol) { case Herz: return "Herz"; @@ -79,14 +67,6 @@ public class Card extends JPanel { } } - public Image getImage() { - if (faceUp) { - return Up; - } else { - return back; - } - } - @Override public String toString() { return "Card [nr=" + nr + ", symbol=" + symbol + ", faceUp=" + faceUp + "]"; @@ -113,9 +93,33 @@ public class Card extends JPanel { protected void paintComponent(Graphics g) { // TODO Auto-generated method stub 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); + } } } diff --git a/src/main/java/solitaer/StartStack.java b/src/main/java/solitaer/StartStack.java index a4bd932..7bc782e 100644 --- a/src/main/java/solitaer/StartStack.java +++ b/src/main/java/solitaer/StartStack.java @@ -1,16 +1,14 @@ package solitaer; +import java.awt.Color; import java.awt.Graphics; -import java.awt.Image; import java.util.ArrayList; -import javax.swing.ImageIcon; import javax.swing.JPanel; public class StartStack extends JPanel { private static final long serialVersionUID = 1L; - private Image im; private ArrayList stack = new ArrayList(); private final int OFFSET_VALUE = 20; @@ -71,9 +69,11 @@ public class StartStack extends JPanel { // TODO Auto-generated method stub super.paintComponent(g); 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); } }