1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class class extends MIDlet implements Runnable, CommandListener
{
private Display display;
private String URL = "adresse_de_l_image";
private Form formImage;
private Image im;
private Thread t1,moribund;
private int mode;
private boolean stopThread = false;
private Command exitCommand,stoprefreshCommand,autorefreshCommand,sortirCommand;
public class()
{
autorefreshCommand = new Command( "Rafraichissement auto",Command.SCREEN, 1);
stoprefreshCommand = new Command( "Arret Rafraichissement",Command.SCREEN, 1);
sortirCommand= new Command("Sortir",Command.SCREEN, 1);
exitCommand = new Command("Quitter",Command.SCREEN, 1);
formImage = new Form("Simple Image Test");
formImage.addCommand(autorefreshCommand);
formImage.addCommand(stoprefreshCommand);
formImage.addCommand(sortirCommand);
formImage.addCommand(exitCommand);
t1 = new Thread(this);
t1.start();
}
public void startApp()
{
display = Display.getDisplay(this);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional)
{
exitMIDlet();
}
private Image getImage(String url) throws IOException
{
HttpConnection connection = (HttpConnection) Connector.open(url);
DataInputStream iStrm = connection.openDataInputStream();
Image im = null;
try
{
byte imageData[];
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);
imageData = bStrm.toByteArray();
bStrm.close();
im = Image.createImage(imageData, 0, imageData.length);
}
finally
{
if (iStrm != null)
iStrm.close();
if (connection != null)
connection.close();
}
return (im == null ? null : im);
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run()
{
// TODO Auto-generated method stub
while(true)
{
try {
im = getImage(URL);
formImage.append(im);
formImage.setCommandListener(this);
} catch (Exception ex) {
System.out.println(ex);
}
display.setCurrent(formImage);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
formImage.deleteAll();
}
}
public void exitMIDlet()
{
notifyDestroyed();
}
public synchronized void stop() {
this.stopThread = true;
}
public synchronized void testFin() throws InterruptedException {
if( stopThread ) {
throw new InterruptedException();
}
}
public void commandAction(Command c, Displayable d)
{
String label = c.getLabel();
if(label.equals("Rafraichissement auto"))
{
formImage.deleteAll();
display = Display.getDisplay(this);
}
if(label.equals("Arret Rafraichissement"))
{
formImage.deleteAll();
display = Display.getDisplay(this);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
display = Display.getDisplay(this);
}
if(label.equals("Sortir"))
{
try {
testFin();
} catch (InterruptedException e) {
e.printStackTrace();
}
while(label.equals("Sortir"))
{
formImage.deleteAll();
}
}
if(label.equals("Quitter"))
{
destroyApp(true);
}
}
} |
Partager