Display big Image : Image « J2ME « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » J2ME » Image 
31.35.5.Display big ImagePrevious/Next
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(00, width - 1, height - 1);
      g.setGrayScale(0);
      g.drawRect(00, width - 1, height - 1);

      g.translate(transX, transY);
      g.drawImage(img, 00, 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.