From b8ad7aca0676b7a6343ca8953003cf3eaa27b098 Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 6 Feb 2022 15:30:15 +0100 Subject: [PATCH 1/7] Add SolitaerMenue --- src/main/java/hauptmenue/GameWindow.java | 7 +++--- src/main/java/solitaer/SolitaerMenue.java | 30 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/main/java/solitaer/SolitaerMenue.java diff --git a/src/main/java/hauptmenue/GameWindow.java b/src/main/java/hauptmenue/GameWindow.java index a2c7479..7fe612d 100644 --- a/src/main/java/hauptmenue/GameWindow.java +++ b/src/main/java/hauptmenue/GameWindow.java @@ -5,18 +5,19 @@ import java.awt.Dimension; import javax.swing.JFrame; import solitaer.SolitaerGamePanel; +import solitaer.SolitaerMenue; public class GameWindow extends JFrame { - /** - * - */ private static final long serialVersionUID = 1L; private final SolitaerGamePanel gamePanel = new SolitaerGamePanel(); + + private SolitaerMenue smenue = new SolitaerMenue(); public GameWindow() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gamePanel.setPreferredSize(new Dimension(1180, 780)); + this.setJMenuBar(smenue.getMenue()); add(gamePanel); pack(); diff --git a/src/main/java/solitaer/SolitaerMenue.java b/src/main/java/solitaer/SolitaerMenue.java new file mode 100644 index 0000000..e2c854b --- /dev/null +++ b/src/main/java/solitaer/SolitaerMenue.java @@ -0,0 +1,30 @@ +package solitaer; + +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; + +public class SolitaerMenue { + + JMenuBar menuBar = new JMenuBar(); + + JMenu menu = new JMenu("options"); + + JMenuItem restart = new JMenuItem("restart"); + JMenuItem quit = new JMenuItem("quit"); + + public SolitaerMenue() { + + this.menu.add(restart); + this.menu.addSeparator(); + this.menu.add(quit); + + this.menuBar.add(menu); + + } + + public JMenuBar getMenue() { + return menuBar; + } + +} From e902f029cebc1b7f86981d2b0a707772c20b108f Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 6 Feb 2022 15:37:30 +0100 Subject: [PATCH 2/7] Add MouseInput MenueQuit-Button --- src/main/java/solitaer/SolitaerMenue.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/solitaer/SolitaerMenue.java b/src/main/java/solitaer/SolitaerMenue.java index e2c854b..76a86ef 100644 --- a/src/main/java/solitaer/SolitaerMenue.java +++ b/src/main/java/solitaer/SolitaerMenue.java @@ -1,10 +1,13 @@ package solitaer; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; -public class SolitaerMenue { +public class SolitaerMenue implements ActionListener{ JMenuBar menuBar = new JMenuBar(); @@ -15,10 +18,13 @@ public class SolitaerMenue { public SolitaerMenue() { + this.restart.addActionListener(this); this.menu.add(restart); this.menu.addSeparator(); + this.quit.addActionListener(this); this.menu.add(quit); + this.menuBar.add(menu); } @@ -27,4 +33,14 @@ public class SolitaerMenue { return menuBar; } + @Override + public void actionPerformed(ActionEvent e) { + // TODO Auto-generated method stub + if(e.getSource() == this.quit) { + System.out.println("quit"); + System.exit(0); + } + + } + } From 2f4e989b9bedbece9ff9fab593fdb61a00767e61 Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 6 Feb 2022 15:50:21 +0100 Subject: [PATCH 3/7] Add methode restart --- src/main/java/solitaer/SolitaerGamePanel.java | 98 ++++++++++++++----- 1 file changed, 72 insertions(+), 26 deletions(-) diff --git a/src/main/java/solitaer/SolitaerGamePanel.java b/src/main/java/solitaer/SolitaerGamePanel.java index 26b1e18..7c9e70a 100644 --- a/src/main/java/solitaer/SolitaerGamePanel.java +++ b/src/main/java/solitaer/SolitaerGamePanel.java @@ -32,10 +32,10 @@ public class SolitaerGamePanel extends JPanel { public SolitaerGamePanel() { setLayout(null); - + int Offset_x_Save = setLayoutX(STARTSTACK_SIZE + 1); int Offset_y_Save = setLayouty(1); - + JLabel label = new JLabel(); label.setText("selected Cards"); label.setBounds(Offset_x_Save, Offset_y_Save - 25, 200, 25); @@ -43,25 +43,25 @@ public class SolitaerGamePanel extends JPanel { save = new SaveStack(Offset_x_Save, Offset_y_Save); this.add(save); - + mouseL = new MyMouseListener(save); this.addMouseListener(mouseL); - deck = new CardDeck(); - deck.shuffelDeck(); +// deck = new CardDeck(); +// deck.shuffelDeck(); for (int i = 0; i < STARTSTACK_SIZE; i++) { int y = setLayouty(1); start[i] = new StartStack(setLayoutX(i), y); - int tmp = i; - while(tmp != 0) { - start[i].addCard(deck.getDeck().remove(0)); - tmp -= 1; - } - Card card = deck.getDeck().remove(0); - card.setFaceUp(true); - start[i].addCard(card); - this.add(start[i]); +// int tmp = i; +// while (tmp != 0) { +// start[i].addCard(deck.getDeck().remove(0)); +// tmp -= 1; +// } +// Card card = deck.getDeck().remove(0); +// card.setFaceUp(true); +// start[i].addCard(card); +// this.add(start[i]); } for (int i = 0; i < ABLAGESTACK_SIZE; i++) { @@ -84,22 +84,24 @@ public class SolitaerGamePanel extends JPanel { default: break; } - this.add(ablage[i]); +// this.add(ablage[i]); } waste = new WasteStack(setLayoutX(1), OFFSET_STACKS_Y); geber = new GeberStack(setLayoutX(0), OFFSET_STACKS_Y, waste); + +// while (deck.getDeck().size() != 1) { +// geber.addCard(deck.getDeck().remove(0)); +// } +// +// Card card = deck.getDeck().remove(0); +// card.setFaceUp(true); +// waste.addCard(card); +// +// this.add(waste); +// this.add(geber); - while(deck.getDeck().size() != 1) { - geber.addCard(deck.getDeck().remove(0)); - } - - Card card = deck.getDeck().remove(0); - card.setFaceUp(true); - waste.addCard(card); - - this.add(waste); - this.add(geber); + restartGame(); } @@ -108,10 +110,54 @@ public class SolitaerGamePanel extends JPanel { } private int setLayouty(int _row) { - if(_row == 1) + if (_row == 1) return IMAGE_HIGHT + (OFFSET_STACKS_Y * 3); else return OFFSET_STACKS_Y; + } + + public void restartGame() { + deck = new CardDeck(); + deck.shuffelDeck(); + + for (int i = 0; i < STARTSTACK_SIZE; i++) { + int tmp = i; + while (tmp != 0) { + start[i].addCard(deck.getDeck().remove(0)); + tmp -= 1; + } + Card card = deck.getDeck().remove(0); + card.setFaceUp(true); + start[i].addCard(card); + this.add(start[i]); + + } + + + + for (int i = 0; i < ABLAGESTACK_SIZE; i++) { + this.add(ablage[i]); + } + + + while (deck.getDeck().size() != 1) { + geber.addCard(deck.getDeck().remove(0)); + } + + Card card = deck.getDeck().remove(0); + card.setFaceUp(true); + waste.addCard(card); + + this.add(waste); + this.add(geber); + + + + + + + + } @Override From c57f31ac025346a9f0ad5671871ffbd96eb8de57 Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 6 Feb 2022 15:54:29 +0100 Subject: [PATCH 4/7] Add clearStack methode to BaseStack --- src/main/java/solitaer/BaseStack.java | 4 ++++ src/main/java/solitaer/SolitaerGamePanel.java | 11 +++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/solitaer/BaseStack.java b/src/main/java/solitaer/BaseStack.java index a94321d..5e78f37 100644 --- a/src/main/java/solitaer/BaseStack.java +++ b/src/main/java/solitaer/BaseStack.java @@ -24,6 +24,10 @@ public abstract class BaseStack extends JPanel { } offset = 0; } + + public void clearStack() { + stack.clear(); + } public boolean stackIsEmpty() { return stack.isEmpty(); diff --git a/src/main/java/solitaer/SolitaerGamePanel.java b/src/main/java/solitaer/SolitaerGamePanel.java index 7c9e70a..9a4bc87 100644 --- a/src/main/java/solitaer/SolitaerGamePanel.java +++ b/src/main/java/solitaer/SolitaerGamePanel.java @@ -42,7 +42,7 @@ public class SolitaerGamePanel extends JPanel { this.add(label); save = new SaveStack(Offset_x_Save, Offset_y_Save); - this.add(save); + mouseL = new MyMouseListener(save); this.addMouseListener(mouseL); @@ -121,6 +121,7 @@ public class SolitaerGamePanel extends JPanel { deck.shuffelDeck(); for (int i = 0; i < STARTSTACK_SIZE; i++) { + start[i].clearStack(); int tmp = i; while (tmp != 0) { start[i].addCard(deck.getDeck().remove(0)); @@ -136,10 +137,12 @@ public class SolitaerGamePanel extends JPanel { for (int i = 0; i < ABLAGESTACK_SIZE; i++) { + ablage[i].clearStack(); this.add(ablage[i]); } - + geber.clearStack(); + waste.clearStack(); while (deck.getDeck().size() != 1) { geber.addCard(deck.getDeck().remove(0)); } @@ -150,6 +153,10 @@ public class SolitaerGamePanel extends JPanel { this.add(waste); this.add(geber); + + + save.clearStack(); + this.add(save); From d478ae0cc636aeca585e8a080c88fa03c3f1f04b Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 6 Feb 2022 15:56:55 +0100 Subject: [PATCH 5/7] Add MouseInput MenueRestart-Button --- src/main/java/hauptmenue/GameWindow.java | 2 +- src/main/java/solitaer/SolitaerMenue.java | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/hauptmenue/GameWindow.java b/src/main/java/hauptmenue/GameWindow.java index 7fe612d..9230044 100644 --- a/src/main/java/hauptmenue/GameWindow.java +++ b/src/main/java/hauptmenue/GameWindow.java @@ -12,7 +12,7 @@ public class GameWindow extends JFrame { private static final long serialVersionUID = 1L; private final SolitaerGamePanel gamePanel = new SolitaerGamePanel(); - private SolitaerMenue smenue = new SolitaerMenue(); + private SolitaerMenue smenue = new SolitaerMenue(gamePanel); public GameWindow() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); diff --git a/src/main/java/solitaer/SolitaerMenue.java b/src/main/java/solitaer/SolitaerMenue.java index 76a86ef..e3dbcb8 100644 --- a/src/main/java/solitaer/SolitaerMenue.java +++ b/src/main/java/solitaer/SolitaerMenue.java @@ -9,6 +9,8 @@ import javax.swing.JMenuItem; public class SolitaerMenue implements ActionListener{ + SolitaerGamePanel solitaerpanel; + JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("options"); @@ -16,7 +18,9 @@ public class SolitaerMenue implements ActionListener{ JMenuItem restart = new JMenuItem("restart"); JMenuItem quit = new JMenuItem("quit"); - public SolitaerMenue() { + public SolitaerMenue(SolitaerGamePanel _panel) { + + solitaerpanel = _panel; this.restart.addActionListener(this); this.menu.add(restart); @@ -41,6 +45,10 @@ public class SolitaerMenue implements ActionListener{ System.exit(0); } + if(e.getSource() == this.restart) { + solitaerpanel.restartGame(); + } + } } From 8a594b8918a445d087cb3198f92ce4efd1d704f3 Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 6 Feb 2022 16:13:55 +0100 Subject: [PATCH 6/7] Fix render when restart --- src/main/java/solitaer/BaseStack.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/solitaer/BaseStack.java b/src/main/java/solitaer/BaseStack.java index 5e78f37..686b60f 100644 --- a/src/main/java/solitaer/BaseStack.java +++ b/src/main/java/solitaer/BaseStack.java @@ -27,6 +27,8 @@ public abstract class BaseStack extends JPanel { public void clearStack() { stack.clear(); + offset = 0; + RenderStackNew(); } public boolean stackIsEmpty() { From 6722a62792900ca9e5a7f5650aab2c3c1a8f1c5f Mon Sep 17 00:00:00 2001 From: Alexander Hartung Date: Sun, 6 Feb 2022 16:20:39 +0100 Subject: [PATCH 7/7] Refactor --- src/main/java/solitaer/BaseStack.java | 4 +- src/main/java/solitaer/SolitaerGamePanel.java | 45 ++----------------- src/main/java/solitaer/SolitaerMenue.java | 3 +- 3 files changed, 7 insertions(+), 45 deletions(-) diff --git a/src/main/java/solitaer/BaseStack.java b/src/main/java/solitaer/BaseStack.java index 686b60f..2b17330 100644 --- a/src/main/java/solitaer/BaseStack.java +++ b/src/main/java/solitaer/BaseStack.java @@ -12,8 +12,8 @@ public abstract class BaseStack extends JPanel { private static final long serialVersionUID = 1L; private ArrayList stack = new ArrayList(); private final int OFFSET_VALUE = 20; - int offset; - int addOffset; + private int offset; + private int addOffset; public BaseStack(boolean _offset) { // TODO Auto-generated constructor stub diff --git a/src/main/java/solitaer/SolitaerGamePanel.java b/src/main/java/solitaer/SolitaerGamePanel.java index 9a4bc87..4df5655 100644 --- a/src/main/java/solitaer/SolitaerGamePanel.java +++ b/src/main/java/solitaer/SolitaerGamePanel.java @@ -1,7 +1,6 @@ package solitaer; import java.awt.Color; -import java.awt.Font; import java.awt.Graphics; import javax.swing.JLabel; @@ -19,6 +18,8 @@ public class SolitaerGamePanel extends JPanel { public final int STARTSTACK_SIZE = 7; public final int ABLAGESTACK_SIZE = 4; + + private Color backgroundColor = new Color(0, 135, 62); private CardDeck deck; private MyMouseListener mouseL; @@ -42,26 +43,13 @@ public class SolitaerGamePanel extends JPanel { this.add(label); save = new SaveStack(Offset_x_Save, Offset_y_Save); - mouseL = new MyMouseListener(save); this.addMouseListener(mouseL); -// deck = new CardDeck(); -// deck.shuffelDeck(); - for (int i = 0; i < STARTSTACK_SIZE; i++) { int y = setLayouty(1); start[i] = new StartStack(setLayoutX(i), y); -// int tmp = i; -// while (tmp != 0) { -// start[i].addCard(deck.getDeck().remove(0)); -// tmp -= 1; -// } -// Card card = deck.getDeck().remove(0); -// card.setFaceUp(true); -// start[i].addCard(card); -// this.add(start[i]); } for (int i = 0; i < ABLAGESTACK_SIZE; i++) { @@ -84,22 +72,10 @@ public class SolitaerGamePanel extends JPanel { default: break; } -// this.add(ablage[i]); } waste = new WasteStack(setLayoutX(1), OFFSET_STACKS_Y); geber = new GeberStack(setLayoutX(0), OFFSET_STACKS_Y, waste); - -// while (deck.getDeck().size() != 1) { -// geber.addCard(deck.getDeck().remove(0)); -// } -// -// Card card = deck.getDeck().remove(0); -// card.setFaceUp(true); -// waste.addCard(card); -// -// this.add(waste); -// this.add(geber); restartGame(); @@ -131,11 +107,8 @@ public class SolitaerGamePanel extends JPanel { card.setFaceUp(true); start[i].addCard(card); this.add(start[i]); - } - - for (int i = 0; i < ABLAGESTACK_SIZE; i++) { ablage[i].clearStack(); this.add(ablage[i]); @@ -146,7 +119,6 @@ public class SolitaerGamePanel extends JPanel { while (deck.getDeck().size() != 1) { geber.addCard(deck.getDeck().remove(0)); } - Card card = deck.getDeck().remove(0); card.setFaceUp(true); waste.addCard(card); @@ -154,24 +126,15 @@ public class SolitaerGamePanel extends JPanel { this.add(waste); this.add(geber); - save.clearStack(); - this.add(save); - - - - - - - - + this.add(save); } @Override protected void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g); - g.setColor(new Color(0, 135, 62)); + g.setColor(backgroundColor); g.fillRect(0, 0, this.getWidth(), this.getHeight()); } } diff --git a/src/main/java/solitaer/SolitaerMenue.java b/src/main/java/solitaer/SolitaerMenue.java index e3dbcb8..9845702 100644 --- a/src/main/java/solitaer/SolitaerMenue.java +++ b/src/main/java/solitaer/SolitaerMenue.java @@ -18,7 +18,7 @@ public class SolitaerMenue implements ActionListener{ JMenuItem restart = new JMenuItem("restart"); JMenuItem quit = new JMenuItem("quit"); - public SolitaerMenue(SolitaerGamePanel _panel) { + public SolitaerMenue(SolitaerGamePanel _panel) { solitaerpanel = _panel; @@ -28,7 +28,6 @@ public class SolitaerMenue implements ActionListener{ this.quit.addActionListener(this); this.menu.add(quit); - this.menuBar.add(menu); }