BlackJack trouble

Hey,

Im having trouble with it displaying the JOptionpane messages. The weird thing is when I minimize the applet and then maximize it the message is there. Any Idea how I could show a message other then JOptionPane with the code I have? Or is there a way to get the message to show up using JOptionPane.

Obviously I have other things to work out but I would like to figure this problem out first. Thanks

import java.util.Vector;

import java.util.Random;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.Image;

import java.awt.Graphics;

publicclass BlackJackextends JAppletimplements MouseListener,

MouseMotionListener

{

private Vector<Integer> random =new Vector<Integer>();

privateint[] placed =newint[52];

privateint[] cardType ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,52};

privateint[] num ={3,52,24,12,16,21,25,32,37,45,9,11,22,28,1,13,33,51,27,7,46,5,41,19,40,14,34,43,39,4,31,49,10,18,47,15,23,38,48,2,44,35,29,6,20,26,42,8,36,17,30};

private Image[] cards =new Image[52];

private Image[] back =new Image[1];

private Image[] noCard =new Image[1];

private Image[] hlight =new Image[1];

private Image[] bgImage =new Image[1];

private Image[] hold =new Image[1];

private Image[] dealer =new Image[1];

private Image[] exit =new Image[1];

private String graphic, message;

private Graphics e;

privateint temp;

privateint x, y,count=0, absTotal, total=0, Computer;

privateboolean cardClicked = false, done=false, paints=false;

privateboolean gameInProgress=true;

publicvoid init()

{

addMouseListener(this);

setSize(1000,600);

graphic ="art.jpg";

bgImage[0] = getImage(getCodeBase(), graphic);

graphic ="200.gif";

back[0] = getImage (getCodeBase(),graphic);

for (int x =0; x<51; x++)

{

graphic =""+x+".gif";

cards[x] = getImage (getCodeBase(),graphic);

}

graphic ="nocard.gif";

noCard[0] = getImage (getCodeBase(),graphic);

graphic ="overlay.gif";

hlight[0] = getImage(getCodeBase(),graphic);

for(int k=0; k <51; k++){

random.insertElementAt((int)((Math.random()*51)+1),k);

}

graphic ="hold.gif";

hold[0]= getImage(getCodeBase(),graphic);

graphic ="dealer.gif";

dealer[0]= getImage(getCodeBase(),graphic);

graphic ="exit.gif";

exit[0]= getImage(getCodeBase(),graphic);

while(Computer <= 12){

Computer=(int)(((Math.random()*21)+1));

}

}

publicvoid mouseClicked(MouseEvent event)

{}

publicvoid mouseEntered(MouseEvent event)

{}

publicvoid mouseExited(MouseEvent event)

{}

publicvoid mousePressed(MouseEvent event)

{}

publicvoid mouseReleased(MouseEvent event)

{

x = event.getX();

y = event.getY();

testClick(x, y);

}

publicvoid mouseDragged(MouseEvent event)

{

}

publicvoid mouseMoved(MouseEvent event)

{}

publicvoid paint(Graphics e){

super.paint(e);

e.drawImage(bgImage[0], 0, 0,this);

e.drawImage(hold[0], 400, 0,this);

e.drawImage(exit[0], 500, 0,this);

/************* Dealer ********************************/

e.drawImage(back[0], 0,300,this);

e.drawImage(back[0], 400,150,this);

e.drawImage(dealer[0], 440, 100,this);

e.drawImage(cards[random.elementAt(10)], 510,150,this);

/*******************************************************/

/*************Your Cards********************************/

e.drawImage(cards[random.elementAt(0)],200,300,this);

placed[0]=random.elementAt(0);

e.drawImage(cards[random.elementAt(1)],350,300,this);

placed[1]=random.elementAt(1);

e.drawImage(cards[random.elementAt(2)],500,300,this);

e.drawImage(cards[random.elementAt(3)],650,300,this);

e.drawImage(cards[random.elementAt(4)],800,300,this);

e.drawImage(noCard[0],500,300,this);

e.drawImage(noCard[0],650,300,this);

e.drawImage(noCard[0],800,300,this);

e.drawImage(hlight[0],500,300,this);

e.drawImage(hlight[0],650,300,this);

e.drawImage(hlight[0],800,300,this);

/*******************************************************/

/******************Adds cards to the applet to 3 other spots************/

if(cardClicked){

if(count == 1){

e.drawImage(cards[random.elementAt(2)],500,300,this);

placed[2]=random.elementAt(2);

}

if(count ==2){

e.drawImage(cards[random.elementAt(3)],650,300,this);

placed[3]=random.elementAt(3);

}

if(count ==3){

e.drawImage(cards[random.elementAt(4)],800,300,this);

placed[4]=random.elementAt(4);

}

}

/*******************************************************/

/*********This will test if the person is done and what the result is****************/

if(done){

absTotal = testCardTotal();

if(absTotal > 21){

JOptionPane.showMessageDialog(null,"Oh you went over 21! Too bad!","alert", JOptionPane.ERROR_MESSAGE);

doStand();

}

elseif(absTotal <= 21){

if(Computer < absTotal){

JOptionPane.showMessageDialog(null,"Your score of "+absTotal+" is better then the dealers "+Computer+"\nYou Win!","alert", JOptionPane.ERROR_MESSAGE);

doStand();

}

elseif(absTotal == Computer){

JOptionPane.showMessageDialog(null,"Your score of "+absTotal+" is the same as the dealers "+Computer+"\nDraw!!","alert", JOptionPane.ERROR_MESSAGE);

doStand();

}

elseif(Computer > absTotal && Computer < 22){

JOptionPane.showMessageDialog(null,"Your score of "+absTotal+" is less then the dealers "+Computer+"\nYou lose! Sucks to be You!","alert", JOptionPane.ERROR_MESSAGE);

doStand();

}

else

JOptionPane.showMessageDialog(null,"The Computer went over 21. Congrats... you win by Default.\n The two best words ever, De - Fault! De-Fault!","alert", JOptionPane.ERROR_MESSAGE);

doStand();

}

}

}

