Rectangles : Shape « J2ME « Java






Rectangles

Rectangles

/*--------------------------------------------------
* Rectangles.java
*
* Draw rectangles on a 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 RectanglesMIDlet extends MIDlet
{
  private Display  display;    // The display
  private RectangleCanvas canvas;   // Canvas 
 
  public Rectangles()
  {
    display = Display.getDisplay(this);
    canvas  = new RectangleCanvas(this);
  }
 
  protected void startApp()
  {
    display.setCurrent( canvas );
  }
 
  protected void pauseApp()
  { }

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

/*--------------------------------------------------
* Class RectangleCanvas
*
* Draw arcs
*-------------------------------------------------*/
class RectangleCanvas extends Canvas implements CommandListener
{
  private Command cmExit;  // Exit midlet
  private Rectangles midlet;

  public RectangleCanvas(Rectangles midlet)
  {
    this.midlet = midlet;
    
    // Create exit command & listen for events
    cmExit = new Command("Exit", Command.EXIT, 1);
    addCommand(cmExit);
    setCommandListener(this);
  } 

  /*--------------------------------------------------
  * Draw an arc 
  *-------------------------------------------------*/
  protected void paint(Graphics g)
  {
    g.drawRect(1, 1, 25, 25);
    g.drawRoundRect(28, 28, 45, 45, 15, 45);
    
//    g.fillRect(1, 1, 25, 25);
//    g.fillRoundRect(28, 28, 45, 45, 15, 45);
    
  }

  public void commandAction(Command c, Displayable d)
  {
    if (c == cmExit)
      midlet.exitMIDlet();
  }
}

           
       








Related examples in the same category

1.Rectangle ExampleRectangle Example
2.Filled Rectangle ExampleFilled Rectangle Example
3.Arc Arc
4.Arc Filled ExampleArc Filled Example
5.Draw arc on a canvasDraw arc on a canvas