Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Game for Small Children Program

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Game for Small Children Program

    Hello everyone. I've been told to post my questions here. I'm a novice programmer who needs to write a game program for small children that shows images of animals whose names are only three letters in length(e.g. "cat" "dog"). It has three panels for the letters to be placed and a keyboard of buttons for the child to choose from that places the chosen letter in one of the three panels. It still needs a play button, code that congratulates the child for getting the correct animal and vice versa, and it needs a "Play Again?" button. It supposed to use arrays, loops, and whatever else there is to use as a novice programmer. Here is a link to my code. Pastebin.com

    GuessMyAnimal1.jpg
    Here is a picture of the output running in it's current state.

    I'm using NetBeans if that makes a difference, the problem I'm having with my code is that I still need it to put in letters into the other two panels and recognize whether or not they are the correct letters. As a student I would like an explanation for any changes made, I'm here to help me learn how to write Java code, because my teacher is difficult to understand and doesn't always explain things very well.

    All help is appreciated and I thank you all in advance for even looking over my code. Below is a "too long; didn't read" version of my post.

    TL;DR
    Want to learn how to write code for the given school assignment, many thanks to those who help.


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Game for Small Children Program

    Hello Javazoid,

    Your ButtonHandler classes actionPerformed method always sets the text of box1Tf and never box2Tf/box3Tf. To do this you will need to create a logic statement checking to see if the boxes have text already or not and move on accordingly. That can be accomplished through some simple if statements. So if box1 has text then write to box2 or if box2 then write box3 etc... Of course that is only one way to go about it. To see if the answer is correct you will need to concatenate the contents of the three boxes and check it against an answer string. Hope that can help a bit.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Game for Small Children Program

    Okay, I've gotten a little further on the code and it now can display the three correct letters in the boxes. I didn't understand what you meant by "some simple if statement" not really sure which one. Assume I don't know anything about Java and explain it very basically, or give examples of code snippets I could apply. I still need the program to reset itself after the correct answer is received, and display an error when the wrong answer is received.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package pkg611;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class GuessMyAnimal extends JFrame{
        private static final int FRAME_WIDTH = 650;
        private static final int FRAME_HEIGHT = 400;
     
        private String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private String toBeGuessedImage =("DOG");
        private String letter1 = "D";
        private String letter2 = "O";
        private String letter3 = "G";
     
        private JButton[] keyBoardButtons;
     
        private JButton pictureBtn;
     
        private JPanel picturePanel;
        private JPanel displayPanel;
        private JPanel keyboardPanel;
     
        private JTextField box1Tf;
        private JTextField box2Tf;
        private JTextField box3Tf;
        private ButtonHandler handler;
     
        public GuessMyAnimal()
        {
            setTitle("Guess My Animal Name");
            setSize(FRAME_WIDTH, FRAME_HEIGHT);
            setLayout(new BorderLayout());
            //centers the window on the monitor
            setLocationRelativeTo(null);        
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            ////////////////////////////////////////
            ///////////////picturePanel/////////////
            ////////////////////////////////////////
            //Create a panel
            picturePanel = new JPanel();
            picturePanel.setLayout(new FlowLayout());
            //create the button
            pictureBtn = new JButton();
            //create an instance pf image
            ImageIcon image = new ImageIcon("dog.jpg");
            //place the image on button
            pictureBtn.setIcon(image);
            //Add object into the panel
            picturePanel.add(pictureBtn);
     
            //////////////////////////////////////
            ///////////displayPanel///////////////
            //////////////////////////////////////
            //Create a panel
            displayPanel = new JPanel();
            displayPanel.setLayout(new GridLayout(1, 3));
            //create textfields
            box1Tf = new JTextField();
            box2Tf = new JTextField();
            box3Tf = new JTextField();
            box1Tf.setEditable(false);
            box2Tf.setEditable(false);
            box3Tf.setEditable(false);
            displayPanel.add(box1Tf);
            displayPanel.add(box2Tf);
            displayPanel.add(box3Tf);
     
            /////////////////////////////////
            ////////keyboardPanel////////////
            /////////////////////////////////
            //Create a panel
            keyboardPanel = new JPanel();
            keyboardPanel.setLayout(new GridLayout(2, 13));
            //Allocate 26 x 4 byte array space
            keyBoardButtons = new JButton[26];
            //Create 26 buttons
            Create26Buttons();
            //Add buttons into the keyboard panel
            Add26ButtonsToPanel();
     
            //add panels to frame
            add(picturePanel, BorderLayout.NORTH);
            add(displayPanel, BorderLayout.CENTER);
            add(keyboardPanel, BorderLayout.SOUTH);
     
            setVisible(true);
        }
        public void Create26Buttons()
        {
            String letter;
     
            handler = new ButtonHandler();
            for(int index = 0; index < 26; index++)
            {
                letter = String.valueOf(abc.charAt(index));
     
                //create A button
                keyBoardButtons[index] = 
                        new JButton(letter);
                keyBoardButtons[index].addActionListener(handler);
     
     
            }
        }
        private class ButtonHandler implements ActionListener
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        String letter; 
                        for(int index = 0; index < 26; index++)
                        {
                            if(e.getSource() == keyBoardButtons[index])
                            {
                                letter = keyBoardButtons[index].getText();
                                checkAnswer(letter, index);
                                break;
                            }
                        }
                    }
                }
        public void checkAnswer(String letter, int index)
        {
            if(letter.equals(letter1))
            {
                box1Tf.setText(letter);
                keyBoardButtons[index].setBackground(Color.red);
            }
            if(letter.equals(letter2))
            {
                box2Tf.setText(letter);
                keyBoardButtons[index].setBackground(Color.red);
            }
            if(letter.equals(letter3))
            {
                box3Tf.setText(letter);
                keyBoardButtons[index].setBackground(Color.red);
            }
        }
        public void Add26ButtonsToPanel()
        {
            for(int index = 0; index < 26; index++)
                keyboardPanel.add(keyBoardButtons[index]);
        }
     
        public static void main(String[]args)
        {
            GuessMyAnimal game = new GuessMyAnimal();
        }
     
    }//end class

    This my updated code.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Game for Small Children Program

    The JOPtionPane class is an easy way to display short messages.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with my game of life program
    By thatni**a in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 11th, 2012, 09:20 AM
  2. REVERSI aka OTHELLO GAME HELP. I need help with this program. please
    By succ3ss91 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 13th, 2011, 08:21 AM
  3. Game Program HELPPPP
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 19
    Last Post: July 20th, 2011, 03:26 AM
  4. Breakout Game- program help
    By strength.honor in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 5th, 2010, 02:44 PM
  5. Error in Program for Game of Craps
    By TheAsianMenace in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 04:31 AM

Tags for this Thread