待ち受けjiglet解説

7セグメント液晶時計


import jp.jig.jiglet.*;
public class clkseg3 extends Jiglet {
	static String URL = "http://club.jig.jp/taisuke/clkresis.jiglet";
	boolean display = true;
	public void main() {
		
		//メニュー設置
		setMenu(new String[] { "共有する", "(c)taisuke" }, new String[] { "このjigletのバーコードを表示します", "このjigletのサイトにアクセスします" });
		
		//ソフトラベルの設定
		setSoftLabel("終了");
		
		//活性時間
		long deactivetime = 0;
		final int VIEWTIME = 3 * 60 * 1000;
		
		for (;;) {
			int key = getKey();
			
			//ソフトキーか、クリアキーが押されたら
			if (key == KEY_SOFT || key == KEY_IAPP) {
				terminate();
			} else if (key > KEY_LAST) {
				int evt = key & 0xffff0000;	//イベント取得
				int val = key & 0xffff;		//メニュー選択位置の取得
				if (evt == EVENT_MENU) {	//メニューを選択されたら
					if (val == 0) {
						launch("http://club.jig.jp/taisuke/makeqr.jiglet?http://br.jig.jp/?p=500&jp=" + encodeURL(URL) , ENCODE_SJIS, LAUNCH_KEEP_ALIVE);
					} else if (val == 1) {
						launch(URL, ENCODE_SJIS, LAUNCH_KEEP_ALIVE);
					}
				} else if (evt == EVENT_CLOSE) {	//端末を閉める
					display = false;
				} else if (evt == EVENT_OPEN) {		//端末を開ける
					display = true;
					deactivetime = 0;
				} else if (evt == EVENT_ACTIVATE) {	//活性化したら
					terminate();
				}
			}
			//非活性化状態だったら
			if (getState(STATE_JIGLET) == JIGLET_DEACTIVE) {
				//非活性状態の時間を計測開始
				if (deactivetime == 0)
					deactivetime = currentTime();
				
				//非活性化状態がVIEWTIMEを超えたら、ディスプレイをOFF
				if (currentTime() - deactivetime > VIEWTIME)
					display = false;
			}
			repaint();
			sleep(200);
			
			//ディスプレイOFFだったら休眠モードに
			if (!display) {
				setState(STATE_JIGLET, JIGLET_SLEEP);
			}
		}
	}
	int fw, fh;	//文字の高さと幅
	int sx, sy;	//
	int sw, sh;	//
	int margin;	//
	
	public void paint(boolean allDrawFlag) {
		//フォントの設定
		setFont(FONT_SMALL);
		
		//文字の高さと幅を取得
		if (fw == 0) {
			fw = stringWidth("あ");
			fh = getFontHeight();
		}
		//現在の時間と分と秒を取得
		setTime(currentTime());
		int hour = getTime(CALENDAR_HOUR_OF_DAY);
		int min = getTime(CALENDAR_MINUTE);
		int sec = getTime(CALENDAR_SECOND);
		//----------------------------------------
		
		setColor(0xc5c595);
		fillRect(0, 0, DW, DH);
		
		//ディスプレイOFFだったら終了
		if (!display)
			return;
		
		//現在の時刻を6個の数字に分ける
		int[] value = new int[6];
		value[0] = hour / 10;
		value[1] = hour % 10;
		value[2] = min / 10;
		value[3] = min % 10;
		value[4] = sec / 10;
		value[5] = sec % 10;
		
		int dw = DW / 8;
		int dh = dw * 3 / 2;
		int mg = 2;
		int offx = (DW - dw * 6 - dw) / 2;
		int offy = (DH - dh - mg * 2) / 2;
		int tw = dw / 8;
		
		int x = offx;
		int y = offy;
		
		//時刻の表示
		for (int i = 0; i < 6; i++) {
			setColor(0xa5a57c);
			draw7seg(8, x, y, dw - mg * 2, dh - mg * 2, tw);
			setColor(0x292929);
			draw7seg(value[i], x, y, dw - mg * 2, dh - mg * 2, tw);
			x += dw;
			
			if (i == 5)
				break;
			if ((i + 1) % 2 == 0) {
				drawColon(x + 2, y, dw / 2, dh - mg * 2);
				x += dw / 2;
			}
		}
	}
	//------------------------------------------------------------------
	// n >= 0, n <= 9
	static int[] SEG7 = {
		125,
		96,
		55,
		103,
		106,
		79,
		95,
		105,
		127,
		111,
	};
	static void drawColon(int x, int y, int w, int h) {
		int r = w;
		int yy = y + h / 2 - r; //* 3 / 2;
		for (int i = 0; i < 2; i++) {
			int xx = x;
			drawLine(xx, yy + 3, xx++, yy + 6);
			drawLine(xx, yy + 2, xx++, yy + 7);
			drawLine(xx, yy + 1, xx++, yy + 8);
			drawLine(xx, yy + 0, xx++, yy + 9);
			drawLine(xx, yy + 0, xx++, yy + 9);
			drawLine(xx, yy + 0, xx++, yy + 9);
			drawLine(xx, yy + 1, xx++, yy + 8);
			drawLine(xx, yy + 2, xx++, yy + 7);
			drawLine(xx, yy + 3, xx++, yy + 6);
			
			yy = y + h / 2 + r / 2;
		}
	}
	
