Use pointer events to draw onto the Canvas : 2D « J2ME « Java






Use pointer events to draw onto the Canvas

Use pointer events to draw onto the Canvas
/*--------------------------------------------------
* Doodle.java
*
* Use pointer events to draw onto the Canvas
*
* 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 Doodle extends MIDlet
{
  private Display  display;       // The display
  private DoodleCanvas canvas;   // Canvas 
 
  public Doodle()
  {
    display = Display.getDisplay(this);
    canvas  = new DoodleCanvas(this);
  }
 
  protected void startApp()
  {
    display.setCurrent( canvas );
  }
 
  protected void pauseApp()
  { }

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

/*--------------------------------------------------
* Class DoodleCanvas
*
* Pointer event handling
*-------------------------------------------------*/
class DoodleCanvas extends Canvas implements CommandListener
{
  private Command cmExit;          // Exit midlet
  
  private Command cmClear;         // Clear display
  
  private int startx = 0,   // Where pointer was clicked
              
              starty = 0,
              
              currentx = 0, // Current location
              
              currenty = 0;
  private Doodle midlet;
  private boolean clearDisplay = false;

  /*--------------------------------------------------
  * Constructor
  *-------------------------------------------------*/
  public DoodleCanvas(Doodle midlet)
  {
    this.midlet = midlet;
    
    // Create exit command & listen for events
    cmExit = new Command("Exit", Command.EXIT, 1);
    cmClear = new Command("Clear", Command.SCREEN, 1);    
    addCommand(cmExit);
    addCommand(cmClear);
    setCommandListener(this);
  } 

  /*--------------------------------------------------
  * Paint the text representing the key code 
  *-------------------------------------------------*/
  protected void paint(Graphics g)
  {
    // Clear the background (to white)
    if (clearDisplay)
    {
      g.setColor(255, 255, 255);
      g.fillRect(0, 0, getWidth(), getHeight());
      
      clearDisplay = false;
      startx = currentx = starty = currenty = 0;
      
      return;
    }
    
    // Draw with black pen
    g.setColor(0, 0, 0);
    
    // Draw line
    g.drawLine(startx, starty, currentx, currenty);
    
    // New starting point is the current position
    startx = currentx;
    starty = currenty;
  }

  /*--------------------------------------------------
  * Command event handling
  *-------------------------------------------------*/  
  public void commandAction(Command c, Displayable d)
  {
    if (c == cmExit)
      midlet.exitMIDlet();
    else if (c == cmClear)
    {
      clearDisplay = true; 
      repaint();
    }
  }

  /*--------------------------------------------------
  * Pointer pressed
  *-------------------------------------------------*/  
  protected void pointerPressed(int x, int y)
  {
    startx = x;
    starty = y;
  }

  /*--------------------------------------------------
  * Pointer moved
  *-------------------------------------------------*/  
  protected void pointerDragged(int x, int y)
  {
    currentx = x;
    currenty = y;       
    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 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.Translate coordinate systemTranslate coordinate system
17.Show various anchor pointsShow various anchor points
18.Draw mutable image on a canvas
19.Canvas for processing key code and commandsCanvas for processing key code and commands
20.Draw immutable image on a canvasDraw immutable image on a canvas
21.A quick sample of graphics, commands, and event handling.A quick sample of graphics,  commands, and event handling.