Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Pacman - KeyListener problem [SOLVED]
New Posts  All Forums:Forum Nav:

Java Pacman - KeyListener problem [SOLVED]

post #1 of 6
Thread Starter 
Hey guys

I'm currently working on an assignment which involves creating a multithreaded pacman game. Consists player controlled pacman, a ghost which will randomly move around the board at a set interval. The game also displays user score and ghost lives, counting down from 10. Also four buttons to start, stop, pause and resume the game.

I have created my ghost, pacman and main game classes. At the moment I have my gui built, the pacman is being drawn in the game. I can move him around using the keys. However, when I press the start button to begin animation, I can no longer move him around. I believe this is to do with the panels and the focusing with the JTextFields being used to display score and lives. Within the pacman class, it is set for focus. I have been messing around with it, tried to create a completely different approach but have not found a solution. I am either looking at something really simple and stupid or I am a million miles off the goal with my not so great programming skills.

When I do NOT have the JTextFields on, the game works as it should.

Any help would be appreciated guy.
Code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.*;

public class PacManGame
{

            JButton stopButton, startButton, pauseButton, resumeButton;
            JFrame gameFrame, help;
            PacMan pacman;
            //Ghost ghost;
                
    File quit;

    JPanel scorePanel = new JPanel();
    JLabel scoreLabel = new JLabel("Score");
    JLabel livesLabel = new JLabel("Ghost Lives");

    //JPanel gameboard = new JPanel();

    private JTextField scoreField = new JTextField(4);
    private JTextField livesField = new JTextField(4);
    int score = 0;
    int lives = 10;


                public static void main(String[] args){

           Runnable r = new Runnable(){
                                public void run()
                                {
                                        PacManGame game = new PacManGame();
                                        game.startGUI();

                                }
                        };
                        SwingUtilities.invokeLater(r);
       }


       public  void startGUI(){

           gameFrame = new JFrame("PacMan: Catch the Ghost");
           

           gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          
           pacman = new PacMan();
           pacman.setFocusable(true);
           gameFrame.add(pacman);
           
          // gameFrame.add(ghost);

               //-----------------------------------     MENUBAR   ---------------------

//menubar & menus

                JMenuBar menubar;
                JMenu filemenu, helpmenu;
                JMenuItem newitem,quititem,developeritem,helpitem;


                menubar = new JMenuBar();       // creates menubar
                
                gameFrame.setJMenuBar(menubar);         // adds menubar to the frame
                filemenu = new JMenu("File");         // creates the file menu
                helpmenu = new JMenu("Help");   // creates the help menu

                // Quit item. Used to quit program from the file menu
                quititem = new JMenuItem("Quit");
                quititem.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        quit();
                    }
                });

                // Adds MenuItems to the File Menu and then adds the File Menu to the Menu Bar
                filemenu.add(quititem);
                menubar.add(filemenu);



                //developer menu item

                developeritem = new JMenuItem("The Developer");
                developeritem.addActionListener(new ActionListener(){

                        public void actionPerformed(ActionEvent e){

                                help = new JFrame();    //creates new frame
                                help.setTitle("Help menu - Developer info");
                                help.setLocation(400,180);      //sets the location of the frame


                                //creates textarea
                                JTextArea devinfo = new JTextArea("STUDENT ID :  \n\nNAME:  \n\nEMAIL: \n\n");
                                devinfo.setLineWrap(true);
                                devinfo.append("\n\n");
                                devinfo.append("COURSE:   \n\n");
                                devinfo.append("YEAR : 2 \n\n");
                                devinfo.append("MODULE :  \n\n");
                                devinfo.append("LECT:  \n\n");
                                devinfo.setEditable(false); // created information is uneditable

                                help.setPreferredSize(new Dimension(375,350));  //sets size of the new frame
                                help.add(devinfo);      //adds textarea to the frame
                                help.pack();            //packs frame to fit the preferred sizes of components added
                                help.setVisible(true);          //shows the new frame

                        }});


