Translate coordinate system : 2D « 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 » 2DScreenshots 
Translate coordinate system
Translate coordinate system


/*--------------------------------------------------
* Translate.java
*
* Translate coordinate system
*
* Example from the book:     Core J2ME Technology
* Copyright John W. Muchow   http://www.CoreJ2ME.com
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/  
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Translate extends MIDlet
{
  private Display  display;     // The display
  private TranslateCanvas canvas;   // Canvas 
 
  public Translate()
  {
    display = Display.getDisplay(this);
    canvas  = new TranslateCanvas(this);
  }
 
  protected void startApp()
  {
    display.setCurrentcanvas );
  }
 
  protected void pauseApp()
  { }

  protected void destroyAppboolean unconditional )
  { }
 
  public void exitMIDlet()
  {
    destroyApp(true);
    notifyDestroyed();
  }
}

/*--------------------------------------------------
* Class Translate
*
* Draw image using translated coordinates
*-------------------------------------------------*/
class TranslateCanvas extends Canvas implements CommandListener
{
  private Command cmExit;  // Exit midlet
  private Translate midlet;
  private Image im = null;
  private int translatex = 0, translatey = 0;
 
  public TranslateCanvas(Translate midlet)
  {
    this.midlet = midlet;
    
    // Create exit command & listen for events
    cmExit = new Command("Exit", Command.EXIT, 1);
    addCommand(cmExit);
    setCommandListener(this);

    try
    {
      // Create immutable image
      im = Image.createImage("/bolt.png");
    }
    catch (java.io.IOException e)
    {
      System.err.println("Unable to locate or read .png file");
    }    
  

  protected void paint(Graphics g)
  {
    if (im != null)
    {
      // Clear the background
      g.setColor(255255255);
      g.fillRect(00, getWidth(), getHeight());
      
      // Translate coordinates
      g.translate(translatex, translatey);      
    
      // Always draw at 0,0
      g.drawImage(im, 00, Graphics.LEFT | Graphics.TOP);
    }
  }

  public void commandAction(Command c, Displayable d)
  {
    if (c == cmExit)
      midlet.exitMIDlet();
  }
  
  protected void keyPressed(int keyCode)
  {
    switch (getGameAction(keyCode))
    {
      case UP: 
      
        // If scrolling off the top, roll around to bottom
        if (translatey - im.getHeight() 0)
          translatey = getHeight() - im.getHeight();
        else
          translatey -= im.getHeight();          
        break;
      
      case DOWN:

        // If scrolling off the bottom, roll around to top
        if ((translatey + im.getHeight() + im.getHeight()) > getHeight())
          translatey = 0;
        else
          translatey += im.getHeight();          
        break;
        
      case LEFT:
      
        // If scrolling off the left, bring around to right
        if (translatex - im.getWidth() 0)
          translatex = getWidth() - im.getWidth();
        else
          translatex -= im.getWidth();          
        break;

      case RIGHT:
      
        // If scrolling off the right, bring around to left
        if ((translatex + im.getWidth() + translatex> getWidth())
          translatex = 0;
        else
          translatex += im.getWidth();          
        break;
    }        
    repaint();
  }
}

           
       
Related examples in the same category
1. Simple Midlet DemoSimple Midlet Demo
2. Piano MIDletPiano MIDlet
3. PacerPacer
4. Pointer ExamplePointer Example
5. Text Example
6. Simple CanvasSimple Canvas
7. Illustrate Graphics MIDletIllustrate Graphics MIDlet
8. Translate CoordinatesTranslate Coordinates
9. Key Canvas
10. Box Text CanvasBox Text Canvas
11. Offscreen MIDletOffscreen MIDlet
12. Quatsch MIDletQuatsch MIDlet
13. Sweep GameSweep Game
14. SweepSweep
15. Text MIDletText MIDlet
16. Show various anchor pointsShow various anchor points
17. Draw mutable image on a canvas
18. Canvas for processing key code and commandsCanvas for processing key code and commands
19. Draw immutable image on a canvasDraw immutable image on a canvas
20. Use pointer events to draw onto the CanvasUse pointer events to draw onto the Canvas
21. A quick sample of graphics, commands, and event handling.A quick sample of graphics,  commands, and event handling.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.