Browse Source

Turn Card with Mouse

feature_solitaer_MouseInput
Alexander Hartung 3 years ago
parent
commit
b11a6244b7
  1. 9
      src/main/java/solitaer/BaseStack.java
  2. 3
      src/main/java/solitaer/Card.java
  3. 31
      src/main/java/solitaer/MyMouseListener.java
  4. 38
      src/main/java/solitaer/SaveStack.java
  5. 12
      src/main/java/solitaer/SolitaerGamePanel.java

9
src/main/java/solitaer/BaseStack.java

@ -19,6 +19,15 @@ public abstract class BaseStack extends JPanel {
return stack.isEmpty();
}
public Card peekLastCard() {
if(!stackIsEmpty()) {
return stack.get(stack.size()-1);
} else {
return null;
}
}
public void addCard(Card _card) {
_card.setLocation(0, offset);
stack.add(_card);

3
src/main/java/solitaer/Card.java

@ -69,7 +69,8 @@ public class Card extends JPanel {
@Override
public String toString() {
return "Card [nr=" + nr + ", symbol=" + symbol + ", faceUp=" + faceUp + "]";
//return "Card [nr=" + nr + ", symbol=" + symbol + ", faceUp=" + faceUp + "]";
return "Card NR="+nr+" | Symbole="+symbol+" | Face="+faceUp;
}
@Override

31
src/main/java/solitaer/MyMouseListener.java

@ -8,6 +8,15 @@ import javax.swing.event.MouseInputListener;
public class MyMouseListener implements MouseInputListener {
private SaveStack save;
private BaseStack tempStack;
private Card tempCard;
public MyMouseListener(SaveStack _save) {
// TODO Auto-generated constructor stub
this.save = _save;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
@ -32,9 +41,27 @@ public class MyMouseListener implements MouseInputListener {
}
//Karte, die in einem StartStack liegt
else if(com instanceof Card && com.getParent() instanceof StartStack) {
System.out.println("Karte in StartStack"+com.toString());
tempCard = (Card) com;
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());
}
}
//System.out.println("Karte in StartStack"+com.toString());
((BaseStack)com.getParent()).removeLastCard();
//save.addCard(((BaseStack)com.getParent()).removeLastCard());;
}
}

38
src/main/java/solitaer/SaveStack.java

@ -0,0 +1,38 @@
package solitaer;
import java.awt.Color;
import java.awt.Graphics;
public class SaveStack extends BaseStack {
private static final long serialVersionUID = 1L;
public SaveStack(int _x, int _y) {
// TODO Auto-generated constructor stub
super();
super.setLocation(_x, _y);
super.setSize(72, 450);
super.setOpaque(false);
super.setLayout(null);
}
@Override
protected boolean cardCheck(Card _topStack, Card _playerCard) {
// TODO Auto-generated method stub
return false;
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
if (stackIsEmpty()) {
g.setColor(Color.pink);
g.fillRect(0, 0, this.getWidth(), 96);
g.setColor(Color.black);
g.drawRect(0, 0, this.getWidth() - 1, 96 - 1);
}
}
}

12
src/main/java/solitaer/SolitaerGamePanel.java

@ -11,11 +11,13 @@ public class SolitaerGamePanel extends JPanel {
private CardDeck deck;
private MyMouseListener mouseL;
private SaveStack save =new SaveStack(500,250);
public SolitaerGamePanel() {
setLayout(null);
mouseL = new MyMouseListener();
mouseL = new MyMouseListener(save);
this.addMouseListener(mouseL);
@ -25,12 +27,18 @@ public class SolitaerGamePanel extends JPanel {
StartStack a = new StartStack(25,25);
a.addCard(deck.getDeck().get(0));
deck.getDeck().get(1).setFaceUp(true);
deck.getDeck().get(1).setFaceUp(false);
a.addCard((deck.getDeck().get(1)));
this.add(a);
StartStack b = new StartStack(200,25);
this.add(b);
//save = new SaveStack(500,250);
this.add(save);
}

Loading…
Cancel
Save