import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class prs extends JPanel implements ActionListener, ItemListener
{
private final Point FRAME_ORIGIN = new Point(450,250);
private final Color clrBackground = new Color(255,255,255);
private final Color clrForeground = new Color(0,0,0);
private JComboBox cboxWeapon;
private JTextField txtCPUWeapon, txtWins, txtLoses, txtDraws;
private JLabel lblPlayerWeapon, lblCPUWeapon, lblWins, lblLoses, lblDraws, lblStatus, lblPlayerWeaponIcon, lblCPUWeaponIcon;
private JButton cmdPlay, cmdReset;
private ImageIcon[] imgWeapon;
private JPanel panRoot, panPlayerArea, panPlayerWeapon, panCPUArea, panCPUWeapon, panStatusArea, panGo, panCounters, panWins, panLoses, panDraws;
private prsEngine engine = new prsEngine();
private objCreateAppletImage createImage = new objCreateAppletImage();
private boolean errorWithImages = false;
public static void main(String[] args)
{
JFrame frame = new JFrame("Paper Rock Scissors Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JComponent paneMain = new prs();
paneMain.setOpaque(true);
paneMain.setPreferredSize(new Dimension(420,350));
frame.setContentPane(paneMain);
frame.pack();
frame.setVisible(true);
}
public prs ()
{
cboxWeapon = new JComboBox(engine.getWeapon());
cboxWeapon.addItemListener(this);
txtCPUWeapon = new JTextField(engine.getStrCPUWeapon(), 5);
txtWins = new JTextField("0", 5);
txtLoses = new JTextField("0", 5);
txtDraws = new JTextField("0", 5);
txtCPUWeapon.setEditable(false);
txtWins.setEditable(false);
txtLoses.setEditable(false);
txtDraws.setEditable(false);
lblPlayerWeapon = new JLabel("Choose Your Weapon:", JLabel.CENTER);
lblCPUWeapon = new JLabel("PC's Weapon:", JLabel.CENTER);
lblWins = new JLabel("Amount of Wins:", JLabel.RIGHT);
lblLoses = new JLabel("Amount of Loses:", JLabel.RIGHT);
lblDraws = new JLabel("Amount of Draws:", JLabel.RIGHT);
lblStatus = new JLabel("", JLabel.CENTER);
lblPlayerWeaponIcon = new JLabel("", JLabel.CENTER);
lblCPUWeaponIcon = new JLabel("", JLabel.CENTER);
lblPlayerWeaponIcon.setPreferredSize(new Dimension(150,150));
lblCPUWeaponIcon.setPreferredSize(new Dimension(150,150));
cmdPlay = new JButton("Go!");
cmdReset = new JButton("Restart");
cmdPlay.addActionListener(this);
cmdReset.addActionListener(this);
try
{
imgWeapon = new ImageIcon[3];
for (int i = 0; i < 3; i++)
{
imgWeapon[i] = createImage.getImageIcon(this, "/" + engine.getWeapon(i) + ".jpg", "Icon for " + engine.getWeapon(i), 13000); // images place at the same directory!
}
lblPlayerWeaponIcon.setIcon(imgWeapon[0]);
lblCPUWeaponIcon.setIcon(imgWeapon[0]);
}
catch (Exception ex) //The game works without the images, so carry on
{
errorWithImages = true;
}
setLayout(new BorderLayout());
panRoot = new JPanel(new BorderLayout());
panPlayerArea = new JPanel(new BorderLayout());
panPlayerWeapon = new JPanel(new BorderLayout());
panCPUArea = new JPanel(new BorderLayout());
panCPUWeapon = new JPanel(new BorderLayout());
panStatusArea = new JPanel(new BorderLayout());
panGo = new JPanel();
panCounters = new JPanel(new GridLayout(3,1,2,2));
panWins = new JPanel();
panLoses = new JPanel();
panDraws = new JPanel();
add(panRoot, BorderLayout.CENTER);
panRoot.add(panPlayerArea, BorderLayout.WEST);
panPlayerArea.add(panPlayerWeapon, BorderLayout.NORTH);
panPlayerWeapon.add(lblPlayerWeapon, BorderLayout.NORTH);
panPlayerWeapon.add(cboxWeapon, BorderLayout.SOUTH);
panPlayerArea.add(lblPlayerWeaponIcon, BorderLayout.SOUTH);
panRoot.add(panCPUArea, BorderLayout.EAST);
panCPUArea.add(panCPUWeapon, BorderLayout.NORTH);
panCPUWeapon.add(lblCPUWeapon, BorderLayout.NORTH);
panCPUWeapon.add(txtCPUWeapon, BorderLayout.SOUTH);
panCPUArea.add(lblCPUWeaponIcon, BorderLayout.SOUTH);
panRoot.add(panStatusArea, BorderLayout.SOUTH);
panStatusArea.add(panGo, BorderLayout.NORTH);
panGo.add(cmdPlay);
panGo.add(cmdReset);
panGo.add(lblStatus);
panStatusArea.add(panCounters, BorderLayout.SOUTH);
panCounters.add(panWins);
panWins.add(lblWins);
panWins.add(txtWins);
panCounters.add(panLoses);
panLoses.add(lblLoses);
panLoses.add(txtLoses);
panCounters.add(panDraws);
panDraws.add(lblDraws);
panDraws.add(txtDraws);
panRoot.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
setBackground(clrBackground);
panRoot.setBackground(clrBackground);
panPlayerArea.setBackground(clrBackground);
panPlayerWeapon.setBackground(clrBackground);
panCPUArea.setBackground(clrBackground);
panCPUWeapon.setBackground(clrBackground);
panStatusArea.setBackground(clrBackground);
panGo.setBackground(clrBackground);
panCounters.setBackground(clrBackground);
panWins.setBackground(clrBackground);
panLoses.setBackground(clrBackground);
panDraws.setBackground(clrBackground);
lblPlayerWeapon.setForeground(clrForeground);
lblCPUWeapon.setForeground(clrForeground);
lblWins.setForeground(clrForeground);
lblLoses.setForeground(clrForeground);
lblDraws.setForeground(clrForeground);
txtWins.setForeground(clrForeground);
txtLoses.setForeground(clrForeground);
txtDraws.setForeground(clrForeground);
txtCPUWeapon.setForeground(clrForeground);
}
public void reset ()
{
cboxWeapon.setSelectedIndex(0);
lblStatus.setText("");
engine.reset();
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource() == cmdReset)
{
reset();
}
else
{
lblStatus.setText(engine.play(cboxWeapon.getSelectedIndex()));
}
txtCPUWeapon.setText(engine.getStrCPUWeapon());
txtWins.setText(Integer.toString(engine.getWins()));
txtLoses.setText(Integer.toString(engine.getLoses()));
txtDraws.setText(Integer.toString(engine.getDraws()));
if (!errorWithImages)
{
lblCPUWeaponIcon.setIcon(imgWeapon[engine.getCPUWeapon()]);
}
}
public void itemStateChanged (ItemEvent e)
{
if (!errorWithImages)
{
lblPlayerWeaponIcon.setIcon(imgWeapon[cboxWeapon.getSelectedIndex()]);
}
}
}