Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplicationca2;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import javax.swing.border.Border;
- import java.applet.*;
- import java.io.*;
- public class GameFrame extends JFrame implements ActionListener, ItemListener {
- //menu
- private JMenu cheatMenu;
- private JMenuBar menuBar;
- private JMenu musicMenu;
- private JMenuItem Music1;
- private JMenuItem Music2;
- private JMenuItem Music3;
- private JMenuItem MusicOff;
- private JMenuItem cheat1;
- private JMenu about;
- private JMenuItem about1;
- //
- private JPanel mainpanel = new JPanel(new BorderLayout());
- private CardGame cardgame = new CardGame();
- private CardGame computerCards = new CardGame();
- //top panel
- private JPanel mnorthPnl, northPnl1, centerPnl1, southPnl1, judgementPnl, buttons, playerName;
- private JButton leftBtn;
- private ImageIcon questionmark;
- private JButton rightBtn;
- private ImageIcon backimg;
- private Card[] computercard = new Card[5];
- private JButton card;
- private JLabel gametitle, player, computer;
- //center panel
- private JPanel mcenterPnl;
- private JButton[] chooseCard;
- private Card[] playercards = new Card[5];
- //bottom panel
- private JPanel msouthPnl, centerPnl3, southPnl3, controlPnlBtm;
- private JTextArea txt;
- private String[] Btnname = {"Restart", "History", "Exit"};
- private JButton[] controlBtnArr = new JButton[Btnname.length];
- private int win = 0, lose = 0, draw = 0;
- private JTextArea textArea;
- private String username;
- // ADDITIONAL FEATURES
- private String[] bkgdFilenameStr = {
- "",
- "explosion.jpg",
- "pebbles.jpg",
- "nature.jpg"};
- private ImageIcon[] bkImgArr = new ImageIcon[bkgdFilenameStr.length];
- private TileBackgroundPanel backgroundPanel; // for the background
- private String[] bkChoiceStr = {"Default", "Explosion", "Pebbles", "Nature"};
- private JComboBox choiceList = new JComboBox(bkChoiceStr);
- //end of addtional features
- //music stuff
- AudioClip bkmusic;
- AudioClip beethoven;
- //
- public GameFrame(String title) {
- super(title);
- loadBkImg(); // load background images
- // Make use of the TileBackgroundPanel as the main panel
- backgroundPanel = new TileBackgroundPanel(bkImgArr[0]);
- backgroundPanel.setTileImage(false);
- setContentPane(backgroundPanel);
- // set it as the main panel of the frame
- initTestPanel();
- getContentPane().setLayout(new BorderLayout());
- setSize(750, 700);
- //Start of mnorthPnl//
- username = cardgame.setUserName();
- mnorthPnl = new JPanel(new BorderLayout());
- mnorthPnl.setOpaque(false);
- centerPnl1 = new JPanel(new BorderLayout());
- centerPnl1.setOpaque(false);
- southPnl1 = new JPanel(new BorderLayout());
- southPnl1.setOpaque(false);
- judgementPnl = new JPanel(new BorderLayout());
- judgementPnl.setOpaque(false);
- buttons = new JPanel(new GridLayout(1, 0));
- buttons.setOpaque(false);
- playerName = new JPanel(new GridLayout(1, 0));
- playerName.setOpaque(false);
- menuBar = new JMenuBar();
- musicMenu = new JMenu("Music");
- Music1 = new JMenuItem("Music 1");
- Music1.addActionListener(this);
- Music2 = new JMenuItem("Music 2");
- Music2.addActionListener(this);
- Music3 = new JMenuItem("Music 3");
- MusicOff = new JMenuItem("OFf");
- cheatMenu = new JMenu("Cheats");
- cheat1 = new JMenuItem("Cheat");
- about = new JMenu("About");
- about1 = new JMenuItem("Credits");
- about1.addActionListener(this);
- musicMenu.add(Music1);
- musicMenu.add(Music2);
- musicMenu.add(Music3);
- musicMenu.add(MusicOff);
- cheatMenu.add(cheat1);
- about.add(about1);
- menuBar.add(about);
- menuBar.add(musicMenu);
- menuBar.add(cheatMenu);
- northPnl1.add(menuBar);
- //centerPnl1//
- gametitle = new JLabel(new ImageIcon("banner.png"), JLabel.CENTER);
- //left side button
- player = new JLabel(username + ":", JLabel.CENTER);
- player.setFont(new Font("calbri", Font.BOLD, 20));
- player.setForeground(Color.red);
- leftBtn = new JButton(questionmark = new ImageIcon("question.png"));
- leftBtn.setPreferredSize(new Dimension(0, 170));
- leftBtn.setContentAreaFilled(false);
- leftBtn.setOpaque(false);
- playerName.add(player);
- //end of left side button
- //right side button
- computer = new JLabel("Computer: ", JLabel.CENTER);
- computer.setFont(new Font("calbri", Font.BOLD, 20));
- computer.setForeground(Color.red);
- rightBtn = new JButton(backimg = new ImageIcon("backimg.jpg"));
- rightBtn.setPreferredSize(new Dimension(0, 100));
- rightBtn.setContentAreaFilled(false);
- rightBtn.setOpaque(false);
- playerName.add(computer);
- for (int i = 0; i < computercard.length; i++) {
- computercard[i] = computerCards.callcard();
- computerCards.dealcard();
- }
- //end of right side button
- buttons.add(leftBtn);
- buttons.add(rightBtn);
- judgementPnl.add(playerName, BorderLayout.NORTH);
- judgementPnl.add(buttons, BorderLayout.SOUTH);
- centerPnl1.add(gametitle);
- southPnl1.add(judgementPnl);
- mnorthPnl.add(centerPnl1, BorderLayout.CENTER);
- mnorthPnl.add(northPnl1, BorderLayout.NORTH);
- mnorthPnl.add(southPnl1, BorderLayout.SOUTH);
- //end of mnorthPnl //
- //Start of mcenterPnl//
- mcenterPnl = new JPanel(new GridLayout(1, 5));
- mcenterPnl.setOpaque(false);
- chooseCard = new JButton[5];
- for (int i = 0; i < chooseCard.length; i++) {
- playercards[i] = cardgame.callcard();
- //imageicon for player cards
- chooseCard[i] = new JButton(playercards[i].getCardImage());
- chooseCard[i].setOpaque(false);
- chooseCard[i].setContentAreaFilled(false);
- mcenterPnl.add(chooseCard[i]);
- chooseCard[i].addActionListener(this);
- cardgame.dealcard();
- }
- mcenterPnl.setBorder(BorderFactory.createTitledBorder(null, "Pick Your Card", 0, 0, null, Color.RED));
- //end of Center panel//
- //start of bottom panel//
- txt = new JTextArea();
- txt.setPreferredSize(new Dimension(0, 100));
- txt.setFont(new Font("calbri", Font.BOLD, 17));
- txt.setText("Number of times Player won: " + win + "\n"
- + "Number of times Computer won: " + lose + "\n"
- + "Number of times Drawn: " + draw);
- msouthPnl = new JPanel(new BorderLayout());
- msouthPnl.setOpaque(false);
- centerPnl3 = new JPanel(new BorderLayout());
- centerPnl3.setOpaque(false);
- southPnl3 = new JPanel(new GridLayout(0, 3));
- southPnl3.setOpaque(false);
- centerPnl3.add(txt);
- for (int i = 0; i < Btnname.length; i++) {
- controlBtnArr[i] = new JButton(Btnname[i]);
- controlBtnArr[i].setBackground(Color.BLACK);
- controlBtnArr[i].setForeground(Color.white);
- southPnl3.add(controlBtnArr[i]);
- controlBtnArr[i].addActionListener(this);
- }
- msouthPnl.add(centerPnl3, BorderLayout.CENTER);
- msouthPnl.add(southPnl3, BorderLayout.SOUTH);
- centerPnl3.setBorder(BorderFactory.createTitledBorder(null, "Score Sheet", 0, 0, null, Color.RED));
- //end of bottom panel//
- mainpanel.add(mnorthPnl, BorderLayout.NORTH);
- mainpanel.add(mcenterPnl, BorderLayout.CENTER);
- mainpanel.add(msouthPnl, BorderLayout.SOUTH);
- mainpanel.setOpaque(false);
- add(mainpanel, BorderLayout.CENTER);
- //combobox and change bacckground stuff
- }
- public void actionPerformed(ActionEvent e) {
- for (int i = 0; i < 5; i++) {
- if (e.getSource() == chooseCard[i]) {
- leftBtn.setIcon(playercards[i].getCardImage());
- chooseCard[i].setIcon(playercards[i].getCardDisabledImg());
- chooseCard[i].setEnabled(false);
- rightBtn.setIcon(computercard[i].getCardImage());
- if (computercard[i].isHigher(playercards[i])) {
- JOptionPane.showMessageDialog(mcenterPnl, cardgame.getUser() + " has failed Fucking miserably!");
- cardgame.setResult(cardgame.getResultText(playercards[i].number, playercards[i].suit)
- + "\t" + cardgame.getResultText(computercard[i].number, computercard[i].suit) + "\t" + cardgame.getUser() + " Has Lost\n");
- lose++;
- } else if (playercards[i].isHigher(computercard[i])) {
- JOptionPane.showMessageDialog(mcenterPnl, cardgame.getUser() + " Is Fucking Victorious!");
- cardgame.setResult(cardgame.getResultText(playercards[i].number, playercards[i].suit)
- + "\t" + cardgame.getResultText(computercard[i].number, computercard[i].suit) + "\t" + cardgame.getUser() + " Has Won\n");
- win++;
- } else {
- JOptionPane.showMessageDialog(mcenterPnl, cardgame.getUser() + " and Computer have drawn");
- cardgame.setResult(cardgame.getResultText(playercards[i].number, playercards[i].suit)
- + "\t" + cardgame.getResultText(computercard[i].number, computercard[i].suit) + "\t" + cardgame.getUser() + " and computer draw\n");
- draw++;
- }
- leftBtn.setIcon(questionmark);
- rightBtn.setIcon(backimg);
- txt.setText("Number of times Player won: " + win + "\n"
- + "Number of times Computer won: " + lose + "\n"
- + "Number of times Drawn: " + draw);
- break;
- }
- }
- if (e.getSource() == controlBtnArr[0]) {
- for (int i = 0; i <= 4; i++) {
- chooseCard[i].setEnabled(true);
- }
- win = 0;
- lose = 0;
- draw = 0;
- txt.setText("Number of times Player won: " + win + "\n"
- + "Number of times Computer won: " + lose + "\n"
- + "Number of times Drawn: " + draw);
- JTextArea textArea = new JTextArea(5, 50);
- textArea.setEditable(false);
- textArea.setText(cardgame.getResult());
- JOptionPane.showMessageDialog(this, textArea, "Results of last game", JOptionPane.INFORMATION_MESSAGE);
- cardgame = new CardGame();
- computerCards = new CardGame();
- cardgame.setUserName(username);
- for (int j = 0; j < computercard.length; j++) {
- computercard[j] = computerCards.callcard();
- computerCards.dealcard();
- playercards[j] = cardgame.callcard();
- cardgame.dealcard();
- chooseCard[j].setIcon(playercards[j].getCardImage());
- }
- }
- if (e.getSource() == controlBtnArr[1]) {
- JTextArea textArea = new JTextArea(5, 50);
- textArea.setEditable(false);
- textArea.setText(cardgame.getResult());
- JOptionPane.showMessageDialog(this, textArea, "History", JOptionPane.INFORMATION_MESSAGE);
- }
- if (e.getSource() == controlBtnArr[2]) {
- JTextArea textArea = new JTextArea(5, 50);
- textArea.setEditable(false);
- textArea.setText(cardgame.getResult());
- JOptionPane.showMessageDialog(this, textArea, "Results of last game", JOptionPane.INFORMATION_MESSAGE);
- JOptionPane.showMessageDialog(this, "Thank You for Playing");
- System.exit(0);
- }
- if (e.getSource() == about1) {
- JOptionPane.showMessageDialog(this, "Created by: \nYap Joon Woon\nLeroy Choo");
- }
- if (e.getSource() == Music1) {
- beethoven.stop();
- try {
- bkmusic = Applet.newAudioClip(new File("bkmusic.mid").toURI().toURL());
- if (bkmusic != null) {
- bkmusic.loop();
- }
- } catch (Exception f) {
- f.printStackTrace();
- }
- }
- if (e.getSource() == Music2) {
- bkmusic.stop();
- try {
- beethoven = Applet.newAudioClip(new File("beethoven.wav").toURI().toURL());
- if (beethoven != null) {
- beethoven.loop();
- }
- } catch (Exception f) {
- f.printStackTrace();
- }
- }
- }
- //northPnl1//
- private void initTestPanel() {
- northPnl1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
- choiceList.setFont(new Font("Verdana", Font.BOLD, 11));
- choiceList.setBackground(Color.yellow);
- choiceList.addItemListener(this);
- northPnl1.add(choiceList);
- northPnl1.setPreferredSize(new Dimension(700, 30));
- }
- private void loadBkImg() {
- for (int i = 0; i < bkgdFilenameStr.length; i++) {
- bkImgArr[i] = new ImageIcon(bkgdFilenameStr[i]);
- }
- }
- public void itemStateChanged(ItemEvent e) {
- if (e.getSource() == choiceList) {
- int selectedIndex = choiceList.getSelectedIndex();
- TileBackgroundPanel temp = (TileBackgroundPanel) getContentPane();
- temp.setTileIcon(bkImgArr[selectedIndex]);
- }
- }
- }
Add Comment
Please, Sign In to add comment