Display big Image : Image « J2ME « Java Tutorial






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.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);
    }
  }
}








31.35.Image
31.35.1.Draw image
31.35.2.Draw image to the bottom and right
31.35.3.Draw image to the center vertically and horizontally
31.35.4.Draw image with rectangle
31.35.5.Display big Image
31.35.6.Immutable image
31.35.7.Mutable ImageMutable Image
31.35.8.Image loading exception