client.gui.ButtonPanel.java Source code

Java tutorial

Introduction

Here is the source code for client.gui.ButtonPanel.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Andrzej Ressel (jereksel@gmail.com)
 * Copyright (c) 2014 Mateusz Kaleciski
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package client.gui;

import client.GlobalVariables;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonPanel extends JPanel implements ActionListener {

    private final JButton exchangeCards;
    private final JButton check;
    private final JButton bet;
    private final JButton raise;
    private final JButton call;
    private final JButton fold;
    private final JButton allIn;
    private final JButton smallBlind;
    private final JButton bigBlind;
    private final JButton liveBlind;

    private final JButton startGame;

    public ButtonPanel() {

        GlobalVariables.buttonPanel = this;

        exchangeCards = new JButton("Exchange cards");
        exchangeCards.setHorizontalAlignment(SwingConstants.LEFT);
        exchangeCards.addActionListener(this);
        check = new JButton("Check");
        check.setHorizontalAlignment(SwingConstants.LEFT);
        check.addActionListener(this);
        bet = new JButton("Bet");
        bet.setHorizontalAlignment(SwingConstants.LEFT);
        bet.addActionListener(this);
        raise = new JButton("Raise");
        raise.setHorizontalAlignment(SwingConstants.LEFT);
        raise.addActionListener(this);
        call = new JButton("Call");
        call.setHorizontalAlignment(SwingConstants.LEFT);
        call.addActionListener(this);
        fold = new JButton("Fold");
        fold.setHorizontalAlignment(SwingConstants.LEFT);
        fold.addActionListener(this);
        allIn = new JButton("All-In");
        allIn.setHorizontalAlignment(SwingConstants.LEFT);
        allIn.addActionListener(this);
        startGame = new JButton("Start Game");
        startGame.setHorizontalAlignment(SwingConstants.LEFT);
        startGame.addActionListener(this);
        smallBlind = new JButton("Small Blind");
        smallBlind.setHorizontalAlignment(SwingConstants.LEFT);
        smallBlind.addActionListener(this);
        bigBlind = new JButton("Big Blind");
        bigBlind.setHorizontalAlignment(SwingConstants.LEFT);
        bigBlind.addActionListener(this);
        liveBlind = new JButton("Live Blind");
        liveBlind.setHorizontalAlignment(SwingConstants.LEFT);
        liveBlind.addActionListener(this);

        setLayout(new GridBagLayout());
        final GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 0;
        add(exchangeCards, c);

        c.gridy++;
        add(check, c);
        c.gridy++;
        add(bet, c);
        c.gridy++;
        add(raise, c);
        c.gridy++;
        add(call, c);
        c.gridy++;
        add(fold, c);
        c.gridy++;
        add(allIn, c);
        // c.gridy++;
        // add(startGame, c);
        c.gridy++;
        add(smallBlind, c);
        c.gridy++;
        add(bigBlind, c);
        c.gridy++;
        add(liveBlind, c);

        exchangeCards.setVisible(false);
        check.setVisible(false);
        bet.setVisible(false);
        raise.setVisible(false);
        call.setVisible(false);
        fold.setVisible(false);
        allIn.setVisible(false);
        smallBlind.setVisible(false);
        bigBlind.setVisible(false);
        liveBlind.setVisible(false);

    }

    @Override
    public void actionPerformed(final ActionEvent event) {
        if (GlobalVariables.connected) {

            if (event.getSource() == exchangeCards) {
                try {
                    final JSONArray toSend = new JSONArray();

                    toSend.put(new JSONObject().put("action", "exchange_cards"));

                    for (int i = 0; i < GlobalVariables.hand.length; i++) {

                        if (!(GlobalVariables.chosen[i])) {
                            continue;
                        }

                        toSend.put(new JSONObject().put("suit", GlobalVariables.hand[i].getSuit()).put("rank",
                                GlobalVariables.hand[i].getRank()));

                    }

                    GlobalVariables.chosen = new boolean[] { false, false, false, false };

                    GlobalVariables.out.println(toSend);

                    System.out.println("[DEBUG] " + toSend);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } else if (event.getSource() == startGame) {

                try {
                    final JSONArray toSend = new JSONArray();

                    toSend.put(new JSONObject().put("action", "start_game"));

                    GlobalVariables.out.println(toSend);

                    System.out.println("[DEBUG] " + toSend);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } else if (event.getSource() == smallBlind) {

                final JSONArray toSend = new JSONArray();

                try {
                    toSend.put(new JSONObject().put("action", "small_blind"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                GlobalVariables.out.println(toSend);

            } else if (event.getSource() == bigBlind) {

                final JSONArray toSend = new JSONArray();

                try {
                    toSend.put(new JSONObject().put("action", "big_blind"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                GlobalVariables.out.println(toSend);

            } else if (event.getSource() == call) {

                final JSONArray toSend = new JSONArray();
                try {
                    toSend.put(new JSONObject().put("action", "call"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                GlobalVariables.out.println(toSend);

            } else if (event.getSource() == raise) {

                int raise;

                try {
                    raise = Integer.parseInt(JOptionPane.showInputDialog(this, "Type your bet:"));
                } catch (NumberFormatException e) {
                    new MyDialog("This isn't a number").setVisible(true);
                    return;
                }

                final JSONArray toSend = new JSONArray();

                try {
                    toSend.put(new JSONObject().put("action", "raise"));
                    toSend.put(new JSONObject().put("bet", raise));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                GlobalVariables.out.println(toSend);

            } else if (event.getSource() == bet) {

                int bet;

                try {
                    bet = Integer.parseInt(JOptionPane.showInputDialog(this, "Type your bet:"));
                } catch (NumberFormatException e) {
                    new MyDialog("This isn't a number").setVisible(true);
                    return;
                }

                final JSONArray toSend = new JSONArray();

                try {
                    toSend.put(new JSONObject().put("action", "bet"));
                    toSend.put(new JSONObject().put("bet", bet));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                GlobalVariables.out.println(toSend);

            } else if (event.getSource() == check) {

                final JSONArray toSend = new JSONArray();
                try {
                    toSend.put(new JSONObject().put("action", "check"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                GlobalVariables.out.println(toSend);

            } else if (event.getSource() == fold) {

                final JSONArray toSend = new JSONArray();
                try {
                    toSend.put(new JSONObject().put("action", "fold"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                GlobalVariables.out.println(toSend);

            } else if (event.getSource() == allIn) {

                final JSONArray toSend = new JSONArray();

                try {
                    toSend.put(new JSONObject().put("action", "all-in"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                GlobalVariables.out.println(toSend);

            } else if (event.getSource() == liveBlind) {

                int raise;

                try {
                    raise = Integer.parseInt(JOptionPane.showInputDialog(this, "Type your bet:"));
                } catch (NumberFormatException e) {
                    new MyDialog("This isn't a number").setVisible(true);
                    return;
                }

                final JSONArray toSend = new JSONArray();

                try {
                    toSend.put(new JSONObject().put("action", "live_blind"));
                    toSend.put(new JSONObject().put("bet", raise));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                GlobalVariables.out.println(toSend);

            }

            hideAll();

            //   GlobalVariables.panel.repaint();
        }

    }

    public void showButton(final String button) {
        for (final Component c : getComponents()) {
            if (c instanceof JButton && ((JButton) c).getText().equals(button)) {
                c.setVisible(true);
            }
        }
    }

    public void hideAll() {
        for (final Component c : getComponents()) {
            if (c instanceof JButton) {
                c.setVisible(false);
            }
        }
    }

}