Browse Source

Refactor Start- Save- BaseStack and MyMousListener

feature_solitaer_MouseInput
Alexander Hartung 3 years ago
parent
commit
5aab041179
  1. 6
      src/main/java/solitaer/BaseStack.java
  2. 18
      src/main/java/solitaer/MyMouseListener.java
  3. 6
      src/main/java/solitaer/SaveStack.java
  4. 10
      src/main/java/solitaer/StartStack.java

6
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<Card> stack = new ArrayList<Card>();
private final int OFFSET_VALUE = 20;

18
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());
}
}

6
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);
}
}

10
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);
}
}

Loading…
Cancel
Save