/* * The method maps the list of the active windows to the window's AppContext, * then the method registers ActiveWindowListener, GuiDisposedListener listeners; * it executes the initilialization only once per AppContext. */ @SuppressWarnings("unchecked") private static void initActiveWindowsTracking(Window w) { AppContext appContext = AppContext.getAppContext(); synchronized (appContext) { List<WWindowPeer> l = (List<WWindowPeer>) appContext.get(ACTIVE_WINDOWS_KEY); if (l == null) { l = new LinkedList<WWindowPeer>(); appContext.put(ACTIVE_WINDOWS_KEY, l); appContext.addPropertyChangeListener(AppContext.GUI_DISPOSED, guiDisposedListener); KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); kfm.addPropertyChangeListener("activeWindow", activeWindowListener); } } }
public void propertyChange(PropertyChangeEvent e) { boolean isDisposed = (Boolean) e.getNewValue(); if (isDisposed != true) { if (log.isLoggable(PlatformLogger.Level.FINE)) { log.fine(" Assertion (newValue != true) failed for AppContext.GUI_DISPOSED "); } } AppContext appContext = AppContext.getAppContext(); synchronized (appContext) { appContext.remove(ACTIVE_WINDOWS_KEY); appContext.removePropertyChangeListener(AppContext.GUI_DISPOSED, this); KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); kfm.removePropertyChangeListener("activeWindow", activeWindowListener); } }
/** * Because there may be many AppContexts, and we can't be sure where this EmbeddedFrame is first * created or shown, we can't automatically determine the correct KeyboardFocusManager to attach * to as KeyEventDispatcher. Those who want to use the functionality of traversing out of the * EmbeddedFrame must call this method on the Applet's AppContext. After that, all the changes can * be handled automatically, including possible replacement of KeyboardFocusManager. */ public void registerListeners() { if (appletKFM != null) { removeTraversingOutListeners(appletKFM); } appletKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager(); if (isVisible()) { addTraversingOutListeners(appletKFM); } }
/** * Needed to track which KeyboardFocusManager is current. We want to avoid memory leaks, so when * KFM stops being current, we remove ourselves as listeners. */ public void propertyChange(PropertyChangeEvent evt) { // We don't handle any other properties. Skip it. if (!evt.getPropertyName().equals("managingFocus")) { return; } // We only do it if it stops being current. Technically, we should // never get an event about KFM starting being current. if (evt.getNewValue() == Boolean.TRUE) { return; } // should be the same as appletKFM removeTraversingOutListeners((KeyboardFocusManager) evt.getSource()); appletKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager(); if (isVisible()) { addTraversingOutListeners(appletKFM); } }
/** Deregister us as KeyEventDispatcher and property "managingFocus" listeners. */ private void removeTraversingOutListeners(KeyboardFocusManager kfm) { kfm.removeKeyEventDispatcher(this); kfm.removePropertyChangeListener("managingFocus", this); }
private void playerSetup() { players = new ArrayList(twoPlayerRB.isSelected() ? 2 : 1); if (mouseRB.isSelected()) { MouseControlledPlayer p1 = new MouseControlledPlayer(width / 2, height / 2, 3, true, this, 3, 1, width); addMouseMotionListener(p1); addMouseListener(p1); players.add(p1); } else { KeyboardControlledPlayer p1 = new KeyboardControlledPlayer( width / 2, height / 2, 3, true, this, 3, KeyEvent.VK_W, KeyEvent.VK_A, KeyEvent.VK_S, KeyEvent.VK_D, keyboardSpeedS1.getValue(), KeyEvent.VK_SPACE, KeyEvent.VK_F, 1, width); KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(p1); players.add(p1); } if (twoPlayerRB.isSelected()) { if (mouseRB.isSelected()) { KeyboardControlledPlayer p2 = new KeyboardControlledPlayer( width / 2, height / 2, 3, true, this, 3, KeyEvent.VK_W, KeyEvent.VK_A, KeyEvent.VK_S, KeyEvent.VK_D, keyboardSpeedS2.getValue(), KeyEvent.VK_SPACE, KeyEvent.VK_F, 2, width); KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(p2); players.add(p2); } else { KeyboardControlledPlayer p2 = new KeyboardControlledPlayer( width / 2, height / 2, 3, true, this, 3, KeyEvent.VK_NUMPAD8, KeyEvent.VK_NUMPAD4, KeyEvent.VK_NUMPAD5, KeyEvent.VK_NUMPAD6, keyboardSpeedS2.getValue(), KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD1, 2, width); KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(p2); players.add(p2); } } }
private void drawLayout(Graphics g) { g.setColor(Color.blue); g.drawRect(10, 10, width - 20, height - 20); g.setColor(Color.red.brighter()); Iterator f = players.iterator(); int lifeDisplayPosition = 0; while (f.hasNext()) { Player h = (Player) f.next(); if (h.isActive()) { lifeDisplayPosition++; String lifeInformation = "Player " + lifeDisplayPosition + ": "; for (int i = 0; i < h.getLives(); i++) { lifeInformation += "\u2606"; } g.drawString(lifeInformation, 20, lifeDisplayPosition * 40); } } borders[2] = width - 20; borders[3] = height - 20; Iterator i = players.iterator(); Player p; while (i.hasNext()) { p = (Player) i.next(); if (p.isActive() && !countdownF && (p.getX() < borders[0] || p.getY() < borders[1] || p.getX() > borders[2] || p.getY() > borders[3]) && (!p.getImmunity())) { p.decLives(width / 2, height / 2); p.setImmunity(true); } if (p.getLives() <= 0) { if (p instanceof MouseControlledPlayer) { MouseControlledPlayer m = (MouseControlledPlayer) p; removeMouseListener(m); removeMouseMotionListener(m); } else if (p instanceof KeyboardControlledPlayer) { KeyboardFocusManager.getCurrentKeyboardFocusManager() .removeKeyEventDispatcher((KeyboardControlledPlayer) p); } i.remove(); for (int h = 0; h < 4; h++) { if (deathLocation[h] == -1) { deathLocation[h] = p.getX(); deathLocation[h + 1] = p.getY(); break; } } } } if ((deathLocation[0] != -1) && (deathLocation[1] != -1)) { g.drawLine( deathLocation[0] - 15, deathLocation[1] - 15, deathLocation[0] + 15, deathLocation[1] + 15); g.drawLine( deathLocation[0] + 15, deathLocation[1] - 15, deathLocation[0] - 15, deathLocation[1] + 15); } if ((deathLocation[2] != -1) && (deathLocation[3] != -1)) { g.drawLine( deathLocation[2] - 15, deathLocation[3] - 15, deathLocation[2] + 15, deathLocation[3] + 15); g.drawLine( deathLocation[2] + 15, deathLocation[3] - 15, deathLocation[2] - 15, deathLocation[3] + 15); } if (!onePlayerAlive) { Font old = g.getFont(); g.setFont(new Font("monospaced", Font.BOLD, 20)); g.setColor(Color.red.darker()); g.drawString("GAME OVER", width / 2 - 40, height / 2); g.setFont(old); g.setColor(Color.WHITE); g.drawString("Score", width - 60, 25); g.drawString(String.valueOf(score), width - 60, 40); } }