Interaction of streams.
Now we shall consider as it is possible to realize animated prompt at the moment of an establishment of connection. For what it is necessary? If the application assumes intensive data exchange with the server well at the moment of connection to deduce{remove} a picture. As in given clause{article} we consider{examine} streams it is possible to make a picture animated. In the beginning some remarks. In the specification it is said, that MIDP the container is obliged to be able to work with graphic files in format PNG. Thus formats GIF and JPG are not mentioned. Nevertheless, a plenty of phones is enough support these formats, but there are models (some of which are very popular, for example smartfony firms HTC), which these formats do not support. Therefore at us all of a picture will be only in PNG.
The animation prompt should work as follows. When the user presses the command of connection with the server the picture emerges animirovanaja. When data exchange with the server comes to the end a picture is cleaned{removed}. All is very simple. Proceeding from reasons resulted earlier, for prompt we shall create one stream and we shall simply cause it{him} during that moment when it will be necessary to us.
...
...
protected void startApp () throws MIDletStateChangeException {
connecto = new ConnectionThread ();
new Thread (connecto) .start ();
animationT = new AnimThread ();
new Thread (animationT) .start ();
Display.getDisplay (this) .setCurrent (mainForm);
}
...
...
public void commandAction (Command command, Displayable arg1) {
if (command.equals (connectSimple)) {
connecto.conn ();
}
}
private class ConnectionThread implements Runnable {
private Object mut = new Object ();
private boolean shouldRun = true;
public void run () {
while (shouldRun) {
synchronized (mut) {
try {
mut.wait ();
} catch (InterruptedException e) {}
}
if (! input.getString () .equals (" ")) {
animationT.show ();
connectWOThread ();
input.setString (" ");
animationT.hide ();
}
}
}
public void conn () {
synchronized (mut) {
mut.notify ();
}
}
}
private class AnimThread extends Canvas implements Runnable {
private final String path = "/test/multithreading/start.png";
private final int w = 55; // sprite pixels width
private final int h = 68; // sprite pixels height
private final int delay = 100; // milliseconds
private final long minShows = 5000;
private Sprite anim = null;
private Object mut = new Object ();
private boolean shouldRun = true;
private Boolean show = new Boolean (false);
public AnimThread () {
InputStream is = AnimThread.class.getResourceAsStream (path);
Image im;
try {
im = Image.createImage (is);
anim = new Sprite (im, w, h);
// adjusting position of animation
int w1 = this.getWidth ();
int h1 = this.getHeight ();
anim.setPosition ((w1-anim.getWidth ())/2, (h1 - anim.getHeight ())/2);
} catch (IOException e) {
e.printStackTrace ();
}
}
public void run () {
while (shouldRun) {
synchronized (mut) {
try {
mut.wait ();
} catch (InterruptedException e) {}
}
if (anim! = null) {
long star_time = System.currentTimeMillis ();
boolean finished = false;
synchronized (show) {
finished = false;
}
int counter = 0;
repaint ();
while (! finished) {
paintSingleFrame (counter ++);
counter = counter <anim.getFrameSequenceLength ()? counter:0;
if (! shouldRun) {
break;
}
synchronized (show) {
if ((star_time+minShows) <System.currentTimeMillis () **! show.booleanValue ()) {
finished = true;
disp.setCurrent (mainForm);
}
}
}
}
}
}
private void paintSingleFrame (int frameIndex) {
anim.setFrame (frameIndex);
repaint (anim.getX (), anim.getY (), anim.getWidth (), anim.getHeight ());
try {
Thread.sleep (delay);
} catch (InterruptedException e) {}
}
protected void paint (Graphics g) {
g.setColor (255,255,255);
g.fillRect (g.getClipX (), g.getClipY (), g.getClipWidth (), g.getClipHeight ());
if (anim! =null) {
anim.paint (g);
}
}
public void show () {
synchronized (mut) {
show = new Boolean (true);
disp.setCurrent (this);
mut.notify ();
}
}
public void hide () {
synchronized (show) {
show = new Boolean (false);
}
}
}
}
As it is possible to see from an above mentioned code ours midlet began more complex{difficult}. Simultaneously at us two streams are started. One of them as before is responsible for network connections. Other stream is responsible for show of animated prompt.
The stream responsible for show of animation, becomes in a mode of expectation right after the start. For it the object mut responds. The stream is activated by precisely same way, as well as a stream connecto. Some complexity of a code is caused by that, that animation will be shown at least minShows milliseconds.
Application
The way of realization described above midleta, establishing{installing} network connections, was applied by development of the mobile application to system DomEconom. This system is intended for the help at conducting the account of the family finance. In her rather interesting technology of the distributed{allocated} data storage that allows to bring the data on incomes and charges different members of family from different computers, for example with domestic and from the worker is realized. The system itself synchronizes the data. Moreover all data are stored{kept} in the ciphered kind, and the password is known only to the user. Thus nobody knowing the password cannot read the confidential information. For simplification of data input about charges has been developed midlet. He works as follows. The user fills in the form in which specifies the sum, the bill, a category, currency, etc. sends these data on the server. During sending midlet ciphers the data, shows the user animated prompt. The system is completely free-of-charge. To see her{it} it is possible to the address: www.domeconom.ru

|