//Program help item for help menu

                helpitem = new JMenuItem("Game Guide");
                helpitem.addActionListener(new ActionListener(){

                        public void actionPerformed(ActionEvent e){

                                help = new JFrame();    //creates new frame
                                help.setLocation(450,270);      //sets location of the new frame
                                help.setTitle("Game Guide");

                                //creates new textarea
                                JTextArea assist = new JTextArea("How to Play: \n\n");
                                assist.setLineWrap(true);
                                assist.append("Please consult the guide below to understand the game \n");
                                assist.append("1: Control the PacMan using the WSAD keys  \n\n");
                                assist.append("2: Move the PacMan around the level to catch the Ghost \n\n");
                                assist.append("3: The Ghost will move around the level every 1 second \n\n");
                                assist.append("4: When caught, the Ghost will loose a life and you gain 100 points \n\n\n");
                                assist.append("The game will end in 10minutes if the Ghost has not been caught \n\n");
                                assist.append(" \n\n");
                                assist.append(" \n\n");
                                assist.append(" \n\n");

                                assist.setEditable(false); // program assist set to uneditable


                                help.setPreferredSize(new Dimension(600,360));  //sets size of the new frame
                                help.add(assist);       //adds text area to frame
                                help.pack();            //packs frame to fit the preferred sizes of components
                                help.setVisible(true);  //shows the new frame


                        }});


                // Adds MenuItems to the Help Menu and then adds the Help Menu to the Menu Bar
                helpmenu.add(developeritem);
                helpmenu.add(helpitem);
                menubar.add(helpmenu);






//-----------------------------------   BUTTONS   ------------------------

               startButton = new JButton("Start");
               startButton.setEnabled(true);

               stopButton = new JButton("Stop");
               stopButton.setEnabled(false);

               pauseButton = new JButton("Pause");
               pauseButton.setEnabled(false);

               resumeButton = new JButton("Resume");
               resumeButton.setEnabled(false);


               startButton.addActionListener(new ActionListener(){

                   public void actionPerformed(ActionEvent e){

                          
                           pacman.setRunning(true);
                           pacman.setEnabled(true);
                           pacman.startGame();

                           startButton.setEnabled(false);
                           stopButton.setEnabled(true);
                           pauseButton.setEnabled(true);
                           resumeButton.setEnabled(true);
                   }
               });


               stopButton.addActionListener(new ActionListener(){

                   public void actionPerformed(ActionEvent e){

                           startButton.setEnabled(true);
                           stopButton.setEnabled(false);
                           pauseButton.setEnabled(false);
                           resumeButton.setEnabled(false);

                           pacman.stopGame();
                          // Thread.currentThread().interrupt();//stop a thread that could wait for a long period
                           pacman.setEnabled(false);

                   }

               });

               pauseButton.addActionListener(new ActionListener(){

                   public void actionPerformed(ActionEvent e){

                           startButton.setEnabled(false);
                           stopButton.setEnabled(true);
                           pauseButton.setEnabled(false);
                           resumeButton.setEnabled(true);

                           pacman.pauseGame();
                          

                   }

               });

               resumeButton.addActionListener(new ActionListener(){

                   public void actionPerformed(ActionEvent e){

                           startButton.setEnabled(false);
                           stopButton.setEnabled(true);
                           pauseButton.setEnabled(true);
                           resumeButton.setEnabled(false);

                           pacman.resumeGame();
                           
                   }

               });

               // create panel to hold buttons
               JPanel buttonPanel = new JPanel();
               buttonPanel.setBackground(Color.ORANGE);
               buttonPanel.setFocusable(false);
               // add the buttons to the panel
               buttonPanel.add(startButton);
               buttonPanel.add(stopButton);
               buttonPanel.add(pauseButton);
               buttonPanel.add(resumeButton);
               // add the buttonPanel to the frame
               //gameFrame.add(buttonPanel, BorderLayout.SOUTH);
               //buttonPanel.add(scoreLabel);
               //buttonPanel.add(scoreField);
               //buttonPanel.add(livesLabel);
               //buttonPanel.add(livesField);


               //-------------------------------   SCORE & LIVES  ---------------------------

                // build score box
                scoreField.setEditable(false);
                scorePanel.add(scoreLabel); // add to scorePanel
                scorePanel.add(scoreField);

                // build lives box
                livesField.setEditable(false);
                scorePanel.add(livesLabel); // add to scorePanel
                scorePanel.add(livesField);

                scorePanel.setBackground(Color.ORANGE);
                

                //add the components to buttonPanel
                gameFrame.add(buttonPanel, BorderLayout.SOUTH);
                buttonPanel.add(scorePanel);
               
               


               // Splat the window on screen
               gameFrame.pack();
               gameFrame.setSize(1020,650);
               gameFrame.setVisible(true);
               

        } // end startGUI

