/* * aclock.java * * Created on November 26, 2007, 10:29 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package clock; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.Calendar; import java.util.TimeZone; /** * * @author sjuva */ public class aclock extends CustomItem { double diam = 0.38; double sLen = 0.90; double mLen = 0.75; double hLen = 0.50; int W = 100; int H = 100; int S; int R; int OX; int OY; private double digitR = 1.22; private double digitS = 0.3; // not used protected boolean showSeconds = true; // TODO: not used Calendar now; /** Creates a new instance of aclock */ public aclock(String label) { super(label); sizeChanged(W, H); } public aclock(String label, int size) { super(label); sizeChanged(size, size); } protected int getMinContentWidth() { return 10; } protected int getMinContentHeight() { return 10; } protected int getPrefContentWidth(int height) { return W; } protected int getPrefContentHeight(int width) { return H; } protected void sizeChanged(int w, int h) { H = h; W = w; S = H; if (W < S) S = W; R = (int)(diam*(double)S); OX = S/2; OY = S/2; now = Calendar.getInstance(); } private int pointX(double minute, double radius, int ox) { double angle = minute * Math.PI / 30.0; return (int)((double)ox + radius * Math.sin(angle)); } private int pointY(double minute, double radius, int oy) { double angle = minute * Math.PI / 30.0; return (int)((double)oy - radius * Math.cos(angle)); } public void update() { now = Calendar.getInstance(); repaint(); } public void update(Calendar currtime) { now = currtime; repaint(); } protected void paint(Graphics g, int w, int h) { // Calendar now = Calendar.getInstance(TimeZone.getDefault()); // Calendar now = Calendar.getInstance(); g.setColor(0xFFFFFF); g.fillRect(0, 0, W-1, H-1); g.setColor(0x000000); // g.drawRect(0, 0, W-1, H-1); g.drawArc(OX-R, OY-R, R*2, R*2, 0, 360); // System.out.print("S " + S + " R " + R + " OX " + OX + " OY " + OY + " W " + W + " H " + H + " w " + w + " h " + h); // System.out.print(" el " + (OX-R) + "," + (OY-R) + "," + (OX+R-1) + "," + (OY+R-1)); // System.out.println(); // tics&digits Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL); // (int)(R*digitS)); g.setFont(font); int textH = font.getHeight(); for (int hour = 1; hour <= 12; hour++) { double angle = hour*60.0/12.0; g.drawLine(pointX(angle, R*0.92, OX), pointY(angle, R*0.92, OY), pointX(angle, R, OX), pointY(angle, R, OY)); // texts int textW = font.stringWidth("" + hour); g.drawString("" + hour, (int)pointX(angle, R*digitR, OX) - textW/2, (int)pointY(angle, R*digitR, OY) - textH/2, 0); // g.HCENTER | g.VCENTER); } // hour g.drawLine(OX, OY, pointX((double)now.get(now.HOUR) * 60.0/12.0 + (double)now.get(now.MINUTE)/12.0, R*hLen, OX), pointY((double)now.get(now.HOUR) * 60.0/12.0 + (double)now.get(now.MINUTE)/12.0, R*hLen, OY) ); // minute g.drawLine(OX, OY, pointX((double)now.get(now.MINUTE) + (double)now.get(now.SECOND) / 60.0, R*mLen, OX ), pointY((double)now.get(now.MINUTE) + (double)now.get(now.SECOND) / 60.0, R*mLen, OY ) ); // second g.drawLine(OX, OY, pointX((double)now.get(now.SECOND), R*sLen, OX ), pointY((double)now.get(now.SECOND), R*sLen, OY ) ); } // paint() }