📄 screen.java
字号:
/* * Copyright (c) 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based * names, or the names of contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or maintenance * of any nuclear facility. $Id: Screen.java,v 1.5 2003/06/03 22:20:35 ro89390 Exp $ */package com.sun.j2me.blueprints.jbricks;import javax.microedition.lcdui.*;/** * This class handles and coordinates most of the * drawing for this game. It knows how to draw the * splash and game over screen, as well as the * actual games screen. It also forwards 'cooked' * key events to the Engine class. * <p> * This class has some public static fields. These are * used for two purposes. First, screen width and height * are used to dynamically calculate dimensions of some * graphical objects. Second, the graphics context and * background color are used for drawing optimizations. * The fields are declared static so that not all classes * need to have a reference to the screen class, or need * to cumbersomely retrieve it through * '(Screen)Display.getDisplay().getCurrent()'. */public class Screen extends Canvas { private Font font_small; private Font font_medium; private Font font_large; private Font font; public static int width; public static int height; private static final int TL_ANCHOR = Graphics.TOP | Graphics.LEFT; private static final int TH_ANCHOR = Graphics.TOP | Graphics.HCENTER; private Image buf; public static Graphics GRAPHICS; public static int VDIVIDER; public static final int BACKGROUND = ThreeDColor.gray.getRGB(); private int wildcard_y; private int last_state; private int last_score; private int last_lives; private Engine engine; private EngineState state; private long last; private int fps; private int font_voff; private String fps_string; private boolean show_fps; public Screen() { int i; width = getWidth(); height = getHeight(); font_small = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); font_medium = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM); font_large = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE); font = height < 150 ? font_small : font_medium; font_voff = font.getHeight(); buf = Image.createImage(width, height - (font_voff + 5)); VDIVIDER = buf.getHeight(); GRAPHICS = buf.getGraphics(); state = new EngineState(); wildcard_y = height + height / 3; last_state = -1; last_score = -1; last_lives = -1; fps_string = "0"; show_fps = false; } public void setEngine(Engine engine) { this.engine = engine; } public void keyPressed(int keycode) { engine.keyPressed(keycode, getGameAction(keycode)); } public void keyReleased(int keycode) { engine.keyReleased(keycode, getGameAction(keycode)); } public boolean isShowFPS() { return show_fps; } public void setShowFPS(boolean x) { show_fps = x; } private void paintBackground(Graphics g) { int i; g.setColor(BACKGROUND); g.fillRect(0, 0, width, height); } private void paintTitle(Graphics g) { engine.getState(state); if (wildcard_y > height / 2 + 15) { wildcard_y -= 4; } if (last_state != Engine.TITLE) { paintBackground(GRAPHICS); state.bricks.paintShadow(GRAPHICS); state.bricks.paint(GRAPHICS); } g.drawImage(buf, 0, 0, TL_ANCHOR); g.setColor(BACKGROUND); g.fillRect(0, height - (font_voff + 5), width, font_voff + 5); g.setFont(font_large); g.setColor(ThreeDColor.black.getRGB()); g.drawString("Bricks", width / 2 + 1, height / 2 + 1, TH_ANCHOR); g.setColor(ThreeDColor.red.getRGB()); g.drawString("Bricks", width / 2, height / 2, TH_ANCHOR); g.setFont(font_medium); g.setColor(ThreeDColor.black.getRGB()); g.drawString("by wildcard", width / 2 + 1, wildcard_y + 1, TH_ANCHOR); g.setColor(ThreeDColor.orange.getRGB()); g.drawString("by wildcard", width / 2, wildcard_y, TH_ANCHOR); } private void paintOver(Graphics g) { paintBackground(g); g.setFont(font_large); g.setColor(ThreeDColor.black.getRGB()); g.drawString("Game", width / 2 + 1, height / 2 - 10 + 1, TH_ANCHOR); g.drawString("Over", width / 2 + 1, height / 2 + 10 + 1, TH_ANCHOR); g.setColor(ThreeDColor.red.getRGB()); g.drawString("Game", width / 2, height / 2 - 10, TH_ANCHOR); g.drawString("Over", width / 2, height / 2 + 10, TH_ANCHOR); } public void paint(Graphics g) { boolean full_repaint = engine.levelStarted(); engine.getState(state); g.setFont(font); if (state.state == Engine.TITLE) { paintTitle(g); } else if (state.state == Engine.OVER) { paintOver(g); } else { if (full_repaint) { paintBackground(GRAPHICS); state.bricks.paintShadow(GRAPHICS); state.bricks.paint(GRAPHICS); } g.drawImage(buf, 0, 0, TL_ANCHOR); if (state.state == Engine.PLAY) { if (state.score != last_score || full_repaint) { g.setColor(BACKGROUND); g.fillRect(width / 3, height - (font_voff + 5), width * 2 / 3, font_voff + 5); g.setColor(ThreeDColor.black.getRGB()); g.drawString("S: " + state.score, width * 1 / 3 + 1, height - (font_voff + 3), TL_ANCHOR); g.drawString("H: " + state.hi_score, width * 2 / 3 + 1, height - (font_voff + 3), TL_ANCHOR); g.setColor(ThreeDColor.orange.getRGB()); g.drawString("S: " + state.score, width * 1 / 3, height - (font_voff + 5), TL_ANCHOR); g.drawString("H: " + state.hi_score, width * 2 / 3, height - (font_voff + 5), TL_ANCHOR); } if (state.lives != last_lives || full_repaint) { g.setColor(BACKGROUND); g.fillRect(0, height - (font_voff + 5), width / 3, font_voff + 5); for (int i = 0; i < state.lives; i++) { g.setColor(ThreeDColor.black.getRGB()); g.fillArc(BrickList.XOFFSET + 1 + 3 * Ball.RADIUS * i, height - (font_voff - 1), Math.max(4, height / 50), Math.max(4, height / 50), 0, 360); g.setColor(ThreeDColor.red.getRGB()); g.fillArc(BrickList.XOFFSET + 3 * Ball.RADIUS * i, height - font_voff, Math.max(4, height / 50), Math.max(4, height / 50), 0, 360); } } } state.ball.paintShadow(g); state.paddle.paintShadow(g); state.ball.paint(g); state.paddle.paint(g); } if (show_fps) { fps++; long n = System.currentTimeMillis() / 1000; if (n != last) { fps_string = "" + fps; fps = 0; last = n; } g.setColor(0xffffff); g.drawString(fps_string, 5, 3, TL_ANCHOR); } last_state = state.state; last_score = state.score; last_lives = state.lives; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -