Browse Source

Move Cards between StartStack with Mouse

feature_solitaer_MouseInput
Alexander Hartung 3 years ago
parent
commit
cc51b3fa68
  1. 31
      src/main/java/solitaer/BaseStack.java
  2. 73
      src/main/java/solitaer/MyMouseListener.java
  3. 26
      src/main/java/solitaer/SolitaerGamePanel.java

31
src/main/java/solitaer/BaseStack.java

@ -1,5 +1,7 @@
package solitaer; package solitaer;
import java.lang.invoke.TypeDescriptor.OfField;
import java.time.OffsetDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -25,7 +27,14 @@ public abstract class BaseStack extends JPanel {
} else { } else {
return null; return null;
} }
}
public int indexOfCard(Card _card) {
return stack.indexOf(_card);
}
public int stackSize() {
return stack.size();
} }
public void addCard(Card _card) { public void addCard(Card _card) {
@ -35,23 +44,23 @@ public abstract class BaseStack extends JPanel {
offset += OFFSET_VALUE; 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() { public Card removeLastCard() {
if (stack.size() != 0) { if (stack.size() != 0) {
Card LastCard = stack.remove(stack.size() - 1); Card LastCard = stack.remove(stack.size() - 1);
offset -= OFFSET_VALUE;
RenderStackNew(); RenderStackNew();
return LastCard; return LastCard;
} }
return null; return null;
}
private void RenderStackNew() {
this.removeAll();
// Alles neu Zeichnen
for (int i = stack.size() - 1; i >= 0; i--) {
this.add(stack.get(i));
}
this.repaint();
} }
protected abstract boolean cardCheck(Card _topStack, Card _playerCard); protected abstract boolean cardCheck(Card _topStack, Card _playerCard);

73
src/main/java/solitaer/MyMouseListener.java

@ -7,11 +7,12 @@ import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener; import javax.swing.event.MouseInputListener;
public class MyMouseListener implements MouseInputListener { public class MyMouseListener implements MouseInputListener {
private SaveStack save; private SaveStack save;
private BaseStack tempStack; private BaseStack tempStack;
private BaseStack selectedStack;
private Card tempCard; private Card tempCard;
public MyMouseListener(SaveStack _save) { public MyMouseListener(SaveStack _save) {
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
this.save = _save; this.save = _save;
@ -27,41 +28,55 @@ public class MyMouseListener implements MouseInputListener {
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
System.out.println("DOWN"); 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("########");
//Leeres StartFeld
Component com = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY());
// Leeres StartFeld
if (com instanceof StartStack) { if (com instanceof StartStack) {
System.out.println("Leer Start"); System.out.println("Leer Start");
} }
//Karte, die in einem StartStack liegt
else if(com instanceof Card && com.getParent() instanceof StartStack) {
// Karte, die in einem StartStack liegt
else if (com instanceof Card && com.getParent() instanceof StartStack) {
tempCard = (Card) com; tempCard = (Card) com;
tempStack = (StartStack) com.getParent(); tempStack = (StartStack) com.getParent();
//Wenn faceDown, schau ob es die oberste Carde ist und dreh diese um
if(!tempCard.isFaceUp()) {
if(tempStack.peekLastCard().equals(tempCard)) {
tempCard.setFaceUp(true);
tempCard.repaint();
System.out.println("Turn"+tempCard.toString());
if (save.stackIsEmpty()) {
// Wenn faceDown, schau ob es die oberste Carde ist und dreh diese um
if (!tempCard.isFaceUp()) {
if (tempStack.peekLastCard().equals(tempCard)) {
tempCard.setFaceUp(true);
tempCard.repaint();
}
}
// Card faceUp
else {
// move all Cards below the selected Card to Save
int index = tempStack.indexOfCard(tempCard);
while (tempStack.stackSize() - 1 >= index) {
save.addCard(tempStack.removeLastCard());
}
selectedStack = tempStack;
}
}
//saveStack is NOT empty
else {
if(selectedStack == null) {
System.out.println("ERROR selectedStack in StartStack");
}
if(tempStack.cardCheck(tempStack.peekLastCard(), save.peekLastCard())) {
while(!save.stackIsEmpty()) {
tempStack.addCard(save.removeLastCard());
}
} else {
//Alles wieder auf den Ursprünglichen Stack packen
while(!save.stackIsEmpty()) {
selectedStack.addCard(save.removeLastCard());
}
} }
selectedStack = null;
} }
//System.out.println("Karte in StartStack"+com.toString());
//save.addCard(((BaseStack)com.getParent()).removeLastCard());;
} }
} }

26
src/main/java/solitaer/SolitaerGamePanel.java

@ -26,14 +26,38 @@ public class SolitaerGamePanel extends JPanel {
StartStack a = new StartStack(25,25); StartStack a = new StartStack(25,25);
a.addCard(deck.getDeck().get(0)); a.addCard(deck.getDeck().get(0));
deck.getDeck().get(1).setFaceUp(false);
deck.getDeck().get(5).setFaceUp(true);
a.addCard((deck.getDeck().get(5)));
deck.getDeck().get(4).setFaceUp(true);
a.addCard((deck.getDeck().get(4)));
deck.getDeck().get(3).setFaceUp(true);
a.addCard((deck.getDeck().get(3)));
deck.getDeck().get(2).setFaceUp(true);
a.addCard((deck.getDeck().get(2)));
deck.getDeck().get(1).setFaceUp(true);
a.addCard((deck.getDeck().get(1))); a.addCard((deck.getDeck().get(1)));
this.add(a); this.add(a);
StartStack b = new StartStack(200,25); StartStack b = new StartStack(200,25);
deck.getDeck().get(32).setFaceUp(true);
b.addCard((deck.getDeck().get(32)));
this.add(b); this.add(b);
StartStack c = new StartStack(375,25);
deck.getDeck().get(33).setFaceUp(true);
c.addCard((deck.getDeck().get(33)));
this.add(c);
//save = new SaveStack(500,250); //save = new SaveStack(500,250);
this.add(save); this.add(save);

Loading…
Cancel
Save