package unogui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import playlogic.Playlogic;
public class Unogui extends JFrame implements ActionListener {
private Playlogic newGuigame;
private JMenuBar menu = new JMenuBar();
private JMenu menu1 = new JMenu("Game");
private JMenu menu2 = new JMenu("Help");
private JMenuItem item1 = new JMenuItem("Start game");
private JMenuItem item2 = new JMenuItem("Exit game");
private JMenuItem item3 = new JMenuItem("Display Rules");
private JMenuItem item4 = new JMenuItem("About");
private JLabel playersName = new JLabel(" ");
private JButton deckButton;
private JPanel middle = new JPanel();
private JPanel leftpanel = new JPanel();
private JPanel centerpanel = new JPanel();
private JPanel rightpanel = new JPanel();
private JPanel bottom = new JPanel();
private JPanel top = new JPanel();
private JComboBox userCards = new JComboBox();
DefaultComboBoxModel cbxModel = new DefaultComboBoxModel();
private static final int GRIDMAGIC_H = 6;
private static final int GRIDMAGIC_W = 3;
private GridLayout grid = new GridLayout(GRIDMAGIC_H, 0);
private GridLayout grida = new GridLayout(0, GRIDMAGIC_W);
private GridLayout gridb = new GridLayout(2, 0);
private static final int UNOWIDTH = 1024;
private static final int UNOHEIGHT = 720;
private static final int MENUHEIGHT = 25;
private static final int MIDDLEHEIGHT = 695;
private static final int RIGHTHEIGHT = 200;
private static final int COLORPANELWIDTH = 150;
private static final int COLORPANELHEIGHT = 20;
private static final int LEFTWIDTH = 150;
private static final int CENTERWIDTH = 350;
private static final int RIGHTWIDTH = 300;
private static final int FIFTY = 50;
private static final int FONTSIZE = 24;
private static final int FONTSIZENAME = 18;
private static final int ZERO = 0;
private static final int ONE = 1;
private static final int TWO = 2;
private static final int THREE = 3;
private static final int FOUR = 4;
public Playlogic getPlaylogic() {
return newGuigame;
}
public Unogui() {
final String playground = "/unoimagesnew/hintergrund_spielflaeche.jpg";
this.setTitle("UNO");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(UNOWIDTH, UNOHEIGHT));
this.setResizable(false);
this.menu.setOpaque(true);
this.menu.setPreferredSize(new Dimension(UNOWIDTH, MENUHEIGHT));
this.menu.add(this.menu1);
this.menu1.add(this.item1);
this.menu1.add(this.item2);
this.menu.add(this.menu2);
this.item1.addActionListener(this);
this.item2.addActionListener(this);
this.item3.addActionListener(this);
this.item4.addActionListener(this);
this.menu2.add(this.item3);
this.menu2.add(this.item4);
this.setContentPane(new Backgroundpanel(playground));
this.middle.setPreferredSize(new Dimension(UNOWIDTH, MIDDLEHEIGHT));
this.middle.setOpaque(false);
this.leftpanel.setOpaque(false);
this.centerpanel.setOpaque(false);
this.rightpanel.setOpaque(false);
this.top.setOpaque(false);
this.bottom.setOpaque(false);
this.add(this.middle);
this.middle.setLayout(gridb);
this.middle.add(top);
this.top.setLayout(grida);
this.middle.add(bottom);
this.top.add(this.leftpanel);
this.top.add(this.centerpanel);
this.top.add(this.rightpanel);
this.userCards.setPreferredSize(new Dimension(RIGHTWIDTH, FONTSIZE));
this.rightpanel.setLayout(grid);
this.rightpanel.add(playersName);
this.leftpanel.setPreferredSize(new Dimension(LEFTWIDTH, MIDDLEHEIGHT));
this.centerpanel.setPreferredSize(new Dimension(CENTERWIDTH, MIDDLEHEIGHT));
this.rightpanel.setPreferredSize(new Dimension(RIGHTWIDTH, RIGHTHEIGHT));
this.userCards.setPreferredSize(new Dimension(COLORPANELWIDTH, COLORPANELHEIGHT));
userCards.setModel(cbxModel);
this.setJMenuBar(this.menu);
this.setLayout(new FlowLayout());
this.pack();
Center.centerOnScreen(this);
this.setVisible(true);
JOptionPane.showMessageDialog(this, "Welcome to UNO", "Welcome", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void actionPerformed(final ActionEvent e) {
if (e.getSource().equals(this.item2)) {
final int n = JOptionPane.showConfirmDialog(this, "Do you realy want to Exit the game?", "Exit game", JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
if (e.getSource().equals(this.item1)) {
String name = JOptionPane.showInputDialog(null, "Enter your Name: ");
playlogic.Global_vars.setName(name);
playersName.setText(playlogic.Global_vars.getName() + "'s deck");
playersName.setFont(new Font("Serif", Font.BOLD, FONTSIZENAME));
playersName.setForeground(Color.WHITE);
this.rightpanel.add(this.userCards);
final int start = JOptionPane.showConfirmDialog(this, "Do you want to start a new game?", "Start game", JOptionPane.YES_NO_OPTION);
if (start == JOptionPane.YES_OPTION) {
String uri = "/unoimagesnew/deck.png";
deckButton = new JButton(" ", createImageIcon(uri));
leftpanel.add(deckButton);
deckButton.setActionCommand("newCard");
deckButton.addActionListener(this);
deckButton.setOpaque(false);
deckButton.setContentAreaFilled(false);
this.newGuigame = new playlogic.Playlogic();
final unocards.Card card = this.newGuigame.getUsed().showCard(0);
guiPlay(card);
}
}
if (e.getSource().equals(this.item4)) {
double version = playlogic.Global_vars.getVersion();
String string = "Copyright Felix Hohlwegler & Duy Nguyen " + "Version: " + version;
JOptionPane.showMessageDialog(this, string, "About", JOptionPane.INFORMATION_MESSAGE);
}
if ("newCard".endsWith(e.getActionCommand())) {
this.newGuigame.newCard(this.newGuigame.getOrder());
this.cardsOut(this.newGuigame.getOrder());
}
}
public void guiPlay(unocards.Card card) {
this.setColorPanel(card);
if (this.newGuigame.getOrder() == 0) {
cardsOut(this.newGuigame.getOrder());
this.setColorPanel(this.newGuigame.getUsed().showCard(0));
this.newGuigame.setOrder(1);
} else {
this.newGuigame.computersTURN(this.newGuigame.getOrder());
this.setColorPanel(this.newGuigame.getUsed().showCard(0));
this.newGuigame.setOrder(0);
}
}
public void cardsOut(final int p) {
cbxModel.removeAllElements();
for (int i = 0; i < this.newGuigame.getPlayers().get(p).getSize(); i++) {
unocards.Card card = this.newGuigame.getPlayers().get(p).showCard(i);
String color = unocards.TransformCard.transformColor(card);
String value = unocards.TransformCard.transformValue(card);
String output = color + " --> " + value;
cbxModel.addElement(output);
}
}
public void setColorPanel(final unocards.Card card) {
ImageIcon image;
switch (card.getColor()) {
case ZERO:
final String red = "/unoimagesnew/rot.png";
image = createImageIcon(red);
setCard(card, image);
break;
case ONE:
final String green = "/unoimagesnew/gruen.png";
image = createImageIcon(green);
setCard(card, image);
break;
case TWO:
final String blue = "/unoimagesnew/blau.png";
image = createImageIcon(blue);
setCard(card, image);
break;
case THREE:
final String yellow = "/unoimagesnew/gelb.png";
image = createImageIcon(yellow);
setCard(card, image);
break;
case FOUR:
final String black = "/unoimagesnew/schwarz.png";
image = createImageIcon(black);
setCard(card, image);
break;
}
}
private void setCard(unocards.Card card, ImageIcon image) {
JLabel cardLabel;
centerpanel.removeAll();
cardLabel = new JLabel(image);
this.centerpanel.add(cardLabel);
cardLabel.setLocation(0, FIFTY);
cardLabel.setText(unocards.TransformCard.transformValue(card));
}
protected ImageIcon createImageIcon(final String path) {
final java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}