	//7セグメントの数字を書く関数
	static void draw7seg(int n, int x, int y, int w, int h, int tw) {
		int d = SEG7[n];
		if ((d & (1 << 0)) != 0) { // 上
//			fillRect(x, y, w, tw);
			int yy = y;
			drawLine(x + 4, yy, x + w - 4, yy++);
			drawLine(x + 3, yy, x + w - 3, yy++);
			drawLine(x + 3, yy, x + w - 3, yy++);
			drawLine(x + 4, yy, x + w - 4, yy++);
			drawLine(x + 5, yy, x + w - 5, yy++);
			drawLine(x + 6, yy, x + w - 6, yy++);
			drawLine(x + 7, yy, x + w - 7, yy++);
		}
		if ((d & (1 << 1)) != 0) { // 中
//			fillRect(x, y + (h - tw) / 2, w, tw);
			int yy = y + h / 2 - 4;
			drawLine(x + 8, yy, x + w - 8, yy++);
			drawLine(x + 7, yy, x + w - 7, yy++);
			drawLine(x + 6, yy, x + w - 6, yy++);
			drawLine(x + 5, yy, x + w - 5, yy++);
			drawLine(x + 5, yy, x + w - 5, yy++);
			drawLine(x + 6, yy, x + w - 6, yy++);
			drawLine(x + 7, yy, x + w - 7, yy++);
			drawLine(x + 8, yy, x + w - 8, yy++);
		}
		if ((d & (1 << 2)) != 0) { // 下
//			fillRect(x, y + (h - tw), w, tw);
			int yy = y + h - 6;
			drawLine(x + 7, yy, x + w - 7, yy++);
			drawLine(x + 6, yy, x + w - 6, yy++);
			drawLine(x + 5, yy, x + w - 5, yy++);
			drawLine(x + 4, yy, x + w - 4, yy++);
			drawLine(x + 3, yy, x + w - 3, yy++);
			drawLine(x + 3, yy, x + w - 3, yy++);
			drawLine(x + 4, yy, x + w - 4, yy++);
		}
		if ((d & (1 << 3)) != 0) { // 左上
//			fillRect(x, y, tw, h / 2);
			int xx = x;
			int y2 = y + h / 2 - 1;
			drawLine(xx, y + 4, xx++, y2 - 4);
			drawLine(xx, y + 3, xx++, y2 - 3);
			drawLine(xx, y + 3, xx++, y2 - 2);
			drawLine(xx, y + 4, xx++, y2 - 1);
			drawLine(xx, y + 5, xx++, y2 - 1);
			drawLine(xx, y + 6, xx++, y2 - 2);
			drawLine(xx, y + 7, xx++, y2 - 3);
			drawLine(xx, y + 8, xx++, y2 - 4);
		}
		if ((d & (1 << 4)) != 0) { // 左下
//			fillRect(x, y + h / 2, tw, h / 2);
			int xx = x;
			int yy = y + h;
			int y2 = y + h / 2;
			drawLine(xx, yy - 4, xx++, y2 + 4);
			drawLine(xx, yy - 3, xx++, y2 + 3);
			drawLine(xx, yy - 3, xx++, y2 + 2);
			drawLine(xx, yy - 4, xx++, y2 + 1);
			drawLine(xx, yy - 5, xx++, y2 + 1);
			drawLine(xx, yy - 6, xx++, y2 + 2);
			drawLine(xx, yy - 7, xx++, y2 + 3);
			drawLine(xx, yy - 8, xx++, y2 + 4);
		}
		if ((d & (1 << 5)) != 0) { // 右上
//			fillRect(x + (w - tw), y, tw, h / 2);
			int xx = x + w;
			int y2 = y + h / 2 - 1;
			drawLine(xx, y + 4, xx--, y2 - 4);
			drawLine(xx, y + 3, xx--, y2 - 3);
			drawLine(xx, y + 3, xx--, y2 - 2);
			drawLine(xx, y + 4, xx--, y2 - 1);
			drawLine(xx, y + 5, xx--, y2 - 1);
			drawLine(xx, y + 6, xx--, y2 - 2);
			drawLine(xx, y + 7, xx--, y2 - 3);
			drawLine(xx, y + 8, xx--, y2 - 4);
		}
		if ((d & (1 << 6)) != 0) { // 右下
//			fillRect(x + (w - tw), y + h / 2, tw, h / 2);
			int xx = x + w;
			int yy = y + h;
			int y2 = y + h / 2;
			drawLine(xx, yy - 4, xx--, y2 + 4);
			drawLine(xx, yy - 3, xx--, y2 + 3);
			drawLine(xx, yy - 3, xx--, y2 + 2);
			drawLine(xx, yy - 4, xx--, y2 + 1);
			drawLine(xx, yy - 5, xx--, y2 + 1);
			drawLine(xx, yy - 6, xx--, y2 + 2);
			drawLine(xx, yy - 7, xx--, y2 + 3);
			drawLine(xx, yy - 8, xx--, y2 + 4);
		}
	}
}

タグ:

+ タグ編集
  • タグ:
最終更新:2008年01月22日 15:42