Java by API/javax.microedition.lcdui/Image

Материал из Java эксперт
Перейти к: навигация, поиск

Image: createImage(String name) throws IOException

   <source lang="java">

import java.io.IOException; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.List; import javax.microedition.midlet.MIDlet; public class J2METravelList extends MIDlet implements CommandListener {

 private List mList;
 private Command mExitCommand, mNextCommand;
 public J2METravelList() {
   String[] stringElements = { "A", "C", "H" };
   Image[] imageElements = { loadImage("/a.png"), loadImage("/c.png"),
       loadImage("/h.png") };
   mList = new List("Reservation type", List.IMPLICIT, stringElements, imageElements);
   mNextCommand = new Command("Next", Command.SCREEN, 0);
   mExitCommand = new Command("Exit", Command.EXIT, 0);
   mList.addCommand(mNextCommand);
   mList.addCommand(mExitCommand);
   mList.setCommandListener(this);
 }
 public void startApp() {
   Display.getDisplay(this).setCurrent(mList);
 }
 public void commandAction(Command c, Displayable s) {
   if (c == mNextCommand || c == List.SELECT_COMMAND) {
     int index = mList.getSelectedIndex();
     Alert alert = new Alert("Your selection", "You chose " + mList.getString(index) + ".", null,
         AlertType.INFO);
     Display.getDisplay(this).setCurrent(alert, mList);
   } else if (c == mExitCommand)
     notifyDestroyed();
 }
 public void pauseApp() {
 }
 public void destroyApp(boolean unconditional) {
 }
 private Image loadImage(String name) {
   Image image = null;
   try {
     image = Image.createImage(name);
   } catch (IOException ioe) {
     System.out.println(ioe);
   }
   return image;
 }

}

 </source>
   
  
 
  



Image: getGraphics()

   <source lang="java">

import javax.microedition.lcdui.Alert; 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.lcdui.Image; import javax.microedition.midlet.MIDlet; public class J2MEMutableImageExample extends MIDlet {

 private Display display;
 private MyCanvas canvas;
 public J2MEMutableImageExample() {
   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 J2MEMutableImageExample mutableImageExample;
   private Image image = null;
   public MyCanvas(J2MEMutableImageExample mutableImageExample) {
     this.mutableImageExample = mutableImageExample;
     exit = new Command("Exit", Command.EXIT, 1);
     addCommand(exit);
     setCommandListener(this);
     try {
       image = Image.createImage(70, 70);
       Graphics graphics = image.getGraphics();
       graphics.setColor(255, 0, 0);
       graphics.fillArc(10, 10, 60, 50, 180, 180);
     } catch (Exception error) {
       Alert alert = new Alert("Failure", "Creating Image", null, null);
       alert.setTimeout(Alert.FOREVER);
       display.setCurrent(alert);
     }
   }
   protected void paint(Graphics graphics) {
     if (image != null) {
       graphics.setColor(255, 255, 255);
       graphics.fillRect(0, 0, getWidth(), getHeight());
       graphics.drawImage(image, 30, 30, Graphics.VCENTER | Graphics.HCENTER);
     }
   }
   public void commandAction(Command command, Displayable display) {
     if (command == exit) {
       mutableImageExample.exitMIDlet();
     }
   }
 }

}

 </source>