/*******************************************************/

/****************TestClick will test where person clicks*******************/

publicvoid testClick(int x,int y){

if(x >=0 && x <= 72 && y >=300 && y <= 396){

cardClicked =true;

count+=1;

if(count == 1){

repaint(500,300,72,96);

}

if(count ==2){

repaint(650,300,72,96);

}

if(count ==3){

repaint(800,300,72,96);

}

}

if(x>=400 && x<= 507 && y >= 0 && y <= 50){

done=true;

}

if(x>=500 && x <= 607 && y >= 0 && y <= 50){

setVisible(false);

}

}

/*******************************************************/

/************Adds the total amount of the cards on the applet**************/

publicint testCardTotal(){

for(int num=0; num < 5; num++){

if(placed[num] == 1 || placed[num] == 14 || placed[num] == 27 || placed[num] == 40){

placed[num] = 11;

}

elseif(placed[num] == 2 || placed[num] == 15 || placed[num] == 28 || placed[num] == 41){

placed[num] = 2;

}

elseif(placed[num] == 3 || placed[num] == 16 || placed[num] == 29 || placed[num] == 42){

placed[num] = 3;

}

elseif(placed[num] == 4 || placed[num] == 17 || placed[num] == 30 || placed[num] == 43){

placed[num] = 4;

}

elseif(placed[num] == 5 || placed[num] == 18 || placed[num] == 31 || placed[num] == 44){

placed[num] = 5;

}

elseif(placed[num] == 6 || placed[num] == 19 || placed[num] == 32 || placed[num] == 45){

placed[num] = 6;

}

elseif(placed[num] == 7 || placed[num] == 20 || placed[num] == 33 || placed[num] == 46){

placed[num] = 7;

}

elseif(placed[num] == 8 || placed[num] == 21 || placed[num] == 34 || placed[num] == 47){

placed[num] = 8;

}

elseif(placed[num] == 9 || placed[num] == 22 || placed[num] == 35 || placed[num] == 48){

placed[num] = 9;

}

elseif(placed[num] == 10 || placed[num] == 23 || placed[num] == 36 || placed[num] == 49){

placed[num] = 10;

}

elseif(placed[num] == 11 || placed[num] == 24 || placed[num] == 37 || placed[num] == 50){

placed[num] = 10;

}

elseif(placed[num] == 12 || placed[num] == 25 || placed[num] == 38 || placed[num] == 51){

placed[num] = 10;

}

elseif(placed[num] == 13 || placed[num] == 26 || placed[num] == 39 || placed[num] == 52){

placed[num] = 10;

}

total += placed[num];

}

for(int j=0; j < 5; j++){

if(placed[j] == 11 && total > 21){

total -=10;

}

}

done=false;

return total;

}

publicvoid doStand(){

repaint();

}

}

[18109 byte] By [Jingles87a] at [2007-11-27 4:02:31]
# 1

1. You shouldn't place the showMessageDialog and all those calculations inside the paint method! Restrict your paint method to the drawing and to the calculations absolutely necessary for it.

2. Do not call repaint (e.g. method doStand) from the paint method.

You could place all those messages into the testCardTotal method for example, but this is just a first approach advise, you could have far more better ideas if you think about.

sztyopeka at 2007-7-12 9:07:15 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank You Very much 'sztyopek' that helped alot! Again Thank You!
Jingles87a at 2007-7-12 9:07:15 > top of Java-index,Java Essentials,Java Programming...
# 3

One more question:

I've finished the game for the most part, but I want to have the user be able to reload the game (Reload the Applet), without having to restart the whole program over. I know you can refresh the applet on a webpage or whatever by clicking refresh but I'd like to be able to reload the applet by having the user click on an image and then it'll reload automatically. Does anyone have idea how to make this happen or if its even possible? Thanks --

Jingles87a at 2007-7-12 9:07:15 > top of Java-index,Java Essentials,Java Programming...
# 4

Sure, you just call a method that sets the state of your class back to whatever constitutes the start of the game.

Right now that code is scattered all over the place. Part of it is where you initialize the instance variables, part of it is in your init() method, and maybe elsewhere too. Just gather it all together into a reset() method.

DrClapa at 2007-7-12 9:07:15 > top of Java-index,Java Essentials,Java Programming...
# 5
Ya I know that code I had was messed up I changed a lot with it I just didnt feel like reposting the code.
Jingles87a at 2007-7-12 9:07:15 > top of Java-index,Java Essentials,Java Programming...
# 6
You may want to collect all required initialisations into the init() method.Read the API specification for methds init and start to see the difference, as JApplet extends Applet. http://java.sun.com/j2se/1.4.2/docs/api/java/applet/Applet.html
sztyopeka at 2007-7-12 9:07:15 > top of Java-index,Java Essentials,Java Programming...