more Hi/Lo code issues

Reply

Join Date: Dec 2008
Posts: 13
Reputation: krhillery is an unknown quantity at this point 
Solved Threads: 0
krhillery krhillery is offline Offline
Newbie Poster

more Hi/Lo code issues

 
0
  #1
Dec 1st, 2008
So I've spent the past 2 hours trying to work all of this out, but I am still getting an error message that seems out of place.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class HiLo extends JApplet
{
Image cardImages;
/**
* An image that holds the pictures of all the cards
*/

public void init()
{
/**
* This method loads the card picture and lays out the applet.
*/

cardImages = getImage(getCodeBase(), "smallcards.gif");

setBackground(Color.blue);

HiLo board = new HiLo();
getContentPane().add(board, BorderLayout.CENTER);

JPanel panel = new JPanel();
panel.setBackground(Color.green);
getContentPane().add(panel, BorderLayout.SOUTH);

JButton hi = new JButton("Higher");
hi.addActionListener(board);
buttonPanel.add(hi);

JButton lo = new JButton("Lower");
lo.addActionListener(board);
buttonPanel.add(lo);

JButton newGame = new JButton("New Game");
newGame.addActionListener(board);
buttonPanel.add(newGame);
}

public Insets getInsets()
{
/**
* Determines the boarders of the container
*/

return new Insets(3,3,3,3);
}
}

class HiLoCanvas extends JPanel implements ActionListener
{
/**
* The Canvas component represents a rectangular area on the screen
* which the application can draw or trap input events.
* [Help from Dad in this class]
*/

Deck deck;
/**
* The deck being used
*/
Hand hand;
/**
* Cards already dealt
*/
String message;
/**
* The message that is shown, saying the state of the game.
*/

boolean gameInProgress;
/**
* True when game begins, since it's in progress, and false at the end,
* when it is no longer in progress.
*/

Font mediumFont;
/**
* The font being used to show the messages.
*/

Font smallFont;
/**
* Being used to draw the cards.
*/

HiLoCanvas()
{
/**
* HiLoCanvas() is the constructor that creates and sets colors,
* and starts the games.
* [Help recieved from Dad]
*/

setBackground(Color.darkGray);
/**
* Sets the background color to dark gray.
*/
setForeground(Color.red);
/**
* Sets foreground to red
*/

smallFont = new Font("Arial", Font.PLAIN, 12);
/**
* Makes the small font that is being used to draw the cards of font
* type Arial, also plain (instead of italics, bold, etc.), and font
* size 12.
*/
mediumFont = new Font("Arial", Font.BOLD, 15);
/**
* The font being used to show the messages is font face Arial, bold,
* and size 15.
*/
doNewGame();
}

public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();

if (command.equals("Higher"))
{
doHi();
}
else if (command.equals("Lower"))
{
doLo();
}
else if (command.equals("New Game"))
{
doNewGame();
}
}

void doHi()
{
/**
* this is called by the actionPerformed() when the "Higher" button
* is clicked on. Game ends if user guessed wrong, or if they have
* correctly guessed four times.
*/
if (gameInProgress==false)
{
message = "Click\"New Game\" first";
repaint();
return;
}
hand.addCard(deck.dealCard());
/**
* Deals a card to the hand
*/
int cardCt = hand.getCardCount();
Card thisCard = hand.getCard(cardCt-1);
/**
* This is the card just dealt
*/
Card prevCard = hand.getCard(cardCt-2);
/**
* This is the previous card
*/
if(thisCard.getValue()<prevCard.getValue())
{
gameInProgress = false;
message = "Sorry, you lose!";
}
else if (thisCard.getValue()==prevCard.getValue())
{
gameInProgress=false;
message= "It's a tie--you lose!";
}
else if (cardCt==4)
{
gameInProgress=false;
message="Congratulations! You win!";
}
else
{
gameInProgress= true;
message = "Correct! Go for "+ cardCt +".";
}
repaint();
}

void doLo();
{
/**
* Same as doHi, but instead is called when the "Lower" button is
* selected.
*/
if (gameInProgress==false)
{
/**
* If the game is not in progress, if it's ended, then an
* error message will show.
*/
message="Click\"New Game\" first!";
repaint();
return;
}
hand.addCard(deck.dealCard());
int cardCt=hand.getCardCount();
Card thisCard=hand.getCard(cardCt-1);
Card prevCard=hand.getCard(cardCt-2);
if(thisCard.getValue()>prevCard.getValue())
{
gameInProgress=false;
message="Sorry! You lose!";
}
else if(thisCard.getValue()==prevCard.getValue())
{
gameInProgress=false;
message="It's a tie--you lose!";
}
else if(cardCt==4)
{
gameInProgress=false;
message="Congratulations! You win!";
}
else
{
gameInProgress=true;
message="Correct! Go for "+ cardCt+".";
}
repaint();
}

