Browse Source

Add BaseStack Refactor StartStack

feature_solitaer_MouseInput
Alexander Hartung 3 years ago
parent
commit
421cd8903f
  1. 49
      src/main/java/solitaer/BaseStack.java
  2. 20
      src/main/java/solitaer/MyMouseListener.java
  3. 27
      src/main/java/solitaer/StartStack.java

49
src/main/java/solitaer/BaseStack.java

@ -0,0 +1,49 @@
package solitaer;
import java.util.ArrayList;
import javax.swing.JPanel;
public abstract class BaseStack extends JPanel {
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 void addCard(Card _card) {
_card.setLocation(0, offset);
stack.add(_card);
RenderStackNew();
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() {
if (stack.size() != 0) {
Card LastCard = stack.remove(stack.size() - 1);
RenderStackNew();
return LastCard;
}
return null;
}
protected abstract boolean cardCheck(Card _topStack, Card _playerCard);
}

20
src/main/java/solitaer/MyMouseListener.java

@ -20,10 +20,22 @@ public class MyMouseListener implements MouseInputListener {
System.out.println("DOWN"); System.out.println("DOWN");
Component com = SwingUtilities.getDeepestComponentAt( Component com = SwingUtilities.getDeepestComponentAt(
e.getComponent(), e.getX(), e.getY()); e.getComponent(), e.getX(), e.getY());
System.out.println("########");
System.out.println(com.toString());
System.out.println(com.getParent().toString());
System.out.println("########");
// System.out.println("########");
// System.out.println(com.toString());
// System.out.println(com.getParent().toString());
// System.out.println("########");
//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) {
System.out.println("Karte in StartStack"+com.toString());
((BaseStack)com.getParent()).removeLastCard();
}
} }

27
src/main/java/solitaer/StartStack.java

@ -2,28 +2,22 @@ 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_WIDTH = 72;
private final int IMAGE_HIGHT = 450; 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, IMAGE_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,26 +43,13 @@ 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(), 96);
@ -77,4 +58,6 @@ public class StartStack extends JPanel {
} }
} }
} }
Loading…
Cancel
Save