📄 downloadmidlet.java
字号:
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class DownloadMIDlet extends MIDlet implements CommandListener, Runnable{
Display display;
Command exitCmd;
Command downloadCmd;
Command backCmd;
Command cancelCmd;
TextBox inputURL;
Form show;
MyCanvas myCanvas;
String defaultURL = "http://127.0.0.1:8080/JavaPowered-8.png";
int dataInc = 0;
int totalDataLength = 100;
boolean isFinished = false;
Thread connectThread;
Image image;
public DownloadMIDlet(){
display = Display.getDisplay(this);
inputURL = new TextBox("输入URL", defaultURL, 100, TextField.URL);
show = new Form("下载图形");
myCanvas = new MyCanvas();
exitCmd = new Command("退出", Command.EXIT, 1);
downloadCmd = new Command("下载", Command.SCREEN, 1);
backCmd = new Command("返回", Command.BACK, 1);
cancelCmd = new Command("取消", Command.CANCEL, 1);
connectThread = new Thread(this);
inputURL.addCommand(exitCmd);
inputURL.addCommand(downloadCmd);
inputURL.setCommandListener(this);
show.addCommand(backCmd);
show.setCommandListener(this);
myCanvas.addCommand(cancelCmd);
myCanvas.setCommandListener(this);
}
public void startApp(){
display.setCurrent(inputURL);
}
public void pauseApp(){
}
public void destroyApp(boolean conditional){
}
public void commandAction(Command c, Displayable d){
if( c == exitCmd){
destroyApp(true);
notifyDestroyed();
}
else if (c == downloadCmd){
try{
display.setCurrent(myCanvas);
connectThread.start();
myCanvas.t.start();
}
catch(Exception ex){}
}
else if(c == backCmd){
display.setCurrent(inputURL);
}
else if(c == cancelCmd){
isFinished = true;
dataInc = 0;
display.setCurrent(inputURL);
}
}
public void run(){
try{
image = download(inputURL.getString());
show.append(image);
display.setCurrent(show);
}
catch(Exception ex){}
}
Image download(String url) throws IOException{
ContentConnection con = (ContentConnection)Connector.open(url);
InputStream ins = con.openInputStream();
totalDataLength = (int)(con.getLength());
Image image = null;
int data = 0;
byte[] imageData = new byte[totalDataLength];
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while((data = ins.read()) != -1 || isFinished == true){
baos.write(data);
dataInc++;
}
imageData = baos.toByteArray();
isFinished = true;
baos.close();
image = Image.createImage(imageData, 0, imageData.length);
}
finally{
if(ins != null)
ins.close();
if(con != null)
con.close();
}
return image;
}
class MyCanvas extends Canvas implements Runnable{
Thread t;
public MyCanvas(){
t = new Thread(this);
}
public void run(){
while(!isFinished){
try{
repaint();
}
catch(Exception ex){}
}
}
public void paint(Graphics g){
int startX = 20;
int startY = getHeight()/2 - 2;
int width = getWidth()-40;
int height = 8;
g.setColor(0xffffff);
g.fillRect(0, 0, getHeight(), getHeight());
g.setColor(0);
g.drawString("下载中......", startX, startY - 20, Graphics.TOP|Graphics.LEFT);
g.drawRect(startX, startY, width, height);
g.setColor(0xff);
g.fillRect(startX, startY ,(dataInc*width/totalDataLength), height);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -