Announcement

Collapse
No announcement yet.

Coding Problem: I can't get a KeyListener to work... Suggestions?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Coding Problem: I can't get a KeyListener to work... Suggestions?

    Looking for suggestions on what I'm doing wrong from someone in the group who knows
    java better than I. The idea is to let keyPress events that happen when the ToolBar
    is the focus get passed on to the map which is where the key press events would be used.
    This is just so if you change the focus to the tool bar by pushing a button on it, that
    you can still enter commands to the map. But my key events vanish into the void....
    KeyPressed is never called... Here's the code, I've made the relevant parts
    bold. What am I screwing up?
    I haven't done any of this stuff in Ages so its probably a stupid error

    Thanks,

    Mark

    Code:
    /*
         
     class ToolBarFrame,[ PI ](aka Toolbar) is designed to always be available. 
     It has Buttons for:  
    1.  new turn (and x10, x100), [FI]
    2.  map button to generate local maps, [FI]
    3.  tech to access tech devel chart for the civ, [PI] and 
    4.  Econ button [PI] 
    5.  Diplomacy [P]
    6.  Government [P]
    7.  Other Buttons… whatever’s useful
    8.  Pull Downs for various utility functions - suggestions? [P]
        
    */
    
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class ToolBarFrame extends GameFrame [b]implements KeyListener[/b]{
    	void autoPilotCheckbox_Action(java.awt.event.ItemEvent event) {
    		Game.setAutoPilot(autoPilotCheckbox.getState());
    	}
    	void econButton_Action(java.awt.event.ActionEvent event) {
            Game.getPlayerCiv().civEconStrat[0].editEconStrategy();
    	}
    	void tenTurnButton_Action(java.awt.event.ActionEvent event) {
            Game.tenTurn();
    	}
    	void toTechButton_Action(java.awt.event.ActionEvent event) {
    		(new TechFrame( Game.getPlayerCiv().provs[1])).show();
    	}
    	void toMapButton_Action(java.awt.event.ActionEvent event) {
    		(new MapSquaresFrame(20,15)).show();
    	}
    	void oneTurnButton_Action(java.awt.event.ActionEvent event) {
            Game.newTurn();
    	}
    	void hundredTurnButton_Action(java.awt.event.ActionEvent event) {
            Game.hundredTurn();
    	}
    	void Open_Action(java.awt.event.ActionEvent event) {
    		openFileDialog1.show();
    	}
    	void About_Action(java.awt.event.ActionEvent event) {
    		(new AboutDialog(this, true)).show();
    	}
    
    	void Exit_Action(Event event) {
    		(new QuitDialog(this, true)).show();
    	}
    	
    //////////////////////////////////////////////////////////////////////////////////////////
    //
    // kludge to show turn number
    
        void refresh(){
    
            int strLoc = 0;
            int moneyHere = (new Float(Game.getPlayerCiv().money)).intValue();
    
            if((strLoc = getTitle().indexOf("Turn:")) > 0)
                setTitle((getTitle()).substring(0,strLoc) + "Turn:"+Game.getTurnNumber() + ",   $= " 
    + moneyHere + ",   Export: " + (int)Game.getPlayerCiv().totalExportAvail
    + "  Mil. Power: "+ (int) Game.getPlayerCiv().getTotalMilPower()+" / "
                + (int) Game.getPlayerCiv().coreAI.offensivePower);
    	}
    
    
    	public ToolBarFrame() {
    
    		//{{INIT_CONTROLS
    		setLayout(new BorderLayout(0,0));
    		setForeground(java.awt.Color.blue);
    		setSize(512,70);
    		setVisible(false);
    		openFileDialog1.setMode(FileDialog.LOAD);
    		openFileDialog1.setTitle("Open");
    		//$$ openFileDialog1.move(12,96);
    		checkbox1.setLabel("checkbox");
    		add(BorderLayout.CENTER, checkbox1);
    		checkbox1.setBounds(0,48,512,53);
    		checkbox1.setVisible(false);
    //		toolBarPanel1.setAlignmentY(0.363636F);
    		add(BorderLayout.NORTH, toolBarPanel1);
    		toolBarPanel1.setBackground(java.awt.Color.lightGray);
    		toolBarPanel1.setBounds(0,0,512,48);
    		oneTurnButton.setLabel("1 Turn");
    		oneTurnButton.setActionCommand("1 Turn");
    		toolBarPanel1.add(oneTurnButton);
    		oneTurnButton.setBounds(0,0,44,40);
    		tenTurnButton.setLabel("10 Turns");
    		tenTurnButton.setActionCommand("10 Turns");
    		toolBarPanel1.add(tenTurnButton);
    		tenTurnButton.setBounds(54,0,45,40);
    		hundredTurnButton.setLabel("100 Turns");
    		hundredTurnButton.setActionCommand("100 Turns");
    		toolBarPanel1.add(hundredTurnButton);
    		hundredTurnButton.setBounds(109,0,56,40);
    		toMapButton.setLabel("Map");
    		toMapButton.setActionCommand("Map");
    		toolBarPanel1.add(toMapButton);
    		toMapButton.setBounds(185,0,44,40);
    		toTechButton.setLabel("Tech");
    		toTechButton.setActionCommand("Tech");
    		toolBarPanel1.add(toTechButton);
    		toTechButton.setBounds(239,0,44,40);
    		econButton.setLabel("Econ");
    		econButton.setActionCommand("Econ");
    		toolBarPanel1.add(econButton);
    		econButton.setBounds(293,0,45,40);
    		autoPilotCheckbox.setState(false);
    		autoPilotCheckbox.setLabel("Auto AI");
    		toolBarPanel1.add(autoPilotCheckbox);
    		autoPilotCheckbox.setFont(new Font("Dialog", Font.BOLD, 14));
    		autoPilotCheckbox.setBounds(388,8,62,24);
    		haltCheckbox.setLabel("HALT");
    		toolBarPanel1.add(haltCheckbox);
    		haltCheckbox.setBounds(435,8,60,23);
    		setTitle(" Turn: 0");
    		//}}
    
    		//{{INIT_MENUS
    		menu1.setLabel("File");
    		menu1.add("New");
    		menu1.add(miOpen);
    		miOpen.setLabel("Open...");
    		menu1.add("Save");
    		menu1.add("Save As...");
    		menu1.addSeparator();
    		menu1.add(miSystem);
    		miSystem.setLabel("Exit");
    		mainMenuBar.add(menu1);
    		menu2.setLabel("Edit");
    		menu2.add("Cut");
    		menu2.add("Copy");
    		menu2.add("Paste");
    		mainMenuBar.add(menu2);
    		menu3.setLabel("Help");
    		menu3.add(miAbout);
    		miAbout.setLabel("About");
    		mainMenuBar.add(menu3);
    		mainMenuBar.setHelpMenu(menu3);
    		//$$ mainMenuBar.move(60,96);
    		setMenuBar(mainMenuBar);
    		//}}
    
    		//{{REGISTER_LISTENERS
    		[b]addKeyListener(this);[/b]
    		Action lAction = new Action();
    		hundredTurnButton.addActionListener(lAction);
    		oneTurnButton.addActionListener(lAction);
    		toMapButton.addActionListener(lAction);
    		toTechButton.addActionListener(lAction);
    		tenTurnButton.addActionListener(lAction);
    		econButton.addActionListener(lAction);
    		Item lItem = new Item();
    		autoPilotCheckbox.addItemListener(lItem);
    		miOpen.addActionListener(lAction);
    		miAbout.addActionListener(lAction);
    		miSystem.addActionListener(lAction);
    		//}}
    	}
    
    	public ToolBarFrame(String title) {
    	    this();
    	    setTitle(title);
    	}
    
        public synchronized void show() {
        	move(0, 0);
        	super.show();
        }
     
     [b]
     public void keyTyped(KeyEvent e){
     }
     public void keyReleased(KeyEvent e){
     }
     /** Doesn't work, Idea is Key presses when tool bar has focus will be sent to a MapCanvas */
     public void keyPressed(KeyEvent e){
        IO.out.println("In key pressed of tool bar frame a ");
        if( mapCanvasToSendKeyPressesTo == null ){
            mapCanvasToSendKeyPressesTo = GUI.findMapCanvasToSendKeyPressesTo();
            IO.out.println("In key pressed of tool bar frame b ");   
        }
        if( mapCanvasToSendKeyPressesTo != null ){    
           mapCanvasToSendKeyPressesTo.keyPressedFromGUI(e);
           IO.out.println("In key pressed of tool bar frame c ");    
        }
        IO.out.println("In key pressed of tool bar frame d ");
        
     }
     [/b]
    	//{{DECLARE_CONTROLS
    	java.awt.FileDialog openFileDialog1 = new java.awt.FileDialog(this);
    	java.awt.Checkbox checkbox1 = new java.awt.Checkbox();
    	Panel toolBarPanel1 = new Panel();
    	Button oneTurnButton = new Button();
    	Button tenTurnButton = new Button();
    	Button hundredTurnButton = new Button();
    	Button toMapButton = new Button();
    	Button toTechButton = new Button();
    	Button econButton = new Button();
    	java.awt.Checkbox autoPilotCheckbox = new java.awt.Checkbox();
    	java.awt.Checkbox haltCheckbox = new java.awt.Checkbox();
    	MapCanvas mapCanvasToSendKeyPressesTo = null;
    	//}}
    
    	//{{DECLARE_MENUS
    	java.awt.MenuBar mainMenuBar = new java.awt.MenuBar();
    	java.awt.Menu menu1 = new java.awt.Menu();
    	java.awt.MenuItem miOpen = new java.awt.MenuItem();
    	java.awt.MenuItem miSystem = new java.awt.MenuItem();
    	java.awt.Menu menu2 = new java.awt.Menu();
    	java.awt.Menu menu3 = new java.awt.Menu();
    	java.awt.MenuItem miAbout = new java.awt.MenuItem();
    	//}}
    
    	class Action implements java.awt.event.ActionListener {
    		public void actionPerformed(java.awt.event.ActionEvent event) {
    			Object object = event.getSource();
    			if (object == hundredTurnButton)
    				hundredTurnButton_Action(event);
    			else if (object == oneTurnButton)
    				oneTurnButton_Action(event);
    			else if (object == toMapButton)
    				toMapButton_Action(event);
    			else if (object == toTechButton)
    				toTechButton_Action(event);
    			else if (object == tenTurnButton)
    				tenTurnButton_Action(event);
    			else if (object == econButton)
    				econButton_Action(event);
    			else if (object == miOpen)
    				Open_Action(event);
    			else if (object == miAbout)
    				About_Action(event);
    			else if (object == miSystem)
    			    System.exit(0);
    				//System(event);
    		    
    		}
    		
    	}
    
    	class Item implements java.awt.event.ItemListener {
    		public void itemStateChanged(java.awt.event.ItemEvent event) {
    			Object object = event.getSource();
    			if (object == autoPilotCheckbox)
    				autoPilotCheckbox_Action(event);
    		}
    	}
    	
    
    }
    [This message has been edited by Mark_Everson (edited October 10, 1999).]
    [This message has been edited by Mark_Everson (edited October 12, 1999).]
    Project Lead for The Clash of Civilizations
    A Unique civ-like game that will feature low micromanagement, great AI, and a Detailed Government model including internal power struggles. Demo 8 available Now! (go to D8 thread at top of forum).
    Check it out at the Clash Web Site and Forum right here at Apolyton!

  • #2
    I haven't used KeyListeners for a while, but I seem to remember having a problem with them before.

    All I can think of is - try adding the KeyListener to a Panel/Applet within a Frame, instead of to a Frame directly.

    Jim

    Comment


    • #3
      Hey Jim:

      That's a good idea. I actually thought of it, but then forgot it in the midst of trying other things. That may be it...
      Project Lead for The Clash of Civilizations
      A Unique civ-like game that will feature low micromanagement, great AI, and a Detailed Government model including internal power struggles. Demo 8 available Now! (go to D8 thread at top of forum).
      Check it out at the Clash Web Site and Forum right here at Apolyton!

      Comment


      • #4

        If I am not mistaken, you have to add a MapCanvas object as KeyListener to each ToolBarFrame's component which is focus traversable. So, when the focus is on a ToolBarFrame's button its key events are caught by the MapCanvas object.

        Comment


        • #5
          Hi Ferdi:

          Good Call, I'm sure that's it! And that's also why I and JimC have had better luck with Canvases, they don't have all those buttons and such to steal the focus!

          Thanks for the help. Interested in being the Java Jock I need to help figure out a good distribution scheme for the demo?

          -Mark
          Project Lead for The Clash of Civilizations
          A Unique civ-like game that will feature low micromanagement, great AI, and a Detailed Government model including internal power struggles. Demo 8 available Now! (go to D8 thread at top of forum).
          Check it out at the Clash Web Site and Forum right here at Apolyton!

          Comment


          • #6
            Thanks Ferdi, that was indeed the problem.

            Mark
            Project Lead for The Clash of Civilizations
            A Unique civ-like game that will feature low micromanagement, great AI, and a Detailed Government model including internal power struggles. Demo 8 available Now! (go to D8 thread at top of forum).
            Check it out at the Clash Web Site and Forum right here at Apolyton!

            Comment


            • #7
              Mark,

              Sure, your job is great, I've read quicky what you are about to include into The Clash of Civilizations and I have only one word : Excellent!
              Yes, I am interested to help you but I fear not to have the time to do that. I am very busy for the moment, only a few minutes in the morning to read some posts in this forum!
              Anyway, if you have any question in Java I can try to answer you but the reply could take a while...(we are not in the same time zone)
              Also don't forget that English isn't my native tongue (French) and it's more difficult for me to tell you with the good words what I have in my mind .

              Good luck,
              Ferdi

              Comment


              • #8
                Ferdi:

                Well thank you for the kind words, and the offer. Right now I don't have any quick questions, but will keep you in mind. I hope you get some time free, I would love to be able to drag you into the project. If you have any takes on what we're doing wrong with the demos I'd certainly like to hear what you have to say.

                bonne chance,

                Mark
                Project Lead for The Clash of Civilizations
                A Unique civ-like game that will feature low micromanagement, great AI, and a Detailed Government model including internal power struggles. Demo 8 available Now! (go to D8 thread at top of forum).
                Check it out at the Clash Web Site and Forum right here at Apolyton!

                Comment

                Working...
                X