Applet Urgent Help Plz

Hello Everyone, im having a major problem with this applet.I am trying to create a Black Jack card game to play around.It is compiling with no errors however the applet doesnt seem to get initialised thus it is not possible to be viewed in the applet viewer. What im trying to achieve is being able to draw 2 cards in the deck. Ever since i tried to use the showBlackjackScore() method as well as the init()method things started to get serious.These methods implementations were taken from: http://nifty.stanford.edu/2004/EstellCardGame/code.html were the Card, Deck, Hand, Rank, and Suit classes are also implemented, and are used with this program. Would appreciate any help! Other than that, the Gui is of my own and is a really nice example for ideas in your future java programs.Thanks in Advance!

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.border.*;

import java.util.*;

publicclass BlackJackClientextends JApplet

{

private Deck cardDeck;

private BlackjackHand myHand;

privatefinalint SIZE_OF_HAND = 2;

privatefinal String directory ="cards/";

private JLabel[] handLbl =new JLabel[ SIZE_OF_HAND ];

private Container container;

private JPanel panel1,panel2,Flow,Opponent,Player;

private JPanel cardsTop,cardsBottom;

private JLabel Player_Status;

private JLabel top_card1,top_card2,top_card3,top_card4,top_card5;

private JLabel bottom_card1,bottom_card2,bottom_card3,bottom_card4,bottom_card5;

private TitledBorder opponent,player;

private TitledBorder Cr1,Cr2,Cr3,Cr4,Cr5;

private TitledBorder B1,B2,B3,B4,B5;

private JPanel box1,box2,box3,box4,box5,box6,box7,box8,box9,box10;

private JTextArea Messages;

private JButton Hit,Stand,JButton1;

private JLabel scoreLbl;

public BlackJackClient()//CONSTRUCTOR

{

container=getContentPane();

container.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS));

scoreLbl.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

scoreLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

getContentPane().add(scoreLbl);

scoreLbl.setForeground(java.awt.Color.black);

scoreLbl.setFont(new Font("Dialog", Font.BOLD, 64));

scoreLbl.setBounds(252,48,103,88);

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++PANELS

JPanel p =new JPanel();

p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

Opponent=new JPanel();

Player=new JPanel();

cardsTop=new JPanel(new FlowLayout(FlowLayout.CENTER,10,15));

cardsBottom=new JPanel(new FlowLayout(FlowLayout.CENTER,10,15));

panel1=new JPanel();

panel2=new JPanel();

box1=new JPanel();

box2=new JPanel();

box3=new JPanel();

box4=new JPanel();

box5=new JPanel();

box6=new JPanel();

box7=new JPanel();

box8=new JPanel();

box9=new JPanel();

box10=new JPanel();

Flow=new JPanel(new FlowLayout(FlowLayout.RIGHT,3,0));

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

JLabel label=new JLabel("Welcome To BlackJack 21",SwingConstants.CENTER);

label.setForeground(Color.white);

label.setFont(new Font("Futura",Font.BOLD,19));

panel1.add(label);

panel1.setBackground(Color.black);

Icon card1=new ImageIcon("Trapula/c1.png");

top_card1=new JLabel(card1);

Icon card2=new ImageIcon("Trapula/d1.png");

top_card2=new JLabel(card2);

Icon card3=new ImageIcon("Trapula/h1.png");

top_card3=new JLabel(card3);

Icon card4=new ImageIcon("Trapula/s1.png");

top_card4=new JLabel(card4);

Icon card5=new ImageIcon("Trapula/ck.png");

top_card5=new JLabel(card5);

box1.add(top_card1);

cardsTop.add(box1);

box2.add(top_card2);

cardsTop.add(box2);

box3.add(top_card3);

cardsTop.add(box3);

box4.add(top_card4);

cardsTop.add(box4);

box5.add(top_card5);

cardsTop.add(box5);

Cr1 =new TitledBorder(new LineBorder(Color.black,2),"");

box1.setBorder(Cr1);

box1.setBackground(Color.gray);

Cr2 =new TitledBorder(new LineBorder(Color.black,2),"");

box2.setBorder(Cr2);

box2.setBackground(Color.gray);

Cr3 =new TitledBorder(new LineBorder(Color.black,2),"");

box3.setBorder(Cr3);

box3.setBackground(Color.gray);

Cr4 =new TitledBorder(new LineBorder(Color.black,2),"");

box4.setBorder(Cr4);

box4.setBackground(Color.gray);

