Browse Source

Merge commit '5aab0411798e7bbd41c70e75d0a2bfc51972ce0e' into HEAD

feature_solitaer_AblageStack
Jenkins 3 years ago
parent
commit
c5fee840c8
  1. 69
      src/main/java/solitaer/BaseStack.java
  2. 3
      src/main/java/solitaer/Card.java
  3. 114
      src/main/java/solitaer/MyMouseListener.java
  4. 38
      src/main/java/solitaer/SaveStack.java
  5. 51
      src/main/java/solitaer/SolitaerGamePanel.java
  6. 39
      src/main/java/solitaer/StartStack.java

69
src/main/java/solitaer/BaseStack.java

@ -0,0 +1,69 @@
package solitaer;
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;
int offset;
public BaseStack() {
// TODO Auto-generated constructor stub
offset = 0;
}
public boolean stackIsEmpty() {
return stack.isEmpty();
}
public Card peekLastCard() {
if(!stackIsEmpty()) {
return stack.get(stack.size()-1);
} else {
return null;
}
}
public int indexOfCard(Card _card) {
return stack.indexOf(_card);
}
public int stackSize() {
return stack.size();
}
public void addCard(Card _card) {
_card.setLocation(0, offset);
stack.add(_card);
RenderStackNew();
offset += OFFSET_VALUE;
}
public Card removeLastCard() {
if (stack.size() != 0) {
Card LastCard = stack.remove(stack.size() - 1);
offset -= OFFSET_VALUE;
RenderStackNew();
return LastCard;
}
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);
}

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

114
src/main/java/solitaer/MyMouseListener.java

@ -0,0 +1,114 @@
package solitaer;
import java.awt.Component;
import java.awt.event.MouseEvent;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;
public class MyMouseListener implements MouseInputListener {
private SaveStack saveStack;
private BaseStack tempStack;
private BaseStack selectedStack;
private Card tempCard;
public MyMouseListener(SaveStack _save) {
// TODO Auto-generated constructor stub
this.saveStack = _save;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("DOWN");
Component com = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY());
// Leeres StartFeld
if (com instanceof StartStack) {
System.out.println("Leer Start");
}
// Karte, die in einem StartStack liegt
else if (com instanceof Card && com.getParent() instanceof StartStack) {
tempCard = (Card) com;
tempStack = (StartStack) com.getParent();
if (saveStack.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) {
saveStack.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(), saveStack.peekLastCard())) {
while(!saveStack.stackIsEmpty()) {
tempStack.addCard(saveStack.removeLastCard());
}
} else {
//Alles wieder auf den Ursprünglichen Stack packen
while(!saveStack.stackIsEmpty()) {
selectedStack.addCard(saveStack.removeLastCard());
}
}
selectedStack = null;
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}

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(IMAGE_WIDTH, STACK_HIGHT);
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(), IMAGE_HIGHT);
g.setColor(Color.black);
g.drawRect(0, 0, this.getWidth() - 1, IMAGE_HIGHT - 1);
}
}
}

51
src/main/java/solitaer/SolitaerGamePanel.java

@ -7,21 +7,62 @@ import javax.swing.JPanel;
public class SolitaerGamePanel extends JPanel { public class SolitaerGamePanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private CardDeck deck;
private MyMouseListener mouseL;
private SaveStack save =new SaveStack(500,250);
public SolitaerGamePanel() { public SolitaerGamePanel() {
setLayout(null); setLayout(null);
mouseL = new MyMouseListener(save);
this.addMouseListener(mouseL);
deck = new CardDeck();
StartStack a = new StartStack(25,25); StartStack a = new StartStack(25,25);
a.addCard(new Card(13, Symbol.Karo, true));
a.addCard(new Card(1, Symbol.Karo, true));
a.addCard(deck.getDeck().get(0));
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)));
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);
this.add(save);
} }

39
src/main/java/solitaer/StartStack.java

@ -2,28 +2,18 @@ package solitaer;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
public class StartStack extends JPanel {
public class StartStack extends BaseStack {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private ArrayList<Card> stack = new ArrayList<Card>();
private final int OFFSET_VALUE = 20;
private final int IMAGE_WIDTH = 72;
private final int IMAGE_HIGHT = 450;
private int offset;
public StartStack(int _x, int _y) { public StartStack(int _x, int _y) {
super();
super.setLocation(_x, _y); super.setLocation(_x, _y);
super.setSize(IMAGE_WIDTH, IMAGE_HIGHT);
super.setSize(IMAGE_WIDTH, STACK_HIGHT);
super.setOpaque(false); super.setOpaque(false);
super.setLayout(null); super.setLayout(null);
offset = 0;
} }
public boolean cardCheck(Card _topStack, Card _playerCard) { public boolean cardCheck(Card _topStack, Card _playerCard) {
@ -49,32 +39,21 @@ public class StartStack extends JPanel {
} }
} }
public void addCard(Card _card) {
_card.setLocation(0, offset);
stack.add(_card);
addtoPanel();
offset += OFFSET_VALUE;
}
private void addtoPanel() {
this.removeAll();
//Alles neu Zeichnen
for (int i = stack.size() - 1; i >= 0; i--) {
this.add(stack.get(i));
}
}
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
super.paintComponent(g); super.paintComponent(g);
if (stack.size() == 0) {
if (stackIsEmpty()) {
g.setColor(Color.gray); g.setColor(Color.gray);
g.fillRect(0, 0, this.getWidth(), 96);
g.fillRect(0, 0, this.getWidth(), IMAGE_HIGHT);
g.setColor(Color.black); 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