Posted below is currently my main class that works for a java blackjack game, i do have 3 other classes for the full game to work but this class is where my problem is. my problem is in the west border i have so much being written to the GUI that it takes up the center border as well. i want to put somthing in the center and i do not want to have to make the GUI as large as the screen to display everything is there a way to do like a new line break so that "in this case" in the west at the top would be the player and dealers current card values, then below the count of wins for both the player and the dealer then in the center i would be able to put what i want. Thank you in advance to anyone that assists me.
i
mport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game extends JFrame implements ActionListener {
private Deck deck;
public Player player = new Player("player");
public Player dealer = new Player("dealer");
private JButton HIT = new JButton("Hit");
private JButton STAY = new JButton("Stay");
private JButton DEAL = new JButton("Deal");
private JButton SHUFFLE = new JButton("Shuffle");
private JLabel STATUS = new JLabel(" ", JLabel.CENTER);
private JLabel STATUS2 = new JLabel();
JPanel playerPanel = new JPanel();
JPanel dealerPanel = new JPanel();
JPanel buttonsPanel = new JPanel();
JPanel statusPanel = new JPanel();
JPanel PlayerPanel = new JPanel();
JPanel DealerPanel = new JPanel();
JPanel ScorePanel = new JPanel();
public int playercount = 0;
public int dealercount = 0;
int playerscore = 0; // Counting Player Wins
int dealerscore = 0; // Counting Dealer wins
Game()
{
JFrame BLACKJACK = new JFrame("BlackJack");
BLACKJACK.setIconImage(Toolkit.getDefaultToolkit().getImage("cards/10.gif"));
BLACKJACK.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonsPanel.add(HIT);
buttonsPanel.add(STAY);
buttonsPanel.add(DEAL);
buttonsPanel.add(SHUFFLE);
ScorePanel.add(STATUS2);
ScorePanel.add(STATUS);
HIT.addActionListener(this);
STAY.addActionListener(this);
DEAL.addActionListener(this);
SHUFFLE.addActionListener(this);
HIT.setEnabled(false);
STAY.setEnabled(false);
//SHUFFLE.setEnabled(false);
// Giving Panels background color
dealerPanel.setBackground(Color.BLACK);
playerPanel.setBackground(Color.BLACK);
buttonsPanel.setBackground(Color.WHITE);
statusPanel.setBackground(Color.WHITE);
ScorePanel.setBackground(Color.WHITE);
// Setting each Panel into different areas
BLACKJACK.setLayout(new BorderLayout());
BLACKJACK.add(dealerPanel, BorderLayout.NORTH);
BLACKJACK.add(playerPanel, BorderLayout.SOUTH);
BLACKJACK.add(buttonsPanel, BorderLayout.EAST);
//BLACKJACK.add(statusPanel, BorderLayout.CENTER);
BLACKJACK.add(ScorePanel, BorderLayout.WEST);
BLACKJACK.repaint();
BLACKJACK.setSize(850, 450);
BLACKJACK.setVisible(true);
} // End Game
private void hitPlayer()
{
Card newCard = player.dealTo(deck.dealFrom());
playerPanel.add(new JLabel(new ImageIcon("cards/" + newCard.toString())));
playerPanel.updateUI();
} // end hit player
private void hitDealerDown()
{
Card newCard = dealer.dealTo(deck.dealFrom());
dealerPanel.add(new JLabel(new ImageIcon("cards/back.gif")));
dealerPanel.updateUI();
} // end hit dealerdown
private void hitDealer()
{
Card newCard = dealer.dealTo(deck.dealFrom());
dealerPanel.add(new JLabel(new ImageIcon("cards/" + newCard.toString())));
dealerPanel.updateUI();
} // end hit dealer
private void deal()
{
playercount = 0;
dealercount = 0;
playerPanel.removeAll();
dealerPanel.removeAll();
playerPanel.updateUI();
dealerPanel.updateUI();
player.reset();
dealer.reset();
if (deck == null || deck.size() < 15)
{
deck = new Deck();
deck.shuffle();
STATUS.setText("Shuffling");
}//end if
hitPlayer();
hitDealerDown();
hitPlayer();
hitDealer();
}// end deal
private void checkWinner()
{
dealerPanel.removeAll();
for (int i = 0; i < dealer.inHand(); i++) {
dealerPanel.add(new JLabel(new ImageIcon("cards/" + dealer.cards[i].toString())));
}
if (player.value() > 21)
{
dealerscore ++;
STATUS.setText("Player Busts " + "Dealer Wins: " + dealerscore + " " + "Player Wins: " + playerscore );
}
else if (dealer.value() > 21)
{
playerscore++;
STATUS.setText("Dealer Busts! " + "Dealer Wins: " + dealerscore + " " + "Player Wins: " + playerscore);
}
else if (dealer.value() == player.value())
{
dealerscore++;
STATUS.setText("Push " + "Dealer Wins: " + dealerscore + " " + "Player Wins: " + playerscore);
}
else if (dealer.value() < player.value())
{
playerscore++;
STATUS.setText("Player Wins " + "Dealer Wins: " + dealerscore + " " + "Player Wins: " + playerscore);
}
else
{
dealerscore++;
STATUS.setText("Dealer Wins: " + "Dealer Wins " + dealerscore + " " + "Player Wins: " + playerscore);
}
playercount = player.value(); // Cards on screen, Value for player
dealercount = dealer.value(); // Cards on screen, Value for dealer
STATUS2.setText("Dealer Count: " + dealercount + " " + " Player Count: " + playercount);
} // End Check Winner
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == HIT)
{
hitPlayer();
if (player.value() > 21)
{
checkWinner();
HIT.setEnabled(false);
STAY.setEnabled(false);
DEAL.setEnabled(true);
}
}
if (e.getSource() == STAY)
{
while (dealer.value() < 17 || player.value() > dealer.value())
{
hitDealer();
}
checkWinner();
HIT.setEnabled(false);
STAY.setEnabled(false);
DEAL.setEnabled(true);
}// end stay
if (e.getSource() == DEAL)
{
deal();
STATUS.setText(" ");
HIT.setEnabled(true);
STAY.setEnabled(true);
DEAL.setEnabled(false);
STATUS2.setText(" ");
} // end DEAL
if (e.getSource() == SHUFFLE)
{
deck.shuffle();
playerPanel.removeAll();
dealerPanel.removeAll();
//playerPanel.updateUI();
//dealerPanel.updateUI();
//player.reset();
//dealer.reset();
STATUS.setText("\"Shuffling\"");
STATUS2.setText (" Hit Deal");
} // end shuffle
}
public static void main(String[] args)
{
new Game();
}
}// End Game