Source code: com/siemens/mp/color_game/GameCanvas.java
1 /*
2 * Siemens API for MicroEmulator
3 * Copyright (C) 2003 Markus Heberling <markus@heberling.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 package com.siemens.mp.color_game;
20
21 import javax.microedition.lcdui.*;
22
23 public abstract class GameCanvas extends Canvas {
24 public static final int UP_PRESSED = 1 << Canvas.UP;
25 public static final int DOWN_PRESSED = 1 << Canvas.DOWN;
26 public static final int LEFT_PRESSED = 1 << Canvas.LEFT;
27 public static final int RIGHT_PRESSED = 1 << Canvas.RIGHT;
28 public static final int FIRE_PRESSED = 1 << Canvas.FIRE;
29 public static final int GAME_A_PRESSED = 1 << Canvas.GAME_A;
30 public static final int GAME_B_PRESSED = 1 << Canvas.GAME_B;
31 public static final int GAME_C_PRESSED = 1 << Canvas.GAME_C;
32 public static final int GAME_D_PRESSED = 1 << Canvas.GAME_D;
33 private Image offscreen_buffer;
34
35 private int keyMask;
36
37
38 protected GameCanvas(boolean suppressKeyEvents) {
39 offscreen_buffer =
40 Image.createImage(getWidth(), getHeight());
41 }
42
43
44
45 protected Graphics getGraphics() {
46 return offscreen_buffer.getGraphics();
47 }
48
49
50 public int getKeyStates() {
51 int ret=keyMask;
52 keyMask=0;
53 return ret;
54 }
55
56
57 public void paint(Graphics g) {
58 g.drawImage(offscreen_buffer, 0, 0, Graphics.TOP|Graphics.LEFT);
59 }
60
61 public void flushGraphics(int x, int y, int width, int height) {
62
63 repaint(x,y,width,height);
64 }
65
66
67 public void flushGraphics() {
68 repaint();
69 }
70
71
72 protected void keyPressed(int keyCode) {
73 //super.keyPressed(keyCode);
74 keyMask=keyMask|(1<<getGameAction(keyCode));
75 }
76
77 /**
78 * Called when a key is released.
79 */
80 protected void keyReleased(int keyCode) {
81 //super.keyReleased(keyCode);
82 }
83
84 /**
85 * Called when a key is repeated (held down).
86 */
87 protected void keyRepeated(int keyCode) {
88 //super.keyRepeated(keyCode);
89 }
90
91 protected void hideNotify() {
92 keyMask=0;
93 }
94 }