Cr5 =new TitledBorder(new LineBorder(Color.black,2),"");

box5.setBorder(Cr5);

box5.setBackground(Color.gray);

opponent=new TitledBorder(new LineBorder(Color.black,5),"Opponent");

Opponent.setBorder(opponent);

Opponent.add(cardsTop);

Opponent.setBackground(Color.green);

cardsTop.setBackground(Color.green);

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Icon card6=new ImageIcon("Trapula/s4.png");

bottom_card1=new JLabel(card6);

Icon card7=new ImageIcon("Trapula/s5.png");

bottom_card2=new JLabel(card7);

Icon card8=new ImageIcon("Trapula/s6.png");

bottom_card3=new JLabel(card8);

Icon card9=new ImageIcon("Trapula/s7.png");

bottom_card4=new JLabel(card9);

Icon card10=new ImageIcon("Trapula/s8.png");

bottom_card5=new JLabel(card10);

box6.add(bottom_card1);

cardsBottom.add(box6);

box7.add(bottom_card2);

cardsBottom.add(box7);

box8.add(bottom_card3);

cardsBottom.add(box8);

box9.add(bottom_card4);

cardsBottom.add(box9);

box10.add(bottom_card5);

cardsBottom.add(box10);

B1=new TitledBorder(new LineBorder(Color.black,2),"");

box6.setBorder(B1);

box6.setBackground(Color.gray);

B2=new TitledBorder(new LineBorder(Color.black,2),"");

box7.setBorder(B2);

box7.setBackground(Color.gray);

B3=new TitledBorder(new LineBorder(Color.black,2),"");

box8.setBorder(B3);

box8.setBackground(Color.gray);

B4=new TitledBorder(new LineBorder(Color.black,2),"");

box9.setBorder(B4);

box9.setBackground(Color.gray);

B5=new TitledBorder(new LineBorder(Color.black,2),"");

box10.setBorder(B5);

box10.setBackground(Color.gray);

player=new TitledBorder(new LineBorder(Color.black,5),"Player");

player.setTitleColor(Color.black);

Player.setBorder(player);

JPanel Hand=new JPanel();

JButton1=new JButton("Draw Hand");

JButton1.setActionCommand("Draw a Hand");

JButton1.addActionListener(new ActionListener()

{publicvoid actionPerformed(ActionEvent e)

{Object object=e.getSource();

if(e.getSource()==object)

{cardDeck.restoreDeck();

cardDeck.shuffle();

myHand.discardHand();

for (int i = 0; i < SIZE_OF_HAND; i++ )

{Card c = cardDeck.dealCard();

myHand.addCard( c );

handLbl[i].setIcon( c.getCardImage() );

handLbl[i].setText( c.toString() );

}

showBlackjackScore();

}

}

});

Hand.add(JButton1);

Hand.setBackground(Color.green);

cardsBottom.setBackground(Color.green);

Player.setBackground(Color.green);

Player.add(cardsBottom);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Messages=new JTextArea("-Messages-\n\n\n",10,42);

JScrollPane scroller=new JScrollPane(Messages);

Messages.setEditable(false);

Messages.setForeground(Color.white);

Messages.setBackground(Color.gray);

panel2.setBackground(Color.green);

panel2.add(scroller);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

JButton Exit=new JButton("Exit");

Exit.setToolTipText("Disconnect");

Hit=new JButton("Hit");

Hit.setToolTipText("Ask another card?");

Stand=new JButton("Stand");

Stand.setToolTipText("Satisfied with your card/s?");

JLabel l1=new JLabel("");

JLabel l2=new JLabel("");

JLabel l3=new JLabel("");

JLabel l4=new JLabel("");

JLabel l5=new JLabel(" ");

Exit.addActionListener(new ActionListener()

{publicvoid actionPerformed(ActionEvent e)

{System.out.println("Disconnected from BlackJack Thank you for Gambling!");

System.exit(0);

}

});

Hit.addActionListener(new ActionListener()

{publicvoid actionPerformed(ActionEvent e)

{Messages.append("Hit Me!!\n");}

});

Stand.addActionListener(new ActionListener()

{publicvoid actionPerformed(ActionEvent e)

{Messages.append("Stand\n");}

});

Flow.add(l1);

Flow.add(l2);

Flow.add(l3);

Flow.add(l4);

Flow.add(l5);

Flow.add(Exit);

Flow.add(Hit);

Flow.add(Stand);

Flow.setBackground(Color.green);

p.add(Flow);

//++++++++++++++++++++++++++++++++++++++++++++++++++++

container.add(panel1);

container.add(Opponent);

container.add(Player);

container.add(Hand);

container.add(panel2);

container.add(p);

}//end of Black Jack client constructor

publicvoid init()

{cardDeck =new Deck();

Iterator suitIterator = Suit.VALUES.iterator();

while ( suitIterator.hasNext() )

{Suit suit = (Suit) suitIterator.next();

Iterator rankIterator = Rank.VALUES.iterator();

while ( rankIterator.hasNext() )

{Rank rank = (Rank) rankIterator.next();

String imageFile = directory + Card.getFilename( suit, rank );

ImageIcon cardImage =new ImageIcon( getImage( getCodeBase(), imageFile ) );

Card card =new Card( suit, rank, cardImage );

cardDeck.addCard( card );

}

}

}//end of init

privatevoid showBlackjackScore()

{

int score = myHand.evaluateHand();

if( score == 21 )

{scoreLbl.setText( score +"!" );

scoreLbl.setForeground( Color.red );

}

else

{scoreLbl.setText("" + score );

scoreLbl.setForeground( Color.black );

}

}//end of showBlackjackScore

}//end of class Black Jack client

[17420 byte] By [Makisana] at [2007-11-27 4:49:32]
# 1
Re-compile it and package it into a JAR. Create an html page and see if you can view it through a web browser.
maple_shafta at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for your reply, unfortunately i cannot view it through the web browser. Really appreciate your feedback!
Makisana at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 3
You can always rewrite it as a stand-alone app.
Hippolytea at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 4

I might be wrong, but maybe the init() method isn't showing any GUI because you're not creating or adding any components there.

Rather than putting all the Swing code in your constructor, put it in a method that returns your top-level component, such as:

private Component createComponents() {

// blah

}

Then, in your init() method, try something like:

public void init() {

try {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createComponents();

}

});

}

catch (Exception e) {

System.err.println("Applet could not be created succesfully");

e.printStackTrace();

}

}

Do you get any error messages with your current program?

Message was edited by:

Djaunl

Djaunla at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 5
Why is it so URGUENT! is it's just a ' ... create a Black Jack card game to play around.' ?PS: Try what Djauni said.
abillconsla at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 6
While you should use invokeLater in init, start, stop... because invokeLater runs asynchronously,I doubt if you will catch any juicy exceptions...
Hippolytea at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 7
The big thing is getting the GUI building into either the init() method or another method that init() calls.
abillconsla at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 8

> While you should use invokeLater in init, start,

> stop... because invokeLater runs asynchronously,

> I doubt if you will catch any juicy exceptions...

Yes, you could use invokeAndWait instead, which executes synchronously. The reason I have invokeLater in there is because I was using it for one of my applications, and I just copied and pasted the code ;-).

Oops also, that code snippet I posted should probably say:

public void init() {

try {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

add(createComponents());

}

});

}

catch (Exception e) {

System.err.println("Applet could not be created succesfully");

e.printStackTrace();

}

}

Message was edited by:

Djaunl

Djaunla at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 9
> Why is it so URGUENT! is it's just a ' ... create a> Black Jack card game to play around.' ?cuz his #($*&@ problem is so much more important than anyone else's.
petes1234a at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 10

Djaunl your code works!!, the applet does start!!!!! However the component isnt showing.. I really do thank you for your valuable input, and my delayed response is because im testing it i over and over. The viewer just appears grey like nothing has been added to it.. any ideas plz?

I would also like to clarify to abillconsl and pete1234, This applet is urgent for me due to the fact that i have a deadline. "playing around" simply translates to: hassle a challenging 2d card game. Sorry for not being clear in the first place.

Message was edited by:

Makisan

Makisana at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 11
> This applet is urgent for me due to the fact that i have a deadline.Okay. But why is it urgent for us? It isn't. Next time you have a deadline, start earlier.
DrClapa at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 12
Ok DrClap i got your message, but i am still waiting for your enlightment :P
Makisana at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 13

> Ok DrClap i got your message, but i am still waiting

> for your enlightment :P

I think that the message is that most people help here out of the desire to share knowledge and to encourage new programmers. A poster's urgency isn't a motivating factor, and in fact if it's in the message title, it may do the exact opposite.

Good luck!

/Pete

petes1234a at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...
# 14
Thank you for the advice.
Makisana at 2007-7-12 10:02:40 > top of Java-index,Java Essentials,Java Programming...