void doNewGame()
{
/**
* Called by actionPerformed() and the constructor, when user clicks
* on "New Game" button, a new game starts
*/
if (gameInProgress)
{
/**
* If a game is currently in progress, and error message will show
*/
message="You haven't finished the game yet!";
repaint();
return;
}
deck=new Deck();
hand = new Hand();
/**
* creat deck and hand used in this game
*/
deck.shuffle();
hand.addCard(deck.dealCar());
message="Do you think the next card is Higher or Lower?";
gameInProgress=true;
repaint();
}

public void paintComponent(Graphics g)
{
/**
* Draws message at the bottom of the canvas, and draws all of the
* cards spread across the canvas. When game is in progress, another
* card is dealt to represent the card that will be dealt next
* [Help from Dad]
*/
super.paintComponent(g);
g.setFont(mediumFont);
g.drawString(message,10, getSize().height-10);
g.setFont(smallFont);
int cardCt=hand.getCardCount();

for (int i=0; i<cardCt;i++)
drawCard(g, hand.getCard(i), 30 + i * 70, 10);

if(gameInProgress)
drawCard(g,null,30+cardCt*70, 10);
}
void drawCard(Graphics g, Card card, int x, int y)
{
/**
* Draws and 40 by 60 rectangle. The Card is drawn in the graphics g.
* When the card is null, a face-down card is then drawn.
* *********
*/
if(card==null)
{
g.setColor(Color.blue);
g.fillRect(x,y,40,60);
g.setColor(Color.white);
g.drawRect(x+3, y+3, 33, 53);
g.drawRect(x+4, y+4, 31, 51);
}
else
{
int row = 0;
switch(card.getSuit())
{
case Card.CLUBS: row=0; break;
case Card.HEARTS: row=1; break;
case Card.SPADES: row=2; break;
case Card.DIAMONDS: row=3; break;
}

int sx,sy;
/**
* The coordinates of the top left corner in the image
*/
sx=40*(card.getValue()-1);
sy=60*row;
g.drawImage(cardImages,x,y,x+40,y+60,sx,sy,sx+60,this);


}
}
[I have a class card and a class deck following this]


Up towards the top there is the:
Deck deck;
Hand hand;

and a few others.
The error message highlights 'Hand hand;' (im using BlueJ), and says "cannot find symbol-class Hand".
why? I really do not know, and I have tried to figure it out. Also, how would I fix this?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: more Hi/Lo code issues

 
0
  #2
Dec 1st, 2008
Please do not start multiple threads for what is essentially the same issue and use [code] [/code] tags (you can type them or use the button on the editor pane) around all code if you want anyone to bother to read it.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: more Hi/Lo code issues

 
0
  #3
Dec 2nd, 2008
if he says he can't find Symbol class hand, mostly that means he can't find it. my guess would be you forgot to code it, forgot to compile it, or are using a constructor you haven't defined in your Hand class
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: krhillery is an unknown quantity at this point 
Solved Threads: 0
krhillery krhillery is offline Offline
Newbie Poster

Re: more Hi/Lo code issues

 
0
  #4
