Browse Source
Merge commit '5aab0411798e7bbd41c70e75d0a2bfc51972ce0e' into HEAD
feature_solitaer_AblageStack
Merge commit '5aab0411798e7bbd41c70e75d0a2bfc51972ce0e' into HEAD
feature_solitaer_AblageStack
Jenkins
3 years ago
6 changed files with 278 additions and 36 deletions
-
69src/main/java/solitaer/BaseStack.java
-
3src/main/java/solitaer/Card.java
-
114src/main/java/solitaer/MyMouseListener.java
-
38src/main/java/solitaer/SaveStack.java
-
51src/main/java/solitaer/SolitaerGamePanel.java
-
37src/main/java/solitaer/StartStack.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); |
|||
} |
@ -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 |
|||
|
|||
} |
|||
|
|||
} |
@ -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); |
|||
} |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue