Java Tutorial/J2ME/Image

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

Display big Image

   <source lang="java">

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; import javax.microedition.midlet.MIDletStateChangeException; public class DisplayBigImageTest extends MIDlet implements CommandListener {

 private Display display;
 private TestCanvas tc;
 private Command leftCommand = new Command("Left", Command.SCREEN, 1);
 private Command rightCommand = new Command("Right", Command.SCREEN, 1);
 public DisplayBigImageTest() {
   Image img = null;
   try {
     img = Image.createImage("/10.png");
   } catch (Exception e) {
     System.out.println(e.getMessage());
   }
   tc = new TestCanvas(img);
   tc.addCommand(rightCommand);
   tc.addCommand(leftCommand);
   tc.setCommandListener(this);
   display = Display.getDisplay(this);
 }
 public void startApp() throws MIDletStateChangeException {
   display.setCurrent(tc);
 }
 public void pauseApp() {
 }
 public void destroyApp(boolean unconditional) {
 }
 public void commandAction(Command c, Displayable d) {
   int stepX = tc.getWidth() / 4;
   int stepY = tc.getHeight() / 4;
   if (d == tc && c == leftCommand) {
     tc.increaseXY(stepX, 0);
     tc.repaint();
   } else if (d == tc && c == rightCommand) {
     tc.increaseXY(-stepX, 0);
     tc.repaint();
   }
 }
 class TestCanvas extends Canvas {
   private Image img;
   private int transX = 0;
   private int transY = 0;
   public TestCanvas(Image img) {
     this.img = img;
     transX = 0;
     transY = 0;
   }
   public void increaseXY(int x, int y) {
     transX += x;
     transY += y;
   }
   public void paint(Graphics g) {
     int width = this.getWidth();
     int height = this.getHeight();
     g.setGrayScale(255);
     g.fillRect(0, 0, width - 1, height - 1);
     g.setGrayScale(0);
     g.drawRect(0, 0, width - 1, height - 1);
     g.translate(transX, transY);
     g.drawImage(img, 0, 0, g.TOP | g.LEFT);
   }
 }

}</source>





Draw image

   <source lang="java">

import java.io.IOException; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.midlet.MIDlet; public class DrawImageGraphicsMIDlet extends MIDlet {

 private Display display;
 protected void startApp() {
   Canvas canvas = new LineCanvas();
   display = Display.getDisplay(this);
   display.setCurrent(canvas);
 }
 protected void pauseApp() {
 }
 protected void destroyApp(boolean unconditional) {
 }

} class LineCanvas extends Canvas {

 public void paint(Graphics g) {
   int width = getWidth();
   int height = getHeight();
   try {
     Image image = Image.createImage("/h.png");
     g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
   } catch (IOException ex) {
     g.setColor(0xffffff);
     g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
     return;
   }
 }

}</source>





Draw image to the bottom and right

   <source lang="java">

import java.io.IOException; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.midlet.MIDlet; public class ImageBottomRightGraphicsMIDlet extends MIDlet {

 private Display display;
 protected void startApp() {
   Canvas canvas = new LineCanvas();
   display = Display.getDisplay(this);
   display.setCurrent(canvas);
 }
 protected void pauseApp() {
 }
 protected void destroyApp(boolean unconditional) {
 }

} class LineCanvas extends Canvas {

 public void paint(Graphics g) {
   int width = getWidth();
   int height = getHeight();
   try {
     Image image = Image.createImage("/h.png");
     g.drawImage(image, width, height, Graphics.BOTTOM | Graphics.RIGHT);
   } catch (IOException ex) {
     g.setColor(0xffffff);
     g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
     return;
   }
 }

}</source>





Draw image to the center vertically and horizontally

   <source lang="java">

import java.io.IOException; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.midlet.MIDlet; public class ImageVerticalHorizontalGraphicsMIDlet extends MIDlet {

 private Display display;
 protected void startApp() {
   Canvas canvas = new LineCanvas();
   display = Display.getDisplay(this);
   display.setCurrent(canvas);
 }
 protected void pauseApp() {
 }
 protected void destroyApp(boolean unconditional) {
 }

} class LineCanvas extends Canvas {

 public void paint(Graphics g) {
   int width = getWidth();
   int height = getHeight();
   try {
     Image image = Image.createImage("/h.png");
     g.drawImage(image, width/2, height/2, Graphics.VCENTER | Graphics.HCENTER);
   } catch (IOException ex) {
     g.setColor(0xffffff);
     g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
     return;
   }
 }

}</source>





Draw image with rectangle

   <source lang="java">

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 DrawImageMIDlet extends MIDlet implements CommandListener {

 private Command exitCommand;
 Display display;
 Image image = null;
 public void startApp() {
   Display display = Display.getDisplay(this);
   try {
     image = Image.createImage("/0.png");
   } catch (Exception e) {
   }
   Displayable d = new DrawImageCanvas(image);
   exitCommand = new Command("Exit", Command.EXIT, 1);
   d.addCommand(exitCommand);
   d.setCommandListener(this);
   display.setCurrent(d);
 }
 public void pauseApp() {
 }
 public void destroyApp(boolean unconditional) {
 }
 public void commandAction(Command c, Displayable s) {
   notifyDestroyed();
 }

} class DrawImageCanvas extends Canvas {

 int width = 0;
 int height = 0;
 Image image = null;
 DrawImageCanvas(Image image) {
   this.image = image;
 }
 public void paint(Graphics g) {
   width = getWidth();
   height = getHeight();
   g.setGrayScale(255);
   g.fillRect(0, 0, width - 1, height - 1);
   g.setGrayScale(0);
   g.drawRect(0, 0, width - 1, height - 1);
   g.drawImage(image, width / 2, height / 2, g.HCENTER | g.VCENTER);
 }

}</source>





Image loading exception

   <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 J2MEImmutableImageExample extends MIDlet {

 private Display display;
 private MyCanvas canvas;
 public J2MEImmutableImageExample() {
   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 J2MEImmutableImageExample immutableImageExample;
   private Image image = null;
   public MyCanvas(J2MEImmutableImageExample immutableImageExample) {
     this.immutableImageExample = immutableImageExample;
     exit = new Command("Exit", Command.EXIT, 1);
     addCommand(exit);
     setCommandListener(this);
     try {
       image = Image.createImage("/myImage.png");
     } catch (Exception error) {
       Alert alert = new Alert("Failure", "Can�t open image file.", null, null);
       alert.setTimeout(Alert.FOREVER);
       display.setCurrent(alert);
     }
   }
   protected void paint(Graphics graphics) {
     if (image != null) {
       graphics.drawImage(image, 0, 0, Graphics.VCENTER | Graphics.HCENTER);
     }
   }
   public void commandAction(Command command, Displayable display) {
     if (command == exit) {
       immutableImageExample.exitMIDlet();
     }
   }
 }

}</source>





Immutable image

   <source lang="java">

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.Form; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.ImageItem; import javax.microedition.midlet.MIDlet; public class J2MEImmutableImage extends MIDlet implements CommandListener {

 private Display display;
 private Form form;
 private Command exit;
 private Image image;
 private ImageItem imageItem;
 public J2MEImmutableImage() {
   display = Display.getDisplay(this);
   exit = new Command("Exit", Command.EXIT, 1);
   form = new Form("Immutable Image Example");
   form.addCommand(exit);
   form.setCommandListener(this);
   try {
     image = Image.createImage("myimage.png");
     imageItem = new ImageItem(null, image, ImageItem.LAYOUT_NEWLINE_BEFORE
         | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_NEWLINE_AFTER, "My Image");
     form.append(imageItem);
   } catch (java.io.IOException error) {
     Alert alert = new Alert("Error", "Cannot load myimage.png.", null, null);
     alert.setTimeout(Alert.FOREVER);
     alert.setType(AlertType.ERROR);
     display.setCurrent(alert);
   }
 }
 public void startApp() {
   display.setCurrent(form);
 }
 public void pauseApp() {
 }
 public void destroyApp(boolean unconditional) {
 }
 public void commandAction(Command command, Displayable Displayable) {
   if (command == exit) {
     destroyApp(false);
     notifyDestroyed();
   }
 }

}</source>





Mutable Image

   <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>