package unogui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import playlogic.Playlogic;
/**
* this class defines the uno gui
*
* @author Holz
*/
public class Unogui extends JFrame implements ActionListener {
//Playlogic
private Playlogic newGuigame;
//Gui Elements
//Menu
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");
//Players name
private JLabel playersName = new JLabel(" ");
private JButton deckButton;
//Panels
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();
//Card choose box
private JComboBox userCards = new JComboBox();
DefaultComboBoxModel cbxModel = new DefaultComboBoxModel();
//GRIDS
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);
//Dimension
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;
//Fontsize for welcome label
private static final int FONTSIZE = 24;
private static final int FONTSIZENAME = 18;
//Magic number corrections
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;
/**
* New game will be started
*
* @return new Game
*/
public Playlogic getPlaylogic() {
return newGuigame;
}
/**
* Initialisation of Gui-Plaaygorund
*/
public Unogui() {
/*
* ---------------------------------------------------------------------
* Bild pfade
* ---------------------------------------------------------------------
*/
final String playground = "/unoimagesnew/hintergrund_spielflaeche.jpg";
/*
* ---------------------------------------------------------------------
* Uno Spielfläche anlegen --> Jframe
* ---------------------------------------------------------------------
*/
// Window Title
this.setTitle("UNO");
// Exit Button
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Window size
this.setPreferredSize(new Dimension(UNOWIDTH, UNOHEIGHT));
// not resizeable
this.setResizable(false);
/*
* ----------------------------------------------------------------------
* Window Menu Bar
* -------------------------------------------------------------------
*/
this.menu.setOpaque(true);
this.menu.setPreferredSize(new Dimension(UNOWIDTH, MENUHEIGHT));
/*
* ----------------------------------------------------------------------
* Menu1
* -------------------------------------------------------------------
*/
// add Menu to Menubar
this.menu.add(this.menu1);
// add Submenus to Menu 1
this.menu1.add(this.item1);
this.menu1.add(this.item2);
/*
* ----------------------------------------------------------------------
* Menu2
* -------------------------------------------------------------------
*/
// add Menu to Menubar
this.menu.add(this.menu2);
// SubItem 1
this.item1.addActionListener(this);
// SubItem 2
this.item2.addActionListener(this);
// SubItem 3
this.item3.addActionListener(this);
// SubItem 5
this.item4.addActionListener(this);
// add Submenus to Menu 1
this.menu2.add(this.item3);
this.menu2.add(this.item4);
/*
* ----------------------------------------------------------------------
* Load Background image
* -------------------------------------------------------------------
*/
this.setContentPane(new Backgroundpanel(playground));
/*
* ----------------------------------------------------------------------
* Border Layout middle = whole panel in the middle --> will be split
* into 3 panels
* -------------------------------------------------------------------
*/
// size
this.middle.setPreferredSize(new Dimension(UNOWIDTH, MIDDLEHEIGHT));
// transparency for all panels
this.middle.setOpaque(false);
this.leftpanel.setOpaque(false);
this.centerpanel.setOpaque(false);
this.rightpanel.setOpaque(false);
this.top.setOpaque(false);
this.bottom.setOpaque(false);
// middle panel add to JFrame
this.add(this.middle);
// add Panels to middle --> middle border Layout
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));
//Rightpanel
this.rightpanel.setLayout(grid);
this.rightpanel.add(playersName);
// add colorPanel on leftpanel
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);
/*
* ----------------------------------------------------------------------
* actual Color on top of deck
* -------------------------------------------------------------------
*/
// Jlabel added to Window
this.setJMenuBar(this.menu);
this.setLayout(new FlowLayout());
this.pack();
Center.centerOnScreen(this);
this.setVisible(true);
//Show Welcome Panel
JOptionPane.showMessageDialog(this, "Welcome to UNO", "Welcome", JOptionPane.INFORMATION_MESSAGE);
}
/*
* --------------------------------------------------------------------------
* Action Listener
* ------------------------------------------------------------------------
*/
@Override
public void actionPerformed(final ActionEvent e) {
/*
* ----------------------------------------------------------------------
* Exit Game
* ----------------------------------------------------------------------
*/
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);
}
}
/*
* ----------------------------------------------------------------------
* Start new Game
* ----------------------------------------------------------------------
*/
if (e.getSource().equals(this.item1)) {
//Get Players Name
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);
//Add Panels
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) {
//add deck to Playground
String uri = "/unoimagesnew/deck.png";
//Deck Button --> for User to get new Cards
deckButton = new JButton(" ", createImageIcon(uri));
leftpanel.add(deckButton);
deckButton.setActionCommand("newCard");
deckButton.addActionListener(this);
deckButton.setOpaque(false);
deckButton.setContentAreaFilled(false);
// Start game
this.newGuigame = new playlogic.Playlogic();
// Color of top Card
final unocards.Card card = this.newGuigame.getUsed().showCard(0);
guiPlay(card);
}
}
/*
* ----------------------------------------------------------------------
* About dialog
* ----------------------------------------------------------------------
*/
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);
}
/*
* ----------------------------------------------------------------------
* New Card
* ----------------------------------------------------------------------
*/
if ("newCard".endsWith(e.getActionCommand())) {
this.newGuigame.newCard(this.newGuigame.getOrder());
this.cardsOut(this.newGuigame.getOrder());
}
}
/**
* Gui Play sets the playing order
*
* @param card
*/
public void guiPlay(unocards.Card card) {
this.setColorPanel(card);
//Centerpanel
//Humans Turn
if (this.newGuigame.getOrder() == 0) {
//Users Cards Reload
cardsOut(this.newGuigame.getOrder());
//TopCard Output
this.setColorPanel(this.newGuigame.getUsed().showCard(0));
//Set ComputersTurn
this.newGuigame.setOrder(1);
} else {
//Computers TURN
this.newGuigame.computersTURN(this.newGuigame.getOrder());
//Top Card Output
this.setColorPanel(this.newGuigame.getUsed().showCard(0));
//Set PlayersTurn
this.newGuigame.setOrder(0);
}
}
/**
* Human Players cards should be added to dropdown list
*
* @param p players number
*/
public void cardsOut(final int p) {
cbxModel.removeAllElements();
for (int i = 0; i < this.newGuigame.getPlayers().get(p).getSize(); i++) {
// Gets all Player Cards
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);
}
}
/**
* Color Panel shows the top cards color
*
* @param card
*/
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;
}
}
/**
* Color Panel shows the top cards color
*
* @param card chosen Card
* @param image set image for card color
*/
private void setCard(unocards.Card card, ImageIcon image) {
//Card label
JLabel cardLabel;
//Panel clean
centerpanel.removeAll();
//Add Uno Card
cardLabel = new JLabel(image);
//Add Label to Panel
this.centerpanel.add(cardLabel);
//Set location
cardLabel.setLocation(0, FIFTY);
//Set Text
cardLabel.setText(unocards.TransformCard.transformValue(card));
}
/**
* Creates images
* @param path file Path (ImagePath)
* @return new Image
*/
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;
}
}
}