All pastes #384421 Raw Edit

BlackJackPanel.java

public text v1 · immutable
·published 2007-03-07 01:47 UTC
rendered paste body
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

class BlackJackPanel extends JPanel implements ActionListener
{

	private JPanel		playerPanel,
						dealerPanel,
						buttonPanel;

	private JButton	hitButton,
						standButton,
						splitButton,
						doubleOrNothingButton,
						dealButton;
						
	private JTextField	dealerBox,
						playerBox,
						playerHandValue;
						
	private JLabel		handValueLabel;

	public BlackJackPanel()
	{
		this.setLayout(new BorderLayout());
		
		playerPanel = new JPanel();
		playerPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		dealerPanel = new JPanel();
		dealerPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		/* Set up the dealer panel */
		
		/* Add the dealer panel */
		this.add(dealerPanel, BorderLayout.NORTH);
		
		/* Set up the player panel */
		playerBox = new JTextField(50);
		playerBox.setEnabled(false);
		playerPanel.add(playerBox);
		handValueLabel = new JLabel("Hand Value: ");
		playerPanel.add(handValueLabel);
		playerHandValue = new JTextField(20);
		playerPanel.add(playerHandValue);
		
		/* Add the player panel */
		this.add(playerPanel, BorderLayout.CENTER);
		
		/* Set up the button panel */
		hitButton = new JButton("Hit");
		standButton = new JButton("Stand");
		splitButton = new JButton("Split");
		doubleOrNothingButton = new JButton("Double or Nothing!");
		dealButton = new JButton("Deal");
		
		hitButton.setVisible(false);
		standButton.setVisible(false);
		splitButton.setVisible(false);
		doubleOrNothingButton.setVisible(false);
		
		hitButton.addActionListener(this);
		dealButton.addActionListener(this);
		
		buttonPanel.add(doubleOrNothingButton);
		buttonPanel.add(splitButton);
		buttonPanel.add(standButton);
		buttonPanel.add(hitButton);
		buttonPanel.add(dealButton);
		
		/* Add the button panel */
		this.add(buttonPanel, BorderLayout.SOUTH);
		
	}
	
	public void actionPerformed(ActionEvent e)
	{
		System.out.println("ButtonListener->actionPerformed()");
		
		/* DEAL BUTTON */
		if(e.getSource() == dealButton)
		{
			System.out.println("\tDeal pressed!");
			startGame();
		}
		
		/* HIT BUTTON */
		if(e.getSource() == hitButton) {
			System.out.println("\tHit pressed!");
			TestApp.playerHit();
			setPlayerPanel();
			splitButton.setVisible(false);
		}	
	}


	public void setPlayerPanel()
	{
		String s = "";
		for(int i=0;i<TestApp.playerHand.size();i++)
		{
			s += TestApp.playerHand.get(i).getCardString();
		}
		playerBox.setText(s);
		int handValue = BlackJack.handValue(TestApp.playerHand);
		
		if(handValue>21)
		{
			playerHandValue.setText("BUST");
			resetGame();
		}
		else 
			playerHandValue.setText(Integer.toString(handValue));
		
		
	}
	
	public void startGame()
	{
		TestApp.dealHand();
		setPlayerPanel();
		
		dealButton.setVisible(false);
		hitButton.setVisible(true);
		if(BlackJack.isSplittable(TestApp.playerHand)) { splitButton.setVisible(true); }
	}
	
	public void resetGame()
	{
		dealButton.setVisible(true);
		hitButton.setVisible(false);
		TestApp.unDealHand();
	}

}