Fill a rectangle, and a round rectangle : Rectangle « J2ME « Java Tutorial






Fill a rectangle, and a round rectangle
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 J2MEFilledRectangleExample extends MIDlet {
  private Display display;

  private MyCanvas canvas;

  public J2MEFilledRectangleExample() {
    display = Display.getDisplay(this);
    canvas = new MyCanvas(this);
  }

  protected void startApp() {
    display.setCurrent(canvas);
  }

  protected void pauseApp() {
  }

  protected void destroyApp(boolean unconditional) {
  }

  public void exitMIDlet() {
    destroyApp(true);
    notifyDestroyed();
  }
}

class MyCanvas extends Canvas implements CommandListener {
  private Command exit;

  private J2MEFilledRectangleExample filledRectangleExample;

  public MyCanvas(J2MEFilledRectangleExample filledRectangleExample) {
    this.filledRectangleExample = filledRectangleExample;
    exit = new Command("Exit", Command.EXIT, 1);
    addCommand(exit);
    setCommandListener(this);
  }

  protected void paint(Graphics graphics) {
    graphics.setColor(255, 255, 255);
    graphics.fillRect(0, 0, getWidth(), getHeight());
    graphics.setColor(0, 0, 255);
    graphics.fillRect(2, 2, 20, 20);
    graphics.fillRoundRect(20, 20, 60, 60, 15, 45);
  }

  public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
      filledRectangleExample.exitMIDlet();
    }
  }
}








31.33.Rectangle
31.33.1.Fill rectangleFill rectangle
31.33.2.Draw rectangleDraw rectangle
31.33.3.Fill Round RectangleFill Round Rectangle
31.33.4.Fill a rectangle, and a round rectangleFill a rectangle, and a round rectangle