0

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?

Edited by Nick Evan: Fixed formatting

3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by krhillery
0

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.

0

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

0

my new code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Image;

public class HiLo extends JApplet
{
    HiLoCanvas Bob;
    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");
        /**
         * "smallcards.gif" is pretty much a condensed image collection of cards,
         * so you don't have to get all 52 images needed.
         * [found by Dad]
         */
        
        setBackground(Color.blue);
        
        Bob = new HiLoCanvas();
        HiLo board = new HiLo();
        getContentPane().add(board, BorderLayout.CENTER);
        
        JPanel panel = new JPanel();
        panel.setBackground(Color.green);
        getContentPane().add(panel, BorderLayout.SOUTH);
      
        class addButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                String command = event.getActionCommand();
        
            if (command.equals("Higher"))
           {
                Bob.doHi();
           }
           else if (command.equals("Lower"))
           {
                Bob.doLo();
           }
           else if (command.equals("New Game"))
           {
             Bob.doNewGame();
           }
            }
        }
        
        ActionListener Blistener = new addButtonListener();
        JButton higher = new JButton("Higher");
        higher.addActionListener(Blistener);
        panel.add(higher);
        
        JButton lower = new JButton("Lower");
        lower.addActionListener(Blistener);
        panel.add(lower);
        
        JButton newGame = new JButton("New Game");
        newGame.addActionListener(Blistener);
        panel.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 method]
     */
    
    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 event)
    {
        String command = event.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.dealCard());
            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 zebra, int cat)
    {
        /**
         * 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.
         * *********
         */
        int row=0;
        if(card==null)
        {
            g.setColor(Color.blue);
            g.fillRect(zebra,cat,40,60);
            g.setColor(Color.white);
            g.drawRect(zebra+3, cat+3, 33, 53);
            g.drawRect(zebra+4, cat+4, 31, 51);
        }
        else
        {
            row = 0;
            
            if( card.getSuit() == 1 )
            {
                row = 0;
            }
            else if(card.getSuit()==2)
            {
                row = 1;
            }
            else if(card.getSuit()==3)
            {
                row = 2;
            }
            else if(card.getSuit()==4)
            {
                row = 3;
            }
        }
            
           int giraffe,dog;
            /**
             * The coordinates of the top left corner in the image
             */
             giraffe=40*(card.getValue()-1);
             dog=60*row;
             g.drawImage(cardImages,zebra,cat,zebra+40,cat+60,giraffe,dog,giraffe+60,this);
   
                
    }
}

}

my last line of code:

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?

0

well everything works, but nothing happens when I go to run it. do I need to use the appletviewer class?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.