[leJOS教學] 感測器資訊顯示在NXT螢幕, 結合按鈕換頁

將4種感測器資訊顯示在NXT螢幕上, 結合按鈕換頁。 加入了Button.LEFT 與 Button.RIGHT的點擊事件監聽器, 藉此達到換頁的功能。

每一頁(case0~case3) 就是獨一的感測器資訊, 這樣就不用把所有的資訊都擠在同一頁了。 覺得NXT螢幕很擠的朋友可以參考本份程式碼。

螢幕截圖稍後補上~

=======================================================

import javax.microedition.lcdui.Graphics;

import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.LCD;
import lejos.nxt.LightSensor;
import lejos.nxt.SensorPort;
import lejos.nxt.SoundSensor;
import lejos.nxt.TouchSensor;
import lejos.nxt.UltrasonicSensor;
import lejos.util.Delay;

public class draw {
static int type=0;
public static void main(String[] arg){

LightSensor light=new LightSensor(SensorPort.S2);
UltrasonicSensor ultra=new UltrasonicSensor(SensorPort.S4);
TouchSensor touch=new TouchSensor(SensorPort.S1);
SoundSensor sound=new SoundSensor(SensorPort.S3);
Graphics gra=new Graphics();
int Lvalue,Uvalue,Svalue;
boolean Tvalue;
Button.ESCAPE.addButtonListener(new ButtonListener() {
public void buttonReleased(Button arg0) {System.exit(1);}
public void buttonPressed(Button arg0) {}
});

Button.LEFT.addButtonListener(new ButtonListener() {
public void buttonReleased(Button arg0) {
type–;
if(type<0){type=3;}
}
public void buttonPressed(Button arg0) {}
});

Button.RIGHT.addButtonListener(new ButtonListener() {
public void buttonReleased(Button arg0) {
type++;
if(type>3){type=0;}
}
public void buttonPressed(Button arg0) {}
});

while(true){
switch(type){
case 0:
Lvalue=light.readValue();
gra.clear();
LCD.drawString(“Light”, 1, 1);
gra.drawRect(40, 10, 10, 50);
gra.fillRect(41, 60-(int)(Lvalue/1.6), 9, (int)(Lvalue/1.6));
break;
case 1:
Uvalue=ultra.getDistance();
gra.clear();
LCD.drawString(“Ultra”, 1, 1);
gra.drawArc(25, 10, 50, 50, 0, 180);
gra.drawLine(25, 35, 75, 35);
if(Uvalue>0&&Uvalue<180){
gra.drawLine(50, 35, 50+(int)(Math.cos(Math.toRadians(Uvalue))*25), 35-(int)(Math.sin(Math.toRadians(Uvalue))*25));
}
break;
case 2:
Svalue=sound.readValue();
gra.clear();
LCD.drawString(“Sound”, 1, 1);
gra.drawLine(40, 50, 60, 50);
gra.drawLine(45, 60, 55, 60);
gra.drawLine(40, 50, 45, 60);
gra.drawLine(60, 50, 55, 60);
if(Svalue>25){gra.drawArc(40, 40, 20, 20, 45, 90);}
if(Svalue>50){gra.drawArc(35, 35, 30, 30, 45, 90);}
if(Svalue>75){gra.drawArc(30, 30, 40, 40, 45, 90);}
if(Svalue>100){gra.drawArc(25, 25, 50, 50, 45, 90);}
break;
case 3:
Tvalue=touch.isPressed();
gra.clear();
gra.drawRect(25, 10, 50, 50);
gra.drawRect(32, 17, 36, 36);
gra.drawLine(25, 10, 32, 17);
gra.drawLine(75, 60, 68, 53);
LCD.drawString(“Touch”, 1, 1);
if(Tvalue){

gra.drawLine(26,11,74,11);
gra.drawLine(27,12,73,12);
gra.drawLine(28,13,72,13);
gra.drawLine(29,14,71,14);

gra.drawLine(30,15,70,15);
gra.drawLine(31,16,69,16);

gra.drawLine(26,11,26,59);
gra.drawLine(27,12,27,58);
gra.drawLine(28,13,28,57);
gra.drawLine(29,14,29,56);
gra.drawLine(30,15,30,55);
gra.drawLine(31,16,31,54);

}
else{

gra.drawLine(74,11,74,59);
gra.drawLine(73,12,73,58);
gra.drawLine(72,13,72,57);
gra.drawLine(71,14,71,56);
gra.drawLine(70,15,70,55);
gra.drawLine(69,16,69,54);

gra.drawLine(74,59,26,59);
gra.drawLine(73,58,27,58);
gra.drawLine(72,57,28,57);
gra.drawLine(71,56,29,56);
gra.drawLine(70,55,30,55);
gra.drawLine(69,54,31,54);
}
break;
}
Delay.msDelay(500);
}

}
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *