Previous Tip  |  Next Tip  |  Design Tips   | [Bill's Home]

... threads in Java for the Pocket PC

A threads-based approach allows us to create specialized task, which have a small definitive goal. It is much easier to test a small program which has a definitive task, rather than to test a large and complex piece of software. It is also easier to upgrade a thread, without af-fecting the overall operation of the complete program [Essay]. Chapter 15 of the Mastering Java book contains some examples on Multithreading, which is one of Java's strengths.

 

// Source Mastering Java, Bill Buchanan.
// Chapter 15, Program 3

import java.util.*;
import java.awt.*;
import java.applet.*;

public class chap15_03 extends Applet
implements Runnable
{
Thread timer = null;
int x=0,y=0;

public void paint(Graphics g)
{
g.setColor(getBackground());
g.fillOval(x,y,20,10);

x+=(Math.random()*10)-5;
y+=(Math.random()*10)-5;

if ( x>200) x=200;
if ( y>200) y=200;
if ( x<0 ) x=0;
if ( y<0 ) y=0;

g.setColor(Color.blue);
g.fillOval(x,y,20,10);
}

public void start()
{
if(timer == null)
{
timer = new Thread(this);
timer.start();
}
}

public void stop()
{
timer = null;
}

public void run()
{
while (timer != null)
{
delay(100); // wait 100 ms
repaint();
}
timer = null;
}

public void delay(int ms)
{
try {timer.sleep(ms);}
catch (InterruptedException e){}
}
}

 

Bill: The alien moves every 0.1 seconds.

// Source Mastering Java, Bill Buchanan.
import java.util.*;
import java.awt.*;
import java.applet.*;

public class chap15_04 extends Applet
implements Runnable
{
Thread timer = null;
int x=100,y=100;//put alien in middle of
//screen

public void paint(Graphics g)
{
g.setColor(getBackground());
alien(g);

x+=(Math.random()*10)-5;
y+=(Math.random()*10)-5;

if ( x>200) x=200; if ( y>200) y=200;
if ( x<0 ) x=0; if ( y<0 ) y=0;

g.setColor(Color.blue);
alien(g);
}

public void alien(Graphics g)
{
g.fillOval(x,y,10,10);
g.fillOval(x+30,y,10,10);
g.fillOval(x,y+30,40,20);
g.drawLine(x+5,y+10,x+5,y+35);
g.drawLine(x+35,y+10,x+35,y+35);
}

public void start()
{
if(timer == null)
{
timer = new Thread(this);
timer.start();
}
}

public void stop()
{
timer = null;
}
public void run()
{
while (timer != null)
{
delay(100); // wait 100 ms
repaint();
}
timer = null;
}

public void delay(int ms)
{
try {timer.sleep(ms);}
catch (InterruptedException e){}
}
}


Bill: This program will update the time every second, but you will only see an update every minute. See please wait for a minute!

// Source Mastering Java, Bill Buchanan.
// Chapter 15, Program 1

import java.util.*;
import java.awt.*;
import java.applet.*;

public class chap15_01 extends Applet
implements Runnable
{
Thread mytimer = null;
String lastdate = null;

public void paint(Graphics g)
{
String today;
Date ddd = new Date();

today = ddd.toLocaleString();

g.setFont(new Font("TimesRoman",
Font.BOLD, 22));

// Erase and redraw time
g.setColor(getBackground());

if (lastdate!=null)
g.drawString(lastdate, 5, 40);

g.setColor(Color.darkGray);
g.drawString(today, 5, 40);
lastdate = today;
}

public void start()
{
if(mytimer == null)
{
mytimer = new Thread(this);
mytimer.start();
}
}

public void stop()
{
mytimer = null;
}

public void run()
{
while (mytimer != null)
{
try {mytimer.sleep(1000);}
catch (InterruptedException e){}
repaint();
}
mytimer = null;
}
}