private int h() { Stack<TreeNode<E>> nodeStack = new Stack<TreeNode<E>>(); Stack<Integer> leftStack = new Stack<Integer>(); Stack<Integer> rightStack = new Stack<Integer>(); nodeStack.push(root); leftStack.push(-1); rightStack.push(-1); while (true) { TreeNode<E> t = nodeStack.peek(); int left = leftStack.peek(); int right = rightStack.peek(); if (t == null) { nodeStack.pop(); leftStack.pop(); rightStack.pop(); int value = 0; if (nodeStack.isEmpty()) return value; else if (leftStack.peek() == -1) { leftStack.pop(); leftStack.push(value); } else { rightStack.pop(); rightStack.push(value); } } else if (left == -1) { nodeStack.push(t.left); leftStack.push(-1); rightStack.push(-1); } else if (right == -1) { nodeStack.push(t.right); leftStack.push(-1); rightStack.push(-1); } else { nodeStack.pop(); leftStack.pop(); rightStack.pop(); int value = 1 + Math.max(left, right); if (nodeStack.isEmpty()) return value; else if (leftStack.peek() == -1) { leftStack.pop(); leftStack.push(value); } else { rightStack.pop(); rightStack.push(value); } } } }
private synchronized void render(Graphics g) { if (level != null) { int xScroll = (int) (player.pos.x - screen.w / 2); int yScroll = (int) (player.pos.y - (screen.h - 24) / 2); soundPlayer.setListenerPosition((float) player.pos.x, (float) player.pos.y); level.render(screen, xScroll, yScroll); } if (!menuStack.isEmpty()) { menuStack.peek().render(screen); } Font.draw(screen, "FPS: " + fps, 10, 10); // for (int p = 0; p < players.length; p++) { // if (players[p] != null) { // String msg = "P" + (p + 1) + ": " + players[p].getScore(); // Font.draw(screen, msg, 320, screen.h - 24 + p * 8); // } // } if (player != null && menuStack.size() == 0) { Font.draw(screen, player.health + " / 10", 340, screen.h - 19); Font.draw(screen, "" + player.score, 340, screen.h - 33); } g.setColor(Color.BLACK); g.fillRect(0, 0, getWidth(), getHeight()); g.translate((getWidth() - GAME_WIDTH * SCALE) / 2, (getHeight() - GAME_HEIGHT * SCALE) / 2); g.clipRect(0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE); if (!menuStack.isEmpty() || level != null) { // render mouse renderMouse(screen, mouseButtons); g.drawImage(screen.image, 0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE, null); } // String msg = "FPS: " + fps; // g.setColor(Color.LIGHT_GRAY); // g.drawString(msg, 11, 11); // g.setColor(Color.WHITE); // g.drawString(msg, 10, 10); }
/** * parse the text. When the text is parsed the width, height, leading are all calculated. The text * will only be truly parsed if the graphics context has changed or the text has changed or the * font has changed. Otherwise nothing is done when this method is called. * * @param g Graphics context. */ public void parseText(Graphics g) { FontMetrics fm; TextState current = new TextState(); char ch; Stack state = new Stack(); int w = 0; if (lg != g) parse = true; lg = g; if (!parse) return; parse = false; width = 0; leading = 0; ascent = 0; descent = 0; height = 0; maxAscent = 0; maxDescent = 0; if (text == null || g == null) return; list.removeAllElements(); if (font == null) current.f = g.getFont(); else current.f = font; state.push(current); list.addElement(current); fm = g.getFontMetrics(current.f); for (int i = 0; i < text.length(); i++) { ch = text.charAt(i); switch (ch) { case '$': i++; if (i < text.length()) current.s.append(text.charAt(i)); break; /* ** Push the current state onto the state stack ** and start a new storage string */ case '{': w = current.getWidth(g); if (!current.isEmpty()) { current = current.copyState(); list.addElement(current); } state.push(current); current.x += w; break; /* ** Pop the state off the state stack and set the current ** state to the top of the state stack */ case '}': w = current.x + current.getWidth(g); state.pop(); current = ((TextState) state.peek()).copyState(); list.addElement(current); current.x = w; break; case '^': w = current.getWidth(g); if (!current.isEmpty()) { current = current.copyState(); list.addElement(current); } current.f = getScriptFont(current.f); current.x += w; current.y -= (int) ((double) (current.getAscent(g)) * sup_offset + 0.5); break; case '_': w = current.getWidth(g); if (!current.isEmpty()) { current = current.copyState(); list.addElement(current); } current.f = getScriptFont(current.f); current.x += w; current.y += (int) ((double) (current.getDescent(g)) * sub_offset + 0.5); break; default: current.s.append(ch); break; } } for (int i = 0; i < list.size(); i++) { current = ((TextState) (list.elementAt(i))); if (!current.isEmpty()) { width += current.getWidth(g); ascent = Math.max(ascent, Math.abs(current.y) + current.getAscent(g)); descent = Math.max(descent, Math.abs(current.y) + current.getDescent(g)); leading = Math.max(leading, current.getLeading(g)); maxDescent = Math.max(maxDescent, Math.abs(current.y) + current.getMaxDescent(g)); maxAscent = Math.max(maxAscent, Math.abs(current.y) + current.getMaxAscent(g)); } } height = ascent + descent + leading; return; }
public void actionPerformed(ActionEvent com) { String e = com.getActionCommand(); if (e.equals("1")) { msg.append("1"); } if (e.equals("2")) { msg.append("2"); } if (e.equals("3")) { msg.append("3"); } if (e.equals("4")) { msg.append("4"); } if (e.equals("5")) { msg.append("5"); } if (e.equals("6")) { msg.append("6"); } if (e.equals("7")) { msg.append("7"); } if (e.equals("8")) { msg.append("8"); } if (e.equals("9")) { msg.append("9"); } if (e.equals("0")) { msg.append("0"); } // *********************OPERANDS********************** if (e.equals("*")) { pushsign = 2; String peeked = new String(OperandStack.peek()); if (peeked == "*") { frac2 = Fraction(FracStack.pop()); frac1 = FracStack.pop(); frac3 = frac1.multiply(frac2); FracStack.push(frac3); FracStack.push(new Fraction(msg.getText())); OperandStack.push(peeked); } if (peeked == "/") { frac2 = new Fraction(FracStack.pop()); frac1 = new Fraction(FracStack.pop()); frac3 = frac1.multiply(frac2); FracStack.push(frac3); FracStack.push(new Fraction(msg.getText())); OperandStack.push(peeked); } } if (e.equals("/")) {} if (e.equals("+")) {} if (e.equals("-")) {} if (e.equals("=")) {} if (e.equals("Frac")) { msg.append("/"); } }
public void keyTyped(KeyEvent e) { if (!menuStack.isEmpty()) { menuStack.peek().keyTyped(e); } }
private void tick() { if (level != null) { if (level.player1Score >= Level.TARGET_SCORE) { addMenu(new WinMenu(GAME_WIDTH, GAME_HEIGHT, 1)); level = null; return; } if (level.player2Score >= Level.TARGET_SCORE) { addMenu(new WinMenu(GAME_WIDTH, GAME_HEIGHT, 2)); level = null; return; } if (keys.escape.wasPressed()) { clearMenus(); addMenu(new TitleMenu(GAME_WIDTH, GAME_HEIGHT)); level = null; return; } } if (packetLink != null) { packetLink.tick(); } mouseButtons.setPosition(getMousePosition()); if (!menuStack.isEmpty()) { menuStack.peek().tick(mouseButtons); } if (mouseMoved) { mouseMoved = false; mouseHideTime = 0; if (mouseHidden) { mouseHidden = false; } } if (mouseHideTime < 60) { mouseHideTime++; if (mouseHideTime == 60) { mouseHidden = true; } } mouseButtons.tick(); if (level != null) { if (synchronizer.preTurn()) { synchronizer.postTurn(); for (int index = 0; index < keys.getAll().size(); index++) { Keys.Key key = keys.getAll().get(index); boolean nextState = key.nextState; if (key.isDown != nextState) { synchronizer.addCommand(new ChangeKeyCommand(index, nextState)); } } if (keys.save.isDown) { level.save(); } keys.tick(); for (Keys skeys : synchedKeys) { skeys.tick(); } // if mouse is in use, update player orientation before level // tick if (!mouseHidden) { // update player mouse, in world pixels relative to player player.setAimByMouse( ((mouseButtons.getX() / SCALE) - (screen.w / 2)), (((mouseButtons.getY() / SCALE) + 24) - (screen.h / 2))); } else { player.setAimByKeyboard(); } level.tick(); } } if (createServerState == 1) { createServerState = 2; synchronizer = new TurnSynchronizer(MojamComponent.this, packetLink, localId, 2); clearMenus(); createLevel(TitleMenu.level); synchronizer.setStarted(true); packetLink.sendPacket(new StartGamePacket(TurnSynchronizer.synchedSeed, TitleMenu.level)); packetLink.setPacketListener(MojamComponent.this); } }