Java/J2ME/Shape

Материал из Java эксперт
Версия от 06:38, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Arc

//jad file (please verify the jar size)
/*
MIDlet-Name: ArcExampleMIDlet
MIDlet-Version: 1.0
MIDlet-Vendor: MyCompany
MIDlet-Jar-URL: ArcExampleMIDlet.jar
MIDlet-1: ArcExampleMIDlet, , ArcExampleMIDlet
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDlet-JAR-SIZE: 100
*/
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class ArcExampleMIDlet extends MIDlet {
  private Display display;
  private MyCanvas canvas;
  public ArcExampleMIDlet() {
    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 ArcExampleMIDlet arcExample;
  public MyCanvas(ArcExampleMIDlet arcExample) {
    this.arcExample = arcExample;
    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(255, 0, 0);
    graphics.drawArc(0, 0, getWidth(), getHeight(), 180, 180);
  }
  public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
      arcExample.exitMIDlet();
    }
  }
}





Arc Filled Example

//jad file (please verify the jar size)
/*
MIDlet-Name: ArcFilledExample
MIDlet-Version: 1.0
MIDlet-Vendor: MyCompany
MIDlet-Jar-URL: ArcFilledExample.jar
MIDlet-1: ArcFilledExample, , ArcFilledExample
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDlet-JAR-SIZE: 100
*/
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class ArcFilledExample extends MIDlet {
  private Display display;
  private MyCanvas canvas;
  public ArcFilledExample() {
    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 ArcFilledExample arcFilledExample;
  public MyCanvas(ArcFilledExample arcFilledExample) {
    this.arcFilledExample = arcFilledExample;
    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(255, 0, 0);
    graphics.fillArc(0, 0, getWidth(), getHeight(), 180, 180);
  }
  public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
      arcFilledExample.exitMIDlet();
    }
  }
}





Draw arc on a canvas

/*--------------------------------------------------
* Arcs.java
*
* Draw arc on a canvas
*
* Example from the book:     Core J2ME Technology
* Copyright John W. Muchow   http://www.CoreJ2ME.ru
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/  
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Arcs extends MIDlet
{
  private Display  display;    // The display
  private ArcsCanvas canvas;   // Canvas 
 
  public Arcs()
  {
    display = Display.getDisplay(this);
    canvas  = new ArcsCanvas(this);
  }
 
  protected void startApp()
  {
    display.setCurrent( canvas );
  }
 
  protected void pauseApp()
  { }
  protected void destroyApp( boolean unconditional )
  { }
 
  public void exitMIDlet()
  {
    destroyApp(true);
    notifyDestroyed();
  }
}
/*--------------------------------------------------
* Class ArcsCanvas
*
* Draw arcs
*-------------------------------------------------*/
class ArcsCanvas extends Canvas implements CommandListener
{
  private Command cmExit;  // Exit midlet
  private Arcs midlet;
 
  public ArcsCanvas(Arcs 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)
  {
    // Start at 3 o"clock and rotate 225 degrees
    g.drawArc(5, 5, 75, 75, 0, 225);
//    g.fillArc(5, 5, 75, 75, 0, 225);
    
    // Start at 12 o"clock and rotate 225 degrees    
//    g.drawArc(5, 5, 75, 75, 90, 225);    
    // Change the size of the bounding box
    // Start at 12 o"clock and rotate 225 degrees
//    g.drawArc(25, 35, 30, 50, 90, 225);    
  }
  public void commandAction(Command c, Displayable d)
  {
    if (c == cmExit)
      midlet.exitMIDlet();
  }
}





Filled Rectangle Example

//jad file (please verify the jar size)
/*
MIDlet-Name: FilledRectangleExample
MIDlet-Version: 1.0
MIDlet-Vendor: MyCompany
MIDlet-Jar-URL: FilledRectangleExample.jar
MIDlet-1: FilledRectangleExample, , FilledRectangleExample
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDlet-JAR-SIZE: 100
*/
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class FilledRectangleExample extends MIDlet {
  private Display display;
  private MyCanvas canvas;
  public FilledRectangleExample() {
    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 FilledRectangleExample filledRectangleExample;
  public MyCanvas(FilledRectangleExample 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();
    }
  }
}





Rectangle Example

//jad file (please verify the jar size)
/*
MIDlet-Name: RectangleExample
MIDlet-Version: 1.0
MIDlet-Vendor: MyCompany
MIDlet-Jar-URL: RectangleExample.jar
MIDlet-1: RectangleExample, , RectangleExample
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDlet-JAR-SIZE: 100
*/
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class RectangleExample extends MIDlet {
  private Display display;
  private MyCanvas canvas;
  public RectangleExample() {
    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 = new Command("Exit", Command.EXIT, 1);
  private RectangleExample rectangleExample;
  public MyCanvas(RectangleExample rectangleExample) {
    this.rectangleExample = rectangleExample;
    addCommand(exit);
    setCommandListener(this);
  }
  protected void paint(Graphics graphics) {
    graphics.setColor(255, 255, 255);
    graphics.fillRect(0, 0, getWidth(), getHeight());
    graphics.setColor(255, 0, 0);
    graphics.drawRect(2, 2, 20, 20);
    graphics.drawRoundRect(20, 20, 60, 60, 15, 45);
  }
  public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
      rectangleExample.exitMIDlet();
    }
  }
}





Rectangles

/*--------------------------------------------------
* Rectangles.java
*
* Draw rectangles on a canvas
*
* Example from the book:     Core J2ME Technology
* Copyright John W. Muchow   http://www.CoreJ2ME.ru
* 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();
  }
}