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
[This message has been edited by Mark_Everson (edited October 10, 1999).]
[This message has been edited by Mark_Everson (edited October 12, 1999).]
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 12, 1999).]
Comment