Dec 2nd, 2008
my new code:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.Image;
  8.  
  9. public class HiLo extends JApplet
  10. {
  11. HiLoCanvas Bob;
  12. Image cardImages;
  13. /**
  14.   * An image that holds the pictures of all the cards
  15.   */
  16.  
  17. public void init()
  18. {
  19. /**
  20.   * This method loads the card picture and lays out the applet.
  21.   */
  22.  
  23. cardImages = getImage(getCodeBase(), "smallcards.gif");
  24. /**
  25.   * "smallcards.gif" is pretty much a condensed image collection of cards,
  26.   * so you don't have to get all 52 images needed.
  27.   * [found by Dad]
  28.   */
  29.  
  30. setBackground(Color.blue);
  31.  
  32. Bob = new HiLoCanvas();
  33. HiLo board = new HiLo();
  34. getContentPane().add(board, BorderLayout.CENTER);
  35.  
  36. JPanel panel = new JPanel();
  37. panel.setBackground(Color.green);
  38. getContentPane().add(panel, BorderLayout.SOUTH);
  39.  
  40. class addButtonListener implements ActionListener
  41. {
  42. public void actionPerformed(ActionEvent event)
  43. {
  44. String command = event.getActionCommand();
  45.  
  46. if (command.equals("Higher"))
  47. {
  48. Bob.doHi();
  49. }
  50. else if (command.equals("Lower"))
  51. {
  52. Bob.doLo();
  53. }
  54. else if (command.equals("New Game"))
  55. {
  56. Bob.doNewGame();
  57. }
  58. }
  59. }
  60.  
  61. ActionListener Blistener = new addButtonListener();
  62. JButton higher = new JButton("Higher");
  63. higher.addActionListener(Blistener);
  64. panel.add(higher);
  65.  
  66. JButton lower = new JButton("Lower");
  67. lower.addActionListener(Blistener);
  68. panel.add(lower);
  69.  
  70. JButton newGame = new JButton("New Game");
  71. newGame.addActionListener(Blistener);
  72. panel.add(newGame);
  73.  
  74.  
  75. }
  76.  
  77. public Insets getInsets()
  78. {
  79. /**
  80.   * Determines the boarders of the container
  81.   */
  82.  
  83. return new Insets(3,3,3,3);
  84. }
  85.  
  86.  
  87. class HiLoCanvas extends JPanel implements ActionListener
  88. {
  89. /**
  90.   * The Canvas component represents a rectangular area on the screen
  91.   * which the application can draw or trap input events.
  92.   * [Help from Dad in this method]
  93.   */
  94.  
  95. Deck deck;
  96. /**
  97.   * The deck being used
  98.   */
  99. Hand hand;
  100. /**
  101.   * Cards already dealt
  102.   */
  103. String message;
  104. /**
  105.   * The message that is shown, saying the state of the game.
  106.   */
  107.  
  108. boolean gameInProgress;
  109. /**
  110.   * True when game begins, since it's in progress, and false at the end,
  111.   * when it is no longer in progress.
  112.   */
  113.  
  114. Font mediumFont;
  115. /**
  116.   * The font being used to show the messages.
  117.   */
  118.  
  119. Font smallFont;
  120. /**
  121.   * Being used to draw the cards.
  122.   */
  123.  
  124. HiLoCanvas()
  125. {
  126. /**
  127.   * HiLoCanvas() is the constructor that creates and sets colors,
  128.   * and starts the games.
  129.   * [Help recieved from Dad]
  130.   */
  131.  
  132. setBackground(Color.darkGray);
  133. /**
  134.   * Sets the background color to dark gray.
  135.   */
  136. setForeground(Color.red);
  137. /**
  138.   * Sets foreground to red
  139.   */
  140.  
  141. smallFont = new Font("Arial", Font.PLAIN, 12);
  142. /**
  143.   * Makes the small font that is being used to draw the cards of font
  144.   * type Arial, also plain (instead of italics, bold, etc.), and font
  145.   * size 12.
  146.   */
  147. mediumFont = new Font("Arial", Font.BOLD, 15);
  148. /**
  149.   * The font being used to show the messages is font face Arial, bold,
  150.   * and size 15.
  151.   */
  152. doNewGame();
  153. }
  154.  
  155. public void actionPerformed(ActionEvent event)
  156. {
  157. String command = event.getActionCommand();
  158.  
  159. if (command.equals("Higher"))
  160. {
  161. doHi();
  162. }
  163. else if (command.equals("Lower"))
  164. {
  165. doLo();
  166. }
  167. else if (command.equals("New Game"))
  168. {
  169. doNewGame();
  170. }
  171. }
  172.  
  173. void doHi()
  174. {
  175. /**
  176.   * this is called by the actionPerformed() when the "Higher" button
  177.   * is clicked on. Game ends if user guessed wrong, or if they have
  178.   * correctly guessed four times.
  179.   */
  180. if (gameInProgress==false)
  181. {
  182. message = "Click\"New Game\" first";
  183. repaint();
  184. return;
  185. }
  186. hand.addCard(deck.dealCard());
  187. /**
  188.   * Deals a card to the hand
  189.   */
  190. int cardCt = hand.getCardCount();
  191. Card thisCard = hand.getCard(cardCt-1);
  192. /**
  193.   * This is the card just dealt
  194.   */
  195. Card prevCard = hand.getCard(cardCt-2);
  196. /**
  197.   * This is the previous card
  198.   */
  199. if(thisCard.getValue()<prevCard.getValue())
  200. {
  201. gameInProgress = false;
  202. message = "Sorry, you lose!";
  203. }
  204. else if (thisCard.getValue()==prevCard.getValue())
  205. {
  206. gameInProgress=false;
  207. message= "It's a tie--you lose!";
  208. }
  209. else if (cardCt==4)
  210. {
  211. gameInProgress=false;
  212. message="Congratulations! You win!";
  213. }
  214. else
  215. {
  216. gameInProgress= true;
  217. message = "Correct! Go for "+ cardCt +".";
  218. }
  219. repaint();
  220. }
  221.  
  222. void doLo()
  223. {
  224. /**
  225.   * Same as doHi, but instead is called when the "Lower" button is
  226.   * selected.
  227.   */
  228. if (gameInProgress==false)
  229. {
  230. /**
  231.   * If the game is not in progress, if it's ended, then an
  232.   * error message will show.
  233.   */
  234. message="Click\"New Game\" first!";
  235. repaint();
  236. return;
  237. }
  238. hand.addCard(deck.dealCard());
  239. int cardCt=hand.getCardCount();
  240. Card thisCard=hand.getCard(cardCt-1);
  241. Card prevCard=hand.getCard(cardCt-2);
  242. if(thisCard.getValue()>prevCard.getValue())
  243. {
  244. gameInProgress=false;
  245. message="Sorry! You lose!";
  246. }
  247. else if(thisCard.getValue()==prevCard.getValue())
  248. {
  249. gameInProgress=false;
  250. message="It's a tie--you lose!";
  251. }
  252. else if(cardCt==4)
  253. {
  254. gameInProgress=false;
  255. message="Congratulations! You win!";
  256. }
  257. else
  258. {
  259. gameInProgress=true;
  260. message="Correct! Go for "+ cardCt+".";
  261. }
  262. repaint();
  263. }
  264.  
  265. void doNewGame()
  266. {
  267. /**
  268.   * Called by actionPerformed() and the constructor, when user clicks
  269.   * on "New Game" button, a new game starts
  270.   */
  271. if (gameInProgress)
  272. {
  273. /**
  274.   * If a game is currently in progress, and error message will show
  275.   */
  276. message="You haven't finished the game yet!";
  277. repaint();
  278. return;
  279. }
  280. deck=new Deck();
  281. hand = new Hand();
  282. /**
  283.   * creat deck and hand used in this game
  284.   */
  285. deck.shuffle();
  286. hand.addCard(deck.dealCard());
  287. message="Do you think the next card is Higher or Lower?";
  288. gameInProgress=true;
  289. repaint();
  290. }
  291.  
  292. public void paintComponent(Graphics g)
  293. {
  294. /**
  295.   * Draws message at the bottom of the canvas, and draws all of the
  296.   * cards spread across the canvas. When game is in progress, another
  297.   * card is dealt to represent the card that will be dealt next
  298.   * [Help from Dad]
  299.   */
  300. super.paintComponent(g);
  301. g.setFont(mediumFont);
  302. g.drawString(message,10, getSize().height-10);
  303. g.setFont(smallFont);
  304. int cardCt=hand.getCardCount();
  305.  
  306. for (int i=0; i<cardCt;i++)
  307. drawCard(g, hand.getCard(i), 30 + i * 70, 10);
  308.  
  309. if(gameInProgress)
  310. drawCard(g,null,30+cardCt*70, 10);
  311. }
  312. void drawCard(Graphics g, Card card, int zebra, int cat)
  313. {
  314. /**
  315.   * Draws and 40 by 60 rectangle. The Card is drawn in the graphics g.
  316.   * When the card is null, a face-down card is then drawn.
  317.   * *********
  318.   */
  319. int row=0;
  320. if(card==null)
  321. {
  322. g.setColor(Color.blue);
  323. g.fillRect(zebra,cat,40,60);
  324. g.setColor(Color.white);
  325. g.drawRect(zebra+3, cat+3, 33, 53);
  326. g.drawRect(zebra+4, cat+4, 31, 51);
  327. }
  328. else
  329. {
  330. row = 0;
  331.  
  332. if( card.getSuit() == 1 )
  333. {
  334. row = 0;
  335. }
  336. else if(card.getSuit()==2)
  337. {
  338. row = 1;
  339. }
  340. else if(card.getSuit()==3)
  341. {
  342. row = 2;
  343. }
  344. else if(card.getSuit()==4)
  345. {
  346. row = 3;
  347. }
  348. }
  349.  
  350. int giraffe,dog;
  351. /**
  352.   * The coordinates of the top left corner in the image
  353.   */
  354. giraffe=40*(card.getValue()-1);
  355. dog=60*row;
  356. g.drawImage(cardImages,zebra,cat,zebra+40,cat+60,giraffe,dog,giraffe+60,this);
  357.  
  358.  
  359. }
  360. }
  361.  
  362. }

my last line of code:
  1. g.drawImage(cardImages,x,y,x+40,y+60,sx,sy,sx+60,this);

(which I have since changed the variable names upon teacher's request) is now the only line of code with an error message. When I compile, I am told "cannot find symbol--method drawImage(java.awt.Image,int,int,int,int,int,int,int,HiLo,HiLoCanvas)"

even my teacher was baffled by how to fix this. any suggestions or ideas?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: more Hi/Lo code issues

 
0
  #5
Dec 3rd, 2008
there is no method drawImage that accepts an Object of the type HiLo

check here to find which one you can use:
Graphics api
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: krhillery is an unknown quantity at this point 
Solved Threads: 0
krhillery krhillery is offline Offline
Newbie Poster

Re: more Hi/Lo code issues

 
0
  #6
Dec 3rd, 2008
well everything works, but nothing happens when I go to run it. do I need to use the appletviewer class?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC