From 421cd8903f8d74d48dbad28bd7e969f4382f7a1f Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Thu, 27 Jan 2022 21:28:46 +0100 Subject: [PATCH] Add BaseStack Refactor StartStack --- src/main/java/solitaer/BaseStack.java | 49 +++++++++++++++++++++ src/main/java/solitaer/MyMouseListener.java | 20 +++++++-- src/main/java/solitaer/StartStack.java | 29 +++--------- 3 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 src/main/java/solitaer/BaseStack.java diff --git a/src/main/java/solitaer/BaseStack.java b/src/main/java/solitaer/BaseStack.java new file mode 100644 index 0000000..7300cb9 --- /dev/null +++ b/src/main/java/solitaer/BaseStack.java @@ -0,0 +1,49 @@ +package solitaer; + +import java.util.ArrayList; + +import javax.swing.JPanel; + +public abstract class BaseStack extends JPanel { + private static final long serialVersionUID = 1L; + private ArrayList stack = new ArrayList(); + private final int OFFSET_VALUE = 20; + int offset; + + public BaseStack() { + // TODO Auto-generated constructor stub + offset = 0; + } + + public boolean stackIsEmpty() { + return stack.isEmpty(); + } + + public void addCard(Card _card) { + _card.setLocation(0, offset); + stack.add(_card); + RenderStackNew(); + offset += OFFSET_VALUE; + } + + private void RenderStackNew() { + this.removeAll(); + // Alles neu Zeichnen + for (int i = stack.size() - 1; i >= 0; i--) { + this.add(stack.get(i)); + } + this.repaint(); + } + + public Card removeLastCard() { + if (stack.size() != 0) { + Card LastCard = stack.remove(stack.size() - 1); + RenderStackNew(); + return LastCard; + } + return null; + + } + + protected abstract boolean cardCheck(Card _topStack, Card _playerCard); +} diff --git a/src/main/java/solitaer/MyMouseListener.java b/src/main/java/solitaer/MyMouseListener.java index a5db8af..7a29a4b 100644 --- a/src/main/java/solitaer/MyMouseListener.java +++ b/src/main/java/solitaer/MyMouseListener.java @@ -20,10 +20,22 @@ public class MyMouseListener implements MouseInputListener { System.out.println("DOWN"); Component com = SwingUtilities.getDeepestComponentAt( e.getComponent(), e.getX(), e.getY()); - System.out.println("########"); - System.out.println(com.toString()); - System.out.println(com.getParent().toString()); - System.out.println("########"); +// System.out.println("########"); +// System.out.println(com.toString()); +// System.out.println(com.getParent().toString()); +// System.out.println("########"); + + //Leeres StartFeld + if (com instanceof StartStack) { + System.out.println("Leer Start"); + + } + //Karte, die in einem StartStack liegt + else if(com instanceof Card && com.getParent() instanceof StartStack) { + System.out.println("Karte in StartStack"+com.toString()); + + ((BaseStack)com.getParent()).removeLastCard(); + } } diff --git a/src/main/java/solitaer/StartStack.java b/src/main/java/solitaer/StartStack.java index 7bc782e..28710e4 100644 --- a/src/main/java/solitaer/StartStack.java +++ b/src/main/java/solitaer/StartStack.java @@ -2,28 +2,22 @@ package solitaer; import java.awt.Color; import java.awt.Graphics; -import java.util.ArrayList; -import javax.swing.JPanel; -public class StartStack extends JPanel { +public class StartStack extends BaseStack { private static final long serialVersionUID = 1L; - private ArrayList stack = new ArrayList(); - private final int OFFSET_VALUE = 20; private final int IMAGE_WIDTH = 72; private final int IMAGE_HIGHT = 450; - private int offset; public StartStack(int _x, int _y) { + super(); super.setLocation(_x, _y); super.setSize(IMAGE_WIDTH, IMAGE_HIGHT); super.setOpaque(false); super.setLayout(null); - - offset = 0; } public boolean cardCheck(Card _topStack, Card _playerCard) { @@ -49,26 +43,13 @@ public class StartStack extends JPanel { } } - public void addCard(Card _card) { - _card.setLocation(0, offset); - stack.add(_card); - addtoPanel(); - offset += OFFSET_VALUE; - } - - private void addtoPanel() { - this.removeAll(); - //Alles neu Zeichnen - for (int i = stack.size() - 1; i >= 0; i--) { - this.add(stack.get(i)); - } - } + @Override protected void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g); - if (stack.size() == 0) { + if (stackIsEmpty()) { g.setColor(Color.gray); g.fillRect(0, 0, this.getWidth(), 96); @@ -77,4 +58,6 @@ public class StartStack extends JPanel { } } + + }