Game Key Event : Key Event « J2ME « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » J2ME » Key EventScreenshots 
Game Key Event
Game Key Event


/*
J2ME in a Nutshell
By Kim Topley
ISBN: 0-596-00253-X

*/


import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

public class EventsMIDlet extends MIDlet 
                        implements CommandListener {

    // The MIDlet's Display object
    private Display display;
        
    // Flag indicating first call of startApp
    protected boolean started;
    
    // Exit command
    private Command exitCommand;
    
    protected void startApp() {
        if (!started) {
            display = Display.getDisplay(this);
            Canvas canvas = new EventsCanvas();            
            exitCommand = new Command("Exit", Command.EXIT, 0);
            canvas.addCommand(exitCommand);
            canvas.setCommandListener(this);
            display.setCurrent(canvas);
            
            started = true;
        }
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            // Exit. No need to call destroyApp
            // because it is empty.
            notifyDestroyed();
        }
    }     
}

class EventsCanvas extends Canvas {
    
    static int[] keyCodes = {KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4,
                             KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9,
                             KEY_POUND, KEY_STAR};
    static String[] keyNames = {"KEY_NUM0""KEY_NUM1""KEY_NUM2""KEY_NUM3""KEY_NUM4",
                             "KEY_NUM5""KEY_NUM6""KEY_NUM7""KEY_NUM8""KEY_NUM9",
                             "KEY_POUND""KEY_STAR"};
                             
    static int[] gameActions = {
                            UP, DOWN, LEFT, RIGHT, FIRE,
                            GAME_A, GAME_B, GAME_C, GAME_D};
    static String[] gameNames = {
                            "UP""DOWN""LEFT""RIGHT""FIRE",
                            "GAME_A""GAME_B""GAME_C""GAME_D" };

    
    int lastKeyCode = 0;
    
    int lastX;
    
    int lastY;
    
    boolean pointer;

    protected void keyPressed(int keyCode) {
        lastKeyCode = keyCode;
        repaint();
    }
        
    protected void keyRepeated(int keyCode) {
        lastKeyCode = keyCode;
        repaint();
    }
        
    protected void keyReleased(int keyCode) {
        lastKeyCode = 0;
        repaint();
    }    

    protected void pointerPressed(int x, int y) {
        lastX = x;
        lastY = y;
        pointer = true;
        repaint();
    }
        
    protected void pointerDragged(int x, int y) {
        lastX = x;
        lastY = y;
        pointer = true;
        repaint();
    }
    
    protected void pointerReleased(int x, int y) {
        pointer = false;
        repaint();
    }
    
    protected void paint(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(00, getWidth(), getHeight());
        
        g.setColor(0);
        if (lastKeyCode != 0) {
            String keyText = "keyCode " + lastKeyCode;
            String keyName = null;

            // See if it is a standard key
            for (int i = 0; i < keyCodes.length; i++) {
                if (lastKeyCode == keyCodes[i]) {
                    keyName = keyNames[i];
                    break;
                }
            }   
            
            if (keyName == null) {
                // See if it is a game action
                for (int i = 0; i < gameActions.length; i++) {
                    if (lastKeyCode == getKeyCode(gameActions[i])) {
                        keyName = gameNames[i];
                        break;
                    }
                }
            }
            
            g.drawString(keyText, getWidth()/2, getHeight()/2
                            Graphics.BASELINE|Graphics.HCENTER);
                    
            if (keyName != null) {
                g.drawString(keyName, getWidth()/2, getHeight()/+ g.getFont().getHeight()
                            Graphics.BASELINE|Graphics.HCENTER);    
            }
        else if (pointer) {
            g.drawString("(" + lastX + ", " + lastY + ")", getWidth()/2, getHeight()/2
                            Graphics.BASELINE|Graphics.HCENTER);
        }
    }
}

           
       
Related examples in the same category
1. Key Event DemoKey Event Demo
2. Low-Level Display Canvas:Key Code Example Low-Level Display Canvas:Key Code Example
3. Event Example 1Event Example 1
4. Event Example 2Event Example 2
5. Key MIDletKey MIDlet
6. Key Event with CanvasKey Event with Canvas
7. Canvas for processing key code and commandsCanvas for processing key code and commands
8. Canvas for processing game actionsCanvas for processing game actions
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.