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

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

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

@ -69,7 +69,8 @@ public class Card extends JPanel {
@Override @Override
public String toString() { 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 @Override

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

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

@ -10,12 +10,14 @@ public class SolitaerGamePanel extends JPanel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private CardDeck deck; private CardDeck deck;
private MyMouseListener mouseL; private MyMouseListener mouseL;
private SaveStack save =new SaveStack(500,250);
public SolitaerGamePanel() { public SolitaerGamePanel() {
setLayout(null); setLayout(null);
mouseL = new MyMouseListener();
mouseL = new MyMouseListener(save);
this.addMouseListener(mouseL); this.addMouseListener(mouseL);
@ -25,12 +27,18 @@ 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(true);
deck.getDeck().get(1).setFaceUp(false);
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);
this.add(b); this.add(b);
//save = new SaveStack(500,250);
this.add(save);
} }

Loading…
Cancel
Save