Java Reference
In-Depth Information
screen that always points to the top of the screen. If you turn the phone on
its side, the program determines which side is now the top and updates
the arrow accordingly.
import com.nttdocomo.ui.*;
import com.nttdocomo.device.location.*;
public class AccelDemo extends IApplication {
public void start() {
MainCanvas canvas = new MainCanvas();
new Thread(canvas).start();
Display.setCurrent(canvas);
}
} class MainCanvas extends Canvas implements Runnable {
AccelerationSensor sensor;
MainCanvas() {
setSoftLabel(SOFT_KEY_1, "END");
setBackground(Graphics.getColorOfName(Graphics.BLUE));
sensor = AccelerationSensor.getAccelerationSensor();
} int[] angle = null;
public void run() {
Graphics g = this.getGraphics();
while(true) {
sensor.start(sensor.getIntervalResolution());
try {
Thread.sleep(100);
} catch(Exception e) {}
sensor.stop();
AccelerationData data = sensor.getData();
angle = data.getScreenOrientation();
sensor.disposeData();
redraw(g);
}
} public void paint(Graphics g)
{
}
{
public void redraw(Graphics g)
g.lock();
g.clearRect(0, 0, Display.getWidth(), Display.getHeight());
g.setColor(Graphics.getColorOfName(Graphics.WHITE));
if(angle != null) {
g.drawString(" "+angle[0], 10, 15);
switch(angle[0]) {
case 0:
g.drawLine(120, 120, 120, 0);
g.drawLine(120, 0, 130, 10);
break;
case 90:
g.drawLine(120, 120, 0, 120);
g.drawLine(0, 120, 10, 130);
break;
case 180:
g.drawLine(120, 120, 120, 240);
 
Search WWH ::




Custom Search