📄 clock.he
字号:
class triangleClock{ const int CMD_BACK = 1; const int ONE_SECOND = 1000; const int ONE_MINUTE = 60*1000; const String VIEW_ANALOG = "analogView"; const String VIEW_DIGITAL = "digitalView"; MenuItem m_back = new MenuItem(CMD_BACK, "Back"); Calendar m_time = new Calendar(); Canvas m_analog; Label m_digi; Timer m_timer; void updateDigital() { if (m_digi != null) { m_digi.setText(format("%02i:%02i", m_time[HOUR_OF_DAY], m_time[MINUTE], m_time[SECOND])); m_digi.repaint(false); } } Component createElement(String viewName, String elementName, Style style, Object context) { if (elementName.equals("digital")) { m_digi = new Label(style, ""); updateDigital(); return m_digi; } else if (elementName.equals("analog")) { m_analog = new Canvas(style); return m_analog; } else { return null; } } void resetTimer(int interval) { if (m_timer != null) { m_timer.cancel(); m_timer = null; } if (interval > 0) { m_timer = schedule(1000, interval); } } void startWidget() { resetTimer(ONE_MINUTE); setMinimizedView(createMinimizedView(VIEW_DIGITAL, null)); } void stopWidget() { resetTimer(0); } Shell openWidget() { resetTimer(ONE_SECOND); m_time.setMillis(currentTimeMillis()); Shell shell = new Shell(createMaximizedView(VIEW_ANALOG, null)); int w, int h = getScreenSize(); slideIn(0, w, w, h, shell); return null; //return new Shell(createMaximizedView(VIEW_ANALOG, null)); } MenuItem getSoftKey(Shell shell, Component focused, int key) { if (key == SOFTKEY_BACK) { return m_back; } return null; } void actionPerformed(Shell shell, Component source, int action) { switch(action) { case CMD_BACK: { m_analog = null; int w, int h = getScreenSize(); slideOut(0, h, w, h, shell); resetTimer(ONE_MINUTE); } break; } } const int SECOND_HAND = 60; const int MINUTE_HAND = 45; const int HOUR_HAND = 30; void drawHand(Graphics g, int cx, int cy, int angle, int base, int height) { int dx = cx+(height*cos(angle))/MATH_SCALE; int dy = cy+(height*sin(angle))/MATH_SCALE; int ax = cx+(base*cos(angle-90))/MATH_SCALE; int ay = cy+(base*sin(angle-90))/MATH_SCALE; int bx = cx+(base*cos(angle+90))/MATH_SCALE; int by = cy+(base*sin(angle+90))/MATH_SCALE; g.fillTriangle(ax, ay, bx, by, dx, dy); } void paint(Component c, Graphics g, Style style, int width, int height) { int h = m_time[HOUR]; int m = m_time[MINUTE]; int s = m_time[SECOND]; int cx = width/2; int cy = height/2; { int angle = (30*h)+(m/2) - 90; g.setColor(0xdc4200); drawHand(g, cx, cy, angle, 6, HOUR_HAND); } { int angle = (6*m)+(s/10) - 90; g.setColor(0xdc4200); drawHand(g, cx, cy, angle, 6, MINUTE_HAND); } { int angle = 6*s - 90; int dx = cx+(SECOND_HAND*cos(angle))/MATH_SCALE; int dy = cy+(SECOND_HAND*sin(angle))/MATH_SCALE; g.setColor(0xffffff); g.drawLine(cx, cy, dx, dy); } g.drawImage(style.image(0), cx, cy, HCENTER|VCENTER); } void timerEvent(Timer timer) { m_time.setMillis(currentTimeMillis()); if (m_analog != null) { m_analog.repaint(false); flushScreen(false); } else if (/*isWidgetVisible()*/isDashboardVisible()) { updateDigital(); flushScreen(true); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -