From 5aab0411798e7bbd41c70e75d0a2bfc51972ce0e Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Thu, 27 Jan 2022 22:51:55 +0100 Subject: [PATCH] Refactor Start- Save- BaseStack and MyMousListener --- src/main/java/solitaer/BaseStack.java | 6 ++++-- src/main/java/solitaer/MyMouseListener.java | 18 +++++++++--------- src/main/java/solitaer/SaveStack.java | 6 +++--- src/main/java/solitaer/StartStack.java | 10 +++------- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main/java/solitaer/BaseStack.java b/src/main/java/solitaer/BaseStack.java index e8064d0..84dfab2 100644 --- a/src/main/java/solitaer/BaseStack.java +++ b/src/main/java/solitaer/BaseStack.java @@ -1,12 +1,14 @@ package solitaer; -import java.lang.invoke.TypeDescriptor.OfField; -import java.time.OffsetDateTime; import java.util.ArrayList; import javax.swing.JPanel; public abstract class BaseStack extends JPanel { + public final int IMAGE_WIDTH = 72; + public final int IMAGE_HIGHT = 96; + public final int STACK_HIGHT = 450; + private static final long serialVersionUID = 1L; private ArrayList stack = new ArrayList(); private final int OFFSET_VALUE = 20; diff --git a/src/main/java/solitaer/MyMouseListener.java b/src/main/java/solitaer/MyMouseListener.java index 4ca99b8..4e9a08a 100644 --- a/src/main/java/solitaer/MyMouseListener.java +++ b/src/main/java/solitaer/MyMouseListener.java @@ -8,14 +8,14 @@ import javax.swing.event.MouseInputListener; public class MyMouseListener implements MouseInputListener { - private SaveStack save; + private SaveStack saveStack; private BaseStack tempStack; private BaseStack selectedStack; private Card tempCard; public MyMouseListener(SaveStack _save) { // TODO Auto-generated constructor stub - this.save = _save; + this.saveStack = _save; } @Override @@ -40,7 +40,7 @@ public class MyMouseListener implements MouseInputListener { tempCard = (Card) com; tempStack = (StartStack) com.getParent(); - if (save.stackIsEmpty()) { + if (saveStack.stackIsEmpty()) { // Wenn faceDown, schau ob es die oberste Carde ist und dreh diese um if (!tempCard.isFaceUp()) { if (tempStack.peekLastCard().equals(tempCard)) { @@ -53,7 +53,7 @@ public class MyMouseListener implements MouseInputListener { // move all Cards below the selected Card to Save int index = tempStack.indexOfCard(tempCard); while (tempStack.stackSize() - 1 >= index) { - save.addCard(tempStack.removeLastCard()); + saveStack.addCard(tempStack.removeLastCard()); } selectedStack = tempStack; @@ -64,14 +64,14 @@ public class MyMouseListener implements MouseInputListener { if(selectedStack == null) { System.out.println("ERROR selectedStack in StartStack"); } - if(tempStack.cardCheck(tempStack.peekLastCard(), save.peekLastCard())) { - while(!save.stackIsEmpty()) { - tempStack.addCard(save.removeLastCard()); + if(tempStack.cardCheck(tempStack.peekLastCard(), saveStack.peekLastCard())) { + while(!saveStack.stackIsEmpty()) { + tempStack.addCard(saveStack.removeLastCard()); } } else { //Alles wieder auf den Ursprünglichen Stack packen - while(!save.stackIsEmpty()) { - selectedStack.addCard(save.removeLastCard()); + while(!saveStack.stackIsEmpty()) { + selectedStack.addCard(saveStack.removeLastCard()); } } diff --git a/src/main/java/solitaer/SaveStack.java b/src/main/java/solitaer/SaveStack.java index d95974f..3f423a2 100644 --- a/src/main/java/solitaer/SaveStack.java +++ b/src/main/java/solitaer/SaveStack.java @@ -11,7 +11,7 @@ public class SaveStack extends BaseStack { // TODO Auto-generated constructor stub super(); super.setLocation(_x, _y); - super.setSize(72, 450); + super.setSize(IMAGE_WIDTH, STACK_HIGHT); super.setOpaque(false); super.setLayout(null); } @@ -28,10 +28,10 @@ public class SaveStack extends BaseStack { super.paintComponent(g); if (stackIsEmpty()) { g.setColor(Color.pink); - g.fillRect(0, 0, this.getWidth(), 96); + g.fillRect(0, 0, this.getWidth(), IMAGE_HIGHT); g.setColor(Color.black); - g.drawRect(0, 0, this.getWidth() - 1, 96 - 1); + g.drawRect(0, 0, this.getWidth() - 1, IMAGE_HIGHT - 1); } } diff --git a/src/main/java/solitaer/StartStack.java b/src/main/java/solitaer/StartStack.java index 28710e4..8f14ae6 100644 --- a/src/main/java/solitaer/StartStack.java +++ b/src/main/java/solitaer/StartStack.java @@ -8,14 +8,10 @@ public class StartStack extends BaseStack { private static final long serialVersionUID = 1L; - private final int IMAGE_WIDTH = 72; - private final int IMAGE_HIGHT = 450; - - public StartStack(int _x, int _y) { super(); super.setLocation(_x, _y); - super.setSize(IMAGE_WIDTH, IMAGE_HIGHT); + super.setSize(IMAGE_WIDTH, STACK_HIGHT); super.setOpaque(false); super.setLayout(null); } @@ -51,10 +47,10 @@ public class StartStack extends BaseStack { super.paintComponent(g); if (stackIsEmpty()) { g.setColor(Color.gray); - g.fillRect(0, 0, this.getWidth(), 96); + g.fillRect(0, 0, this.getWidth(), IMAGE_HIGHT); g.setColor(Color.black); - g.drawRect(0, 0, this.getWidth()-1, 96-1); + g.drawRect(0, 0, this.getWidth()-1, IMAGE_HIGHT-1); } }