Package blackjack

Source Code of blackjack.BlackjackView

/*
* BlackjackView.java
*/

package blackjack;

import blackjack.resources.Profile;
import java.awt.Dimension;
import java.awt.GridLayout;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Statement;
import java.sql.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
* The application's main frame.
*/
public class BlackjackView extends FrameView {

    public BlackjackView(SingleFrameApplication app) {
        super(app);

        initComponents();
        Profile.read();
        Profile.save();
        pnlFunctions.setVisible(false);
       
       

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = BlackjackApp.getApplication().getMainFrame();
            aboutBox = new BlackjackAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        BlackjackApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        pnlDealer = new javax.swing.JPanel();
        pnlPlayer = new javax.swing.JPanel();
        lblUsername = new javax.swing.JLabel();
        lblMoney = new javax.swing.JLabel();
        pnlFunctions = new javax.swing.JPanel();
        btnStay = new javax.swing.JButton();
        btnHit = new javax.swing.JButton();
        btnDoubleDown = new javax.swing.JButton();
        btnSplit = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        lblHand = new javax.swing.JLabel();
        lblHandTotal = new javax.swing.JLabel();
        btnDeal = new javax.swing.JButton();
        lblNextBet = new javax.swing.JLabel();
        btnRaiseBet = new javax.swing.JButton();
        btnLowerBet = new javax.swing.JButton();
        pnlStartGame = new javax.swing.JPanel();
        btnStart = new javax.swing.JButton();
        btnLogin = new javax.swing.JButton();
        btnScoreboard = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        lblDealerHandTotal = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        pnlDealer.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlDealer.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        pnlDealer.setMaximumSize(new java.awt.Dimension(709, 242));
        pnlDealer.setMinimumSize(new java.awt.Dimension(709, 242));
        pnlDealer.setName("pnlDealer"); // NOI18N
        pnlDealer.setPreferredSize(new java.awt.Dimension(709, 242));

        javax.swing.GroupLayout pnlDealerLayout = new javax.swing.GroupLayout(pnlDealer);
        pnlDealer.setLayout(pnlDealerLayout);
        pnlDealerLayout.setHorizontalGroup(
            pnlDealerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 786, Short.MAX_VALUE)
        );
        pnlDealerLayout.setVerticalGroup(
            pnlDealerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 251, Short.MAX_VALUE)
        );

        pnlPlayer.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlPlayer.setMaximumSize(new java.awt.Dimension(709, 242));
        pnlPlayer.setMinimumSize(new java.awt.Dimension(709, 242));
        pnlPlayer.setName("pnlPlayer"); // NOI18N
        pnlPlayer.setPreferredSize(new java.awt.Dimension(709, 242));

        javax.swing.GroupLayout pnlPlayerLayout = new javax.swing.GroupLayout(pnlPlayer);
        pnlPlayer.setLayout(pnlPlayerLayout);
        pnlPlayerLayout.setHorizontalGroup(
            pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 786, Short.MAX_VALUE)
        );
        pnlPlayerLayout.setVerticalGroup(
            pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 250, Short.MAX_VALUE)
        );

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(blackjack.BlackjackApp.class).getContext().getResourceMap(BlackjackView.class);
        lblUsername.setText(resourceMap.getString("lblUsername.text")); // NOI18N
        lblUsername.setName("lblUsername"); // NOI18N

        lblMoney.setText(resourceMap.getString("lblMoney.text")); // NOI18N
        lblMoney.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        lblMoney.setName("lblMoney"); // NOI18N

        pnlFunctions.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlFunctions.setName("pnlFunctions"); // NOI18N

        btnStay.setText(resourceMap.getString("btnStay.text")); // NOI18N
        btnStay.setName("btnStay"); // NOI18N
        btnStay.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnStayMouseClicked(evt);
            }
        });

        btnHit.setText(resourceMap.getString("btnHit.text")); // NOI18N
        btnHit.setName("btnHit"); // NOI18N
        btnHit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnHitMouseClicked(evt);
            }
        });

        btnDoubleDown.setText(resourceMap.getString("btnDoubleDown.text")); // NOI18N
        btnDoubleDown.setName("btnDoubleDown"); // NOI18N
        btnDoubleDown.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDoubleDownMouseClicked(evt);
            }
        });

        btnSplit.setText(resourceMap.getString("btnSplit.text")); // NOI18N
        btnSplit.setName("btnSplit"); // NOI18N
        btnSplit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnSplitMouseClicked(evt);
            }
        });

        btnClose.setText(resourceMap.getString("btnClose.text")); // NOI18N
        btnClose.setName("btnClose"); // NOI18N
        btnClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCloseMouseClicked(evt);
            }
        });

        lblHand.setText(resourceMap.getString("lblHand.text")); // NOI18N
        lblHand.setName("lblHand"); // NOI18N

        lblHandTotal.setText(resourceMap.getString("lblHandTotal.text")); // NOI18N
        lblHandTotal.setName("lblHandTotal"); // NOI18N

        btnDeal.setText(resourceMap.getString("btnDeal.text")); // NOI18N
        btnDeal.setName("btnDeal"); // NOI18N
        btnDeal.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDealMouseClicked(evt);
            }
        });

        lblNextBet.setFont(resourceMap.getFont("lblNextBet.font")); // NOI18N
        lblNextBet.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblNextBet.setText(resourceMap.getString("lblNextBet.text")); // NOI18N
        lblNextBet.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        lblNextBet.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblNextBet.setName("lblNextBet"); // NOI18N

        btnRaiseBet.setText(resourceMap.getString("btnRaiseBet.text")); // NOI18N
        btnRaiseBet.setName("btnRaiseBet"); // NOI18N
        btnRaiseBet.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnRaiseBetMouseClicked(evt);
            }
        });

        btnLowerBet.setText(resourceMap.getString("btnLowerBet.text")); // NOI18N
        btnLowerBet.setName("btnLowerBet"); // NOI18N
        btnLowerBet.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnLowerBetMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout pnlFunctionsLayout = new javax.swing.GroupLayout(pnlFunctions);
        pnlFunctions.setLayout(pnlFunctionsLayout);
        pnlFunctionsLayout.setHorizontalGroup(
            pnlFunctionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlFunctionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlFunctionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlFunctionsLayout.createSequentialGroup()
                        .addGroup(pnlFunctionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlFunctionsLayout.createSequentialGroup()
                                .addGroup(pnlFunctionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblHand)
                                    .addComponent(btnLowerBet))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pnlFunctionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlFunctionsLayout.createSequentialGroup()
                                        .addComponent(lblNextBet, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnRaiseBet))
                                    .addComponent(lblHandTotal)))
                            .addComponent(btnClose, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                            .addComponent(btnDeal, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlFunctionsLayout.createSequentialGroup()
                        .addComponent(btnStay, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlFunctionsLayout.createSequentialGroup()
                        .addComponent(btnHit, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlFunctionsLayout.createSequentialGroup()
                        .addGroup(pnlFunctionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnSplit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                            .addComponent(btnDoubleDown, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        pnlFunctionsLayout.setVerticalGroup(
            pnlFunctionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlFunctionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlFunctionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblHand)
                    .addComponent(lblHandTotal))
                .addGap(5, 5, 5)
                .addGroup(pnlFunctionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLowerBet)
                    .addComponent(lblNextBet)
                    .addComponent(btnRaiseBet))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnStay)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnHit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDoubleDown)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSplit)
                .addGap(18, 18, 18)
                .addComponent(btnDeal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnClose)
                .addContainerGap())
        );

        pnlStartGame.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlStartGame.setName("pnlStartGame"); // NOI18N

        btnStart.setText(resourceMap.getString("btnStart.text")); // NOI18N
        btnStart.setName("btnStart"); // NOI18N
        btnStart.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnStartMouseClicked(evt);
            }
        });

        btnLogin.setText(resourceMap.getString("btnLogin.text")); // NOI18N
        btnLogin.setName("btnLogin"); // NOI18N
        btnLogin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnLoginMouseClicked(evt);
            }
        });

        btnScoreboard.setText(resourceMap.getString("btnScoreboard.text")); // NOI18N
        btnScoreboard.setName("btnScoreboard"); // NOI18N

        btnExit.setText(resourceMap.getString("btnExit.text")); // NOI18N
        btnExit.setName("btnExit"); // NOI18N

        javax.swing.GroupLayout pnlStartGameLayout = new javax.swing.GroupLayout(pnlStartGame);
        pnlStartGame.setLayout(pnlStartGameLayout);
        pnlStartGameLayout.setHorizontalGroup(
            pnlStartGameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlStartGameLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlStartGameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addComponent(btnStart, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addComponent(btnExit, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addComponent(btnScoreboard, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlStartGameLayout.setVerticalGroup(
            pnlStartGameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlStartGameLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnStart)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnScoreboard)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addComponent(btnExit)
                .addContainerGap())
        );

        lblDealerHandTotal.setText(resourceMap.getString("lblDealerHandTotal.text")); // NOI18N
        lblDealerHandTotal.setName("lblDealerHandTotal"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDealerHandTotal)
                            .addComponent(lblMoney)
                            .addComponent(lblUsername)))
                    .addComponent(pnlStartGame, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlFunctions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnlDealer, javax.swing.GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE)
                    .addComponent(pnlPlayer, javax.swing.GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(lblUsername)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblMoney, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblDealerHandTotal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlStartGame, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlDealer, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnlFunctions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlPlayer, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE))
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(blackjack.BlackjackApp.class).getContext().getActionMap(BlackjackView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 984, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 814, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void btnStayMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnStayMouseClicked
        if(currentHand>=totalHands-1)
            dealerTurn();
        else
        {
            currentHand++;
            playerTurn();
        }
        updateTable();
    }//GEN-LAST:event_btnStayMouseClicked

    private void btnHitMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnHitMouseClicked
        playerHand.get(currentHand).addCard(pickCard());

        //bust for last hand
        if(playerHand.get(currentHand).addUp() > 21 && currentHand >= totalHands-1)
        {
            updateTable();
            roundOver();
        }

        //bust for hands remaining
        if(playerHand.get(currentHand).addUp() > 21 && currentHand < totalHands-1)
        {
            currentHand++;
            playerTurn();
        }

        updateTable();
    }//GEN-LAST:event_btnHitMouseClicked

    private void btnDoubleDownMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDoubleDownMouseClicked
       
        if(Profile.getMoney()-getTotalBets() < bet[currentHand]) // not enough money
            return;

        playerHand.get(currentHand).addCard(pickCard());
        bet[currentHand] = bet[currentHand]*2;

        updateTable();
        //last hand
        if(currentHand >= totalHands-1)
            dealerTurn();

        //bust for hands remaining
        if(playerHand.get(currentHand).addUp() > 21 && currentHand < totalHands-1)
        {
            currentHand++;
            playerTurn();
        }

       
    }//GEN-LAST:event_btnDoubleDownMouseClicked

    private void btnSplitMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSplitMouseClicked
       
        btnSplit.setVisible(false);
        //check if player has enough money
        if(Profile.getMoney()-getTotalBets() < bet[currentHand]) // not enough money
            return;
       
        totalHands++;
        playerHand.add(new Hand());
        Hand oldHand = playerHand.get(currentHand);
        Hand newHand = playerHand.get(playerHand.size()-1);

        // put second card in new hand
        newHand.addCard(oldHand.getCard(1));

        // remove second card from original hand
        oldHand.remove(1);

        // draw new cards for the
        oldHand.addCard(pickCard());
        newHand.addCard(pickCard());
        updateTable();

    }//GEN-LAST:event_btnSplitMouseClicked

    private void btnCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCloseMouseClicked
        System.exit(0);
    }//GEN-LAST:event_btnCloseMouseClicked

    private void btnStartMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnStartMouseClicked
        pnlStartGame.setVisible(false);
        pnlFunctions.setVisible(true);
        deal();
    }//GEN-LAST:event_btnStartMouseClicked

    private void btnLoginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLoginMouseClicked
        LoginDialog ld = new LoginDialog(new javax.swing.JFrame(), true);
        ld.setVisible(true);
    }//GEN-LAST:event_btnLoginMouseClicked

    private void btnDealMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDealMouseClicked
        btnDeal.setVisible(false);
        deal();

    }//GEN-LAST:event_btnDealMouseClicked

    private void btnRaiseBetMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRaiseBetMouseClicked
        nextBet += 5;
        if(nextBet > Profile.getMoney())
            nextBet = Profile.getMoney();
        lblNextBet.setText("" + nextBet);
    }//GEN-LAST:event_btnRaiseBetMouseClicked

    private void btnLowerBetMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLowerBetMouseClicked
        nextBet -= 5;
        if(nextBet <= 5)
            nextBet = 5;
        lblNextBet.setText("" + nextBet);
    }//GEN-LAST:event_btnLowerBetMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnDeal;
    private javax.swing.JButton btnDoubleDown;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnHit;
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnLowerBet;
    private javax.swing.JButton btnRaiseBet;
    private javax.swing.JButton btnScoreboard;
    private javax.swing.JButton btnSplit;
    private javax.swing.JButton btnStart;
    private javax.swing.JButton btnStay;
    private javax.swing.JLabel lblDealerHandTotal;
    private javax.swing.JLabel lblHand;
    private javax.swing.JLabel lblHandTotal;
    private javax.swing.JLabel lblMoney;
    private javax.swing.JLabel lblNextBet;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JPanel pnlDealer;
    private javax.swing.JPanel pnlFunctions;
    private javax.swing.JPanel pnlPlayer;
    private javax.swing.JPanel pnlStartGame;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private javax.swing.JPanel pnlDynamicDealer = new javax.swing.JPanel(new GridLayout(1,6))//inserted into pnlDealer
    private javax.swing.JPanel pnlDynamicPlayer= new javax.swing.JPanel(new GridLayout(1,6))//inserted into pnlPlayer
    private ArrayList<javax.swing.JLabel> lblDealer;
    private ArrayList<javax.swing.JLabel> lblPlayer; 
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private int currentHand=0;
    private int totalHands=1;
    private ArrayList<Card> deck;
    private JDialog aboutBox;
    private ArrayList<Card> dealerHand;
    private ArrayList<Hand> playerHand;
    private int[] bet;
    private int nextBet=25;
    //private int money=500;
    private boolean playerTurn = true;

    void deal()
    {
        if(nextBet>Profile.getMoney())
            nextBet=Profile.getMoney();
        playerTurn=true;
        btnStay.setVisible(true);
        btnHit.setVisible(true);
        btnDoubleDown.setVisible(true);
        btnSplit.setVisible(false);
        deck = new ArrayList<Card>();
        dealerHand = new ArrayList<Card>();
        playerHand = new ArrayList<Hand>();
        bet = new int[10];
        bet[0] = nextBet;
        constructDeck();
        shuffle();
        currentHand=0;
        totalHands=1;
        dealerHand.add(pickCard());
        dealerHand.add(pickCard());
        playerHand.add(new Hand());
        playerHand.get(0).addCard(pickCard());
        playerHand.get(0).addCard(pickCard());
        if(getHandValue(dealerHand)==21 || getHandValue(playerHand.get(0).getHand())==21)
        {
            roundOver();
            return;
        }
        updateTable();
        playerTurn();
    }

    int getHandValue(ArrayList<Card> hand)
    {
        int value=0;
        int soft=0;
        for(int i=0;i<hand.size();i++)
        {
            value+=hand.get(i).getValue();
            if(hand.get(i).getValue()==11)
                soft++;
        }
        while(value>21 && soft>0)
        {
            soft--;
            value-=10;
        }
        return value;
    }

    void roundOver()
    {
        playerTurn = false;
        updateTable();
        btnStay.setVisible(false);
        btnHit.setVisible(false);
        btnDoubleDown.setVisible(false);
        btnSplit.setVisible(false);
        btnDeal.setVisible(true);

        //player blackjack
        if(isBlackjack(playerHand.get(0).getHand())  )
        {
            Profile.setMoney((int)Math.ceil(bet[0]*1.5));
            updateTable();
            btnDeal.setVisible(true);
            return;
        }

        //dealer blackjack
        if(isBlackjack(dealerHand) )
        {

            Profile.setMoney(Profile.getMoney()-bet[0]);
            updateTable();
            btnDeal.setVisible(true);
            return;
        }

        // gives bet total for each hand
        for(int i=0;i < playerHand.size();i++)
        {
            if(getHandValue(playerHand.get(i).getHand()) > getHandValue(dealerHand) && getHandValue(playerHand.get(i).getHand())<22 || getHandValue(dealerHand) > 21) //player win
                Profile.setMoney(Profile.getMoney()+bet[0]);
            else //dealer win or tie
                Profile.setMoney(Profile.getMoney()-bet[0]);
           
            //tie raises money to undo loss.
            if(getHandValue(playerHand.get(i).getHand()) == getHandValue(dealerHand))
                Profile.setMoney(Profile.getMoney()+bet[0]);
        }
        updateTable();
    }

    boolean isBlackjack(ArrayList<Card> hand)
    {
        if( hand.size()==2 && getHandValue(hand)==21 )
            return true;
        return false;
    }

    void updateTable()
    {
        int cardWidth = 100;
        int margin = 20;
        int leftMargin = 10;
        lblNextBet.setText("" + nextBet);
        lblMoney.setText("Money: " + Profile.getMoney());
        lblHand.setText("Hand " + (currentHand + 1) + ": ");
        lblUsername.setText(Profile.getUsername());

        //show dealer hand total only during the dealer's turn.
        if(playerTurn)
            lblDealerHandTotal.setText("");
        else
            lblDealerHandTotal.setText("" + getHandValue(dealerHand));

        pnlDynamicDealer.removeAll();
        pnlDynamicPlayer.removeAll();
        lblDealer = new ArrayList<javax.swing.JLabel>();
        lblPlayer = new ArrayList<javax.swing.JLabel>();
        pnlDynamicDealer.setSize(new Dimension(dealerHand.size()* (cardWidth+ margin), 240));
        pnlDynamicDealer.setLocation(leftMargin, 1);
        int playerTableWidth = playerHand.get(currentHand).getHand().size() * (cardWidth + margin);
        pnlDynamicPlayer.setSize(new Dimension(playerTableWidth, 240));
        pnlDynamicPlayer.setLocation(leftMargin, 1);

        //show player cards
        for(int i=0;i<playerHand.get(currentHand).size();i++)
        {
            lblPlayer.add(new javax.swing.JLabel());
            pnlDynamicPlayer.add(lblPlayer.get(i));
            lblPlayer.get(i).setIcon(playerHand.get(currentHand).getCard(i).getImage());
           
        }

        //show dealer cards
        for(int i=0;i<dealerHand.size();i++)
        {
            lblDealer.add(new javax.swing.JLabel());
            pnlDynamicDealer.add(lblDealer.get(i));
            if(i==1 && playerTurn)
                lblDealer.get(i).setIcon(dealerHand.get(i).getBackOfCard());
            else
                lblDealer.get(i).setIcon(dealerHand.get(i).getImage());
           
        }

        //show player hand total
        if(playerTurn)
            lblHandTotal.setText("" + playerHand.get(currentHand).addUp());

        pnlDealer.add(pnlDynamicDealer);
        pnlPlayer.add(pnlDynamicPlayer);
        pnlDynamicDealer.setVisible(true);
        pnlDynamicPlayer.setVisible(true);

    }

    Card pickCard()
    {
        Random rnd = new Random();
        int n = rnd.nextInt(deck.size());
        Card c = deck.get(n);
        deck.remove(n);
        return c;
    }

    int getTotalBets ()
    {
        int totalBets=0;
        for(int i=0;i<totalHands;i++)
            totalBets+=bet[i];
        return totalBets;
    }

    void playerTurn()
    {
        if(currentHand==0)
            bet[0]=nextBet;
       
        btnStay.setVisible(true);
        btnHit.setVisible(true);
        btnDoubleDown.setVisible(true);
       
        //can split
        btnSplit.setVisible(false);
        if(playerTurn
                && playerHand.get(currentHand).getHand().size()==2
                && playerHand.get(currentHand).getHand().get(0).getType() == playerHand.get(currentHand).getHand().get(1).getType())
            btnSplit.setVisible(true);
    }

    void dealerTurn()
    {
        playerTurn = false;
        btnStay.setVisible(false);
        btnHit.setVisible(false);
        btnSplit.setVisible(false);
        btnDoubleDown.setVisible(false);
        while(getHandValue(dealerHand)<17)
        {
            dealerHand.add(pickCard());
            delay(1000);
            updateTable();
        }
        roundOver();
    }

    void delay(long wait)
    {
        /*
        long start = System.currentTimeMillis();
        while(System.currentTimeMillis()-start < wait)
        {
            //do nothing
        }*/

    }

    void shuffle()
    {
        ArrayList<Card> newDeck = new ArrayList<Card>();
        Random rnd = new Random();
        for(int i=0;i<52;i++)
        {
            int n = (int)(rnd.nextFloat() * (float)deck.size());
            newDeck.add((Card)deck.get(n));
            deck.remove(n);
        }
        deck = newDeck;
    }

    void constructDeck()
    {
        deck = new ArrayList<Card>();
        for(int suit=0;suit<4;suit++)
            for(int value=1;value<14;value++)
                deck.add(new Card(suit,value));
    }
}//class
TOP

Related Classes of blackjack.BlackjackView

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.