📄 imagegetter.java
字号:
package getimagefromnet;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
class ImageGetter extends MIDlet implements CommandListener
{
private Display display;
public static final Command connCommand = new Command("Connect",
Command.ITEM, 1);
public static final Command exitCommand = new Command("Exit", Command.EXIT,
1);
private Form mainForm;
private GetterThread gt;
protected void startApp() throws MIDletStateChangeException
{
display = Display.getDisplay(this);
mainForm = new Form("Image Getter");
mainForm.append("Click Connect to get Image");
mainForm.addCommand(connCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
gt = new GetterThread(this);
gt.start();
}
public void setImage(Image image)
{
mainForm.append(image);
display.setCurrent(mainForm);
}
protected void pauseApp()
{
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
public void commandAction(Command cmd, Displayable disp)
{
if (cmd == connCommand)
{
synchronized (this)
{
notify();
}
} else if (cmd == exitCommand)
{
exitMIDlet();
}
}
private void exitMIDlet()
{
try
{
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException e)
{
e.printStackTrace();
}
}
class GetterThread extends Thread
{
private ImageGetter midlet;
public static final String URL = "http://localhost/j2medev.png";
private HttpConnection httpConn = null;
private InputStream is = null;
public GetterThread(ImageGetter midlet)
{
this.midlet = midlet;
}
public void run()
{
synchronized (midlet)
{
try
{
midlet.wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("connect to server...");
try
{
httpConn = (HttpConnection) Connector.open(URL);
is = httpConn.openInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = 0;
while ((ch = is.read()) != -1)
{
baos.write(ch);
}
byte[] imageData = baos.toByteArray();
Image image = Image.createImage(imageData, 0, imageData.length);
midlet.setImage(image);
baos.close();
is.close();
httpConn.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -