Java me j2me应用程序中的图像显示

Java me j2me应用程序中的图像显示,java-me,Java Me,如何在j2me应用程序中创建和显示图像 我可以将该图像放在我的应用程序中的哪个文件夹中?正是您想要开始的 基本上,要创建图像,需要调用image.createImage() 如果它位于Jar中的子文件夹中: Image img = Image.createImage("/subDir/imageName.png"); 要显示图像,需要通过绑定到画布的图形实例将其绘制到画布上(在上面的链接中可以更好地可视化) 您也可以使用Graphics.drawRegion函数,但可以使用JavaDocs f

如何在j2me应用程序中创建和显示图像

我可以将该图像放在我的应用程序中的哪个文件夹中?

正是您想要开始的

基本上,要创建图像,需要调用image.createImage()

如果它位于Jar中的子文件夹中:

Image img = Image.createImage("/subDir/imageName.png");
要显示图像,需要通过绑定到画布的图形实例将其绘制到画布上(在上面的链接中可以更好地可视化)

您也可以使用Graphics.drawRegion函数,但可以使用JavaDocs for J2ME,以便您查看最适合您需要的功能。

正好提供了入门所需的功能

基本上,要创建图像,需要调用image.createImage()

如果它位于Jar中的子文件夹中:

Image img = Image.createImage("/subDir/imageName.png");
要显示图像,需要通过绑定到画布的图形实例将其绘制到画布上(在上面的链接中可以更好地可视化)


您也可以使用Graphics.drawRegion函数,但可以使用JavaDocs for J2ME,以便查看最适合您的需要。

要在JavaME MIDlet上绘制图像,您需要一块画布来绘制图像。您可以执行以下操作: 首先,您必须将原始图像文件放在包中(通常放在“res”或其子目录中)。 其次,您需要创建一个扩展画布的类并实现paint方法:

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class MyCanvas extends Canvas {
private Image image;
public MyCanvas(){
    try {
        image = Image.createImage("picture.png");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

protected void paint(Graphics g) {
    g.drawImage(image, 10, 10, Graphics.TOP | Graphics.LEFT);
}
}
现在,您需要创建此类的实例并告诉MIDlet di显示它,例如:

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MyMIDlet extends MIDlet {
public MyMIDlet(){
}

protected void destroyApp(boolean unconditional)
        throws MIDletStateChangeException {
}

protected void pauseApp() {
}

protected void startApp() throws MIDletStateChangeException {
    Display.getDisplay(this).setCurrent(new MyCanvas());
}

}

请记住,这样画布将只绘制一次,如果您更改了某些内容,则需要调用repaint()方法。

要在JavaME MIDlet上绘制图像,您需要一个画布来绘制图像。您可以执行以下操作: 首先,您必须将原始图像文件放在包中(通常放在“res”或其子目录中)。 其次,您需要创建一个扩展画布的类并实现paint方法:

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class MyCanvas extends Canvas {
private Image image;
public MyCanvas(){
    try {
        image = Image.createImage("picture.png");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

protected void paint(Graphics g) {
    g.drawImage(image, 10, 10, Graphics.TOP | Graphics.LEFT);
}
}
现在,您需要创建此类的实例并告诉MIDlet di显示它,例如:

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MyMIDlet extends MIDlet {
public MyMIDlet(){
}

protected void destroyApp(boolean unconditional)
        throws MIDletStateChangeException {
}

protected void pauseApp() {
}

protected void startApp() throws MIDletStateChangeException {
    Display.getDisplay(this).setCurrent(new MyCanvas());
}

}

请记住,这样画布将只绘制一次,如果您更改了某些内容,则需要调用repaint()方法。

此源代码基于以前发布的注释:

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class ImageLoader extends MIDlet
    implements CommandListener, Runnable {
  private Display mDisplay;
  private Form mForm;
  public ImageLoader() {
    mForm = new Form("Connecting...");
    mForm.addCommand(new Command("Exit", Command.EXIT, 0));
    mForm.setCommandListener(this);
  }
  public void startApp() {
    if (mDisplay == null) mDisplay = Display.getDisplay(this);
    mDisplay.setCurrent(mForm);     
    Thread t = new Thread(this);
    t.start();
  }
  public void pauseApp() {}
  public void destroyApp(boolean unconditional) {}
  public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT)
      notifyDestroyed();
  }
  public void run() {
    FileConnection fc = null;
    DataInputStream in = null;
    DataOutputStream out = null;    
    try {
      fc = (FileConnection)Connector.open("file:///root1/i.PNG");
      int length = (int)fc.fileSize();//possible loss of precision may throw error
      byte[] data = null;
      if (length != -1) {
        data = new byte[length];
        in = new DataInputStream(fc.openInputStream());
        in.readFully(data);
      }
      else {
        int chunkSize = 512;
        int index = 0;
        int readLength = 0;
        in = new DataInputStream(fc.openInputStream());
        data = new byte[chunkSize];
        do {
          if (data.length < index + chunkSize) {
            byte[] newData = new byte[index + chunkSize];
            System.arraycopy(data, 0, newData, 0, data.length);
            data = newData;
          }
          readLength = in.read(data, index, chunkSize);
          index += readLength;
        } while (readLength == chunkSize);
        length = index;
      }
      Image image = Image.createImage(data, 0, length);
      ImageItem imageItem = new ImageItem(null, image, 0, null);
      mForm.append(imageItem);
      mForm.setTitle("Done.");
      fc = (FileConnection)Connector.open("file:///root1/x.PNG");
      if(!fc.exists()){
          try{
          fc.create();
          }catch(Exception ce){System.out.print("Create Error: " + ce);}
      }
      out = new DataOutputStream(fc.openOutputStream());
      out.write(data);
    }
    catch (IOException ioe) {
      StringItem stringItem = new StringItem(null, ioe.toString());
      mForm.append(stringItem);
      mForm.setTitle("Done.");
    }
    finally {
      try {
        if (in != null) in.close();
        if (fc != null) fc.close();
      }
      catch (IOException ioe) {}
    }
  }
}
import java.io.*;
导入javax.microedition.io.*;
导入javax.microedition.io.file.FileConnection;
导入javax.microedition.lcdui.*;
导入javax.microedition.midlet.*;
公共类ImageLoader扩展了MIDlet
实现CommandListener,可运行{
专用显示器;
私人表格;
公共图像加载器(){
mForm=新形式(“连接…”);
mForm.addCommand(新命令(“Exit”,Command.Exit,0));
mForm.setCommandListener(此);
}
公开作废startApp(){
如果(mDisplay==null)mDisplay=Display.getDisplay(this);
mDisplay.setCurrent(mForm);
螺纹t=新螺纹(此);
t、 start();
}
public void pauseApp(){}
公共应用程序(布尔无条件){}
公共无效命令操作(命令c,可显示){
if(c.getCommandType()==Command.EXIT)
通知销毁();
}
公开募捐{
FileConnection fc=null;
DataInputStream in=null;
DataOutputStream out=null;
试一试{
fc=(文件连接)连接器。打开(“file:///root1/i.PNG");
int length=(int)fc.fileSize();//可能的精度损失可能引发错误
字节[]数据=null;
如果(长度!=-1){
数据=新字节[长度];
in=新的DataInputStream(fc.openInputStream());
in.readFully(数据);
}
否则{
int chunkSize=512;
int指数=0;
int readLength=0;
in=新的DataInputStream(fc.openInputStream());
数据=新字节[chunkSize];
做{
if(data.length
代码是从这里提供的链接Fostah修改的


它打开一个图像,显示它,然后使用FileConnection将其保存为x.PNG而不是i.PNG。需要注意的棘手问题是文件的保存/加载位置。如果您将J2meWTK与Netbeans一起使用,则在运行移动应用程序时,该文件夹将显示在输出窗口中。该文件夹类似于temp.DefaultColorPhone/filesystem/root1。这就是你必须有一个形象的地方。我不确定默认情况下如何使用映像创建临时环境。这意味着您必须启动移动应用程序,在IDE中检查temp root1/的位置,然后将图像放入文件夹,然后继续运行ImageLoader应用程序。我将通过发布一个问题来尝试找出如何实现自动化。另外,从一个小图像50x50开始(较大的图像可能会导致问题)。

此源代码基于以前发布的注释:

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class ImageLoader extends MIDlet
    implements CommandListener, Runnable {
  private Display mDisplay;
  private Form mForm;
  public ImageLoader() {
    mForm = new Form("Connecting...");
    mForm.addCommand(new Command("Exit", Command.EXIT, 0));
    mForm.setCommandListener(this);
  }
  public void startApp() {
    if (mDisplay == null) mDisplay = Display.getDisplay(this);
    mDisplay.setCurrent(mForm);     
    Thread t = new Thread(this);
    t.start();
  }
  public void pauseApp() {}
  public void destroyApp(boolean unconditional) {}
  public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT)
      notifyDestroyed();
  }
  public void run() {
    FileConnection fc = null;
    DataInputStream in = null;
    DataOutputStream out = null;    
    try {
      fc = (FileConnection)Connector.open("file:///root1/i.PNG");
      int length = (int)fc.fileSize();//possible loss of precision may throw error
      byte[] data = null;
      if (length != -1) {
        data = new byte[length];
        in = new DataInputStream(fc.openInputStream());
        in.readFully(data);
      }
      else {
        int chunkSize = 512;
        int index = 0;
        int readLength = 0;
        in = new DataInputStream(fc.openInputStream());
        data = new byte[chunkSize];
        do {
          if (data.length < index + chunkSize) {
            byte[] newData = new byte[index + chunkSize];
            System.arraycopy(data, 0, newData, 0, data.length);
            data = newData;
          }
          readLength = in.read(data, index, chunkSize);
          index += readLength;
        } while (readLength == chunkSize);
        length = index;
      }
      Image image = Image.createImage(data, 0, length);
      ImageItem imageItem = new ImageItem(null, image, 0, null);
      mForm.append(imageItem);
      mForm.setTitle("Done.");
      fc = (FileConnection)Connector.open("file:///root1/x.PNG");
      if(!fc.exists()){
          try{
          fc.create();
          }catch(Exception ce){System.out.print("Create Error: " + ce);}
      }
      out = new DataOutputStream(fc.openOutputStream());
      out.write(data);
    }
    catch (IOException ioe) {
      StringItem stringItem = new StringItem(null, ioe.toString());
      mForm.append(stringItem);
      mForm.setTitle("Done.");
    }
    finally {
      try {
        if (in != null) in.close();
        if (fc != null) fc.close();
      }
      catch (IOException ioe) {}
    }
  }
}
import java.io.*;
导入javax.microedition