BlackjackGame
843798Jun 25 2007 — edited Jun 25 2007Hi,
For one of my college classes I volunteered to change a gui line blackjack program to a gui based on swing. This is my first time actually creating something useful with swing so my code probably sucks but it works for the most part.
heres my problem...
the cards are loaded onto the frame first by the GUIGame which makes a call to my addCard class and either executing one of the following methods, addCardDealer or addCardPlayer. My problem is that I have no idea how to load images and what would be the best way of loading my card images on to the screen.
here is the relivant code that i'm working with...
package blackjackpackage;
/**
*
* @author villajo
*/
public class GUIGame
{
public void playGUI()
{
BlackJackGUI GUI = new BlackJackGUI();
addCard add = new addCard();
//Instantiates our deck of cards object. Constructor builds the arraylist of Card Objects.
Deck gameDeck = new Deck();
Hand playerHand = new Hand();
Hand dealerHand = new Hand();
boolean continueGame = true;
gameDeck.shuffle();
//playerHand.add(gameDeck.draw());
GUI.add(add.addCardPlayer(gameDeck.draw()));
GUI.add(add.addCardDealer(gameDeck.draw()));
GUI.add(add.addCardPlayer(gameDeck.draw()));
GUI.add(add.addCardDealer(gameDeck.draw()));
...........................................................................................................................................
This is the BlackjackGUI. This is responsable for the buttons, menus, action handlers and anything that has to do with the BlackjackGUI..(I know that there aren't action handlers yet for the buttons.. thats coming in time..)
/*
* BlackJackGUI.java
*
* Created on June 22, 2007, 10:38 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package blackjackpackage;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author villajo
*/
public class BlackJackGUI extends JFrame
{
/** Creates a new instance of BlackJackGUI */
public BlackJackGUI()
{
/*finds screenheight and width and sets the program to center.
*
*/
int screenHeight;
int screenWidth;
FindScreenSize screen = new FindScreenSize();
screenHeight = screen.returnHeight();
screenWidth = screen.returnWidth();
setLocation(screenWidth / 4, screenHeight / 4);
//create our JFrame
setTitle("BlackJack");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
//this makes it so we CANNOT resize the window
setResizable(false);
//all menu stuff..
BlackJackMenuBar MenuBar = new BlackJackMenuBar();
BlackJackButtons buttons = new BlackJackButtons();
add(buttons);
setVisible(true);
}
//
class BlackJackMenuBar
{
protected BlackJackMenuBar()
{
JMenu File = new JMenu("File");
JMenu About = new JMenu("About");
//File menu attributes..
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
//adding the menus..
menuBar.add(File);
menuBar.add(About);
//adding functions to menubar..
File.addSeparator();
File.add(new AbstractAction("Exit")
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
About.add(
new AbstractAction("About")
{
public void actionPerformed(ActionEvent event)
{
aboutWindow about = new aboutWindow();
}
});
}
}
class addCard extends JPanel
{
}
private static final int DEFAULT_WIDTH = 640;
private static final int DEFAULT_HEIGHT = 480;
public int playerScore = 0;
public int dealerScore = 0;
}
class BlackJackButtons extends JPanel
{
public BlackJackButtons()
{
BlackjackGame game = new BlackjackGame();
JButton Hit = new JButton("Hit");
JButton Stand = new JButton("Stand");
//JButton Smack = new JButton("Smack");
//adding the cute java image..
setLayout(null);
add(Hit);
add(Stand);
//add(Smack);
Insets buttonInsets = getInsets();
Dimension size = Stand.getPreferredSize();
Hit.setBounds(220 + buttonInsets.left, 385 + buttonInsets.bottom, size.width, size.height);
Stand.setBounds(310 + buttonInsets.left, 385 + buttonInsets.bottom, size.width, size.height);
//Jpanel S
//add action Listeners...
//add JLabels
//pastes our label on the panel..
PlayerMessage player = new PlayerMessage();
DealerMessage dealer = new DealerMessage();
add(player);
add(dealer);
Insets playerInsets = getInsets();
Insets dealerInsets = getInsets();
Dimension dealerSize = dealer.getPreferredSize();
Dimension playerSize = player.getPreferredSize();
player.setBounds(15 + playerInsets.left, 80 + playerInsets.bottom, playerSize.width, playerSize.height);
dealer.setBounds(15 + dealerInsets.left, 205 + dealerInsets.bottom, dealerSize.width, dealerSize.height);
}
}
class PlayerMessage extends JPanel
{
public PlayerMessage()
{
JLabel player = new JLabel("Player:");
add(player);
}
}
class DealerMessage extends JPanel
{
public DealerMessage()
{
JLabel dealer = new JLabel("Dealer:");
add(dealer);
}
}
//
class addImage extends JLabel
{
public void addImage(String card)
{
//retrieve the card from the file of cards..
ImageIcon icon = new ImageIcon("/home/villajo/blackjackPackage/src/blackjackpackage/Cards/" + card);
icon.getImage();
//this determines the bounds or the placement of the cards..
}
}
class addCard extends JPanel
{
public JLayeredPane addCardPlayer(Card card)
{
/*
*What i'm doing is finding the deck and tally so that I can put it on screen..
*/
//This finds the card and the score for that card and adds it to player..
GUIHelper findNumber = new GUIHelper();
int localScore = findNumber.WhatScoreAmI(card);
String cardtype = card.toString() + ".jpg";
//Load the appropriate Image..
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(76,110));
ImageIcon img = new ImageIcon(cardtype);
img.getImage();
JLabel label = new JLabel(img);
layeredPane.add(label);
return layeredPane;
}
public JLayeredPane addCardDealer(Card card)
{
GUIHelper findNumber = new GUIHelper();
int localScore = findNumber.WhatScoreAmI(card);
String cardtype = card.toString() + ".jpg";
System.out.println(cardtype);
//load the appropriate image..
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(76,110));
ImageIcon img = new ImageIcon(cardtype);
img.getImage();
JLabel label = new JLabel(img);
layeredPane.add(label);
return layeredPane;
}
}
I know that I dont have the bounds set or any of the properties set up, I'm just wanting it to load up and from there I think I can figure it out. Some other information, the images are of JPG format and are located in the source file where all of the other sources and classes are located for the project.
addCard class handles the adding of cards to new layers of the LayeredPanel. I thought this might be easier if I used a layered pane instead of adding a new JPanel.
card takes in a card from another class called card but is passed through GUIgame to addCard. This is probably bad design practice, but i'm just trying to get it to work. What card does is takes a shuffled deck of cards and takes a card from it.
thanks in advance for any help
Joseph Villa