// Quit method for quititem
        public static void quit(){
            System.out.println("Quit when pressed");
            System.exit(0);
        }

}


Edited by Moynesy - 12/16/11 at 2:23pm
Arctic Thunder
(10 items)
 
ASUS G51VX-RX05
(8 items)
 
 
CPUGraphicsRAMHard Drive
T9900 3.06GHz GTX 260M 1GB 4GB DDR2 800MHz 320GB 
OSMonitorKeyboardMouse
Windows 7 IIyama ProLite E2773HDS Logitech G15 G5 
  hide details  
Reply
Arctic Thunder
(10 items)
 
ASUS G51VX-RX05
(8 items)
 
 
CPUGraphicsRAMHard Drive
T9900 3.06GHz GTX 260M 1GB 4GB DDR2 800MHz 320GB 
OSMonitorKeyboardMouse
Windows 7 IIyama ProLite E2773HDS Logitech G15 G5 
  hide details  
Reply
post #2 of 6
Thread Starter 
Solution was simple: I had the PacMan class set for focus, however I hadn't added code for JTextfields to set for focus (false). When JTextFields were created and delcared, this simple line of code was required.

scoreField.setFocusable(false); *this is added to the above main class, not the pacman class*

I really embarrassed myself with that one.
Edited by Moynesy - 12/16/11 at 2:30pm
Arctic Thunder
(10 items)
 
ASUS G51VX-RX05
(8 items)
 
 
CPUGraphicsRAMHard Drive
T9900 3.06GHz GTX 260M 1GB 4GB DDR2 800MHz 320GB 
OSMonitorKeyboardMouse
Windows 7 IIyama ProLite E2773HDS Logitech G15 G5 
  hide details  
Reply
Arctic Thunder
(10 items)
 
ASUS G51VX-RX05
(8 items)
 
 
CPUGraphicsRAMHard Drive
T9900 3.06GHz GTX 260M 1GB 4GB DDR2 800MHz 320GB 
OSMonitorKeyboardMouse
Windows 7 IIyama ProLite E2773HDS Logitech G15 G5 
  hide details  
Reply
post #3 of 6
What was the problem? I looked through the code and I couldn't figure it out myself.
post #4 of 6
Instead of deleting this thread it might be more beneficial if you put your post 1 back and than for post 2 say what you did to solve the problem than change the name of the thread to "Java Pacman - KeyListener problem [SOLVED]" so if other people have a similar problem they can check out your thread
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
post #5 of 6
Thread Starter 
Sorry, yeah good idea.
Arctic Thunder
(10 items)
 
ASUS G51VX-RX05
(8 items)
 
 
CPUGraphicsRAMHard Drive
T9900 3.06GHz GTX 260M 1GB 4GB DDR2 800MHz 320GB 
OSMonitorKeyboardMouse
Windows 7 IIyama ProLite E2773HDS Logitech G15 G5 
  hide details  
Reply
Arctic Thunder
(10 items)
 
ASUS G51VX-RX05
(8 items)
 
 
CPUGraphicsRAMHard Drive
T9900 3.06GHz GTX 260M 1GB 4GB DDR2 800MHz 320GB 
OSMonitorKeyboardMouse
Windows 7 IIyama ProLite E2773HDS Logitech G15 G5 
  hide details  
Reply
post #6 of 6
np, gj solving your problem!
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II x4 955 ASUS Crosshair 3 EVGA 465GTX 1GB 8GB DDR3 1600mhz GSkill/ADATA 
Hard DriveOSMonitorKeyboard
40GB Intel SSD 1TB Western Digital Black Edition Windows 7 x64 Pro Dual 21.5 Sceptre Saitek Eclipse 
PowerCaseMouseMouse Pad
850watt Silverstone Element Coolermaster Haf 932 Razer Lachesis Razer Vespula 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Pacman - KeyListener problem [SOLVED]