I want to make an Analog Clock In blackberry, and I want the hands of the clock to be custom images.
I gone through this thread Help with analog clock code but didnt get it worked. Can any body help me to make an analog clock
Update The code i am getting is from the supports forum
// How to use it
ClockBitmapField clock = new ClockBitmapField(face, Field.NON_FOCUSABLE | Field.FIELD_HCENTER,
hrPng, minPng, secPng);
I can i use this to make an analog clock
How Can i use this
add(clock);
// Clock Face
class ClockBitmapField extends BitmapField {
Bitmap _face = null;
UpdateClockThread _updateClockThread = null;
Bitmap [] _hourBitmaps = null;
Bitmap [] _minBitmaps = null;
Bitmap [] _secBitmaps = null;
public ClockBitmapField(Bitmap face, long style, String [] hourPngs, String [] minPngs, String [] secPngs) {
super(face, style);
_face = face;
_ourBitmap = new Bitmap(_face.getWidth(), _face.getHeight());
this.setBitmap(_ourBitmap); // Swap to using work area
_hourBitmaps = new Bitmap [hourPngs.length];
for ( int i = 0; i < hourPngs.length; i++ ) {
_hourBitmaps[i] = Bitmap.getBitmapResource(hourPngs[i]);
}
_minBitmaps = new Bitmap [minPngs.length];
for ( int i = 0; i < minPngs.length; i++ ) {
_minBitmaps[i] = Bitmap.getBitmapResource(minPngs[i]);
}
_secBitmaps = new Bitmap [secPngs.length];
for ( int i = 0; i < secPngs.length; i++ ) {
_secBitmaps[i] = Bitmap.getBitmapResource(secPngs[i]);
}
}
protected void onDisplay() {
onExposed();
}
protected void onUnDisplay() {
onObscured();
}
protected void onExposed() {
if ( _updateClockThread == null || !_updateClockThread.isAlive() ) {
_updateClockThread = new UpdateClockThread(_ourBitmap, _face, this, _hourBitmaps, _minBitmaps, _secBitmaps);
_updateClockThread.start();
}
}
protected void onObscured() {
if ( _updateClockThread != null ) {
_updateClockThread.stop();
_updateClockThread = null;
}
}
public void invalidate() {
super.invalidate();
}
class UpdateClockThread extends Thread {
private Calendar _cal = null;
int _curHr = 0;
int _curMin = 0;
int _curSec = 0;
Bitmap _face = null;
int _faceWidth = 0;
int _faceHeight = 0;
_ourBitmap = null;
Graphics _g = null;
ClockBitmapField _ourField = null;
long LONG_ONE_THOUSAND = 1000;
boolean _stopped = false;
Bitmap [] _hourBitmaps = null;
Bitmap [] _minBitmaps = null;
Bitmap [] _secBitmaps = null;
public UpdateClockThread(Bitmap ourBitmap, Bitmap face, ClockBitmapField fieldToInvalidate,
Bitmap [] hourBitmaps, Bitmap [] minBitmaps, Bitmap [] secBitmaps) {
super();
_cal = Calendar.getInstance();
_face = face;
_faceWidth = _face.getWidth();
_faceHeight = _face.getHeight();
_ourBitmap = ourBitmap;
_g = new Graphics(_ourBitmap);
_ourField = fieldToInvalidate;
}
public void run() {
long timeToSleep = 0;
while (!_stopped) {
_g.setBackgroundColor(0x00191919);
_g.clear();
_g.drawBitmap(0, 0, _faceWidth, _faceHeight, _face, 0, 0);
_cal.setTime(new Date(System.currentTimeMillis()));
_curHr = cal.get(Calendar.HOUR);
_curMin = cal.get(Calendar.MINUTE);
_curHr = (_curHr * 5) + (5 * _curMin / 60);
if (_curHr > 60) _curHr = _curHr - 60;
_curSec = cal.get(Calendar.SECOND);
_g.drawBitmap(0, 0, _faceWidth, _faceHeight, _secBitmaps[_curSec], 0, 0);
_g.drawBitmap(0, 0, _faceWidth, _faceHeight, _minBitmaps[_curMin], 0, 0);
_g.drawBitmap(0, 0, _faceWidth, _faceHeight, _hourBitmaps[_curHr], 0, 0);
_ourField.invalidate();
timeToSleep = LONG_ONE_THOUSAND - ( System.currentTimeMillis() % LONG_ONE_THOUSAND );
if ( timeToSleep > 20 ) {
try {
Thread.sleep(timeToSleep);
} catch (Exception e) {
}
}
}
}
public void stop() {
_stopped = true;
}
}
}
and make minutes as second
thanks and regards


Just by looking at the code, it appears that the inputs:
String [] hourPngs, String [] minPngs, String [] secPngs are each a list of filenames of images which represent the clock hands at each position.
In this snippet, he builds an array of 60 Bitmaps from the strings:
_secBitmaps = new Bitmap [secPngs.length];
for ( int i = 0; i < secPngs.length; i++ ) {
_secBitmaps[i] = Bitmap.getBitmapResource(secPngs[i]);
}
Then, in this snippet you can see he gets the Bitmap object from a array by passing in the current "second" as the index:
_g.drawBitmap(0, 0, _faceWidth, _faceHeight, _secBitmaps[_curSec], 0, 0);
There doesn't appear to be any code where he rotates the images or anything. So I guess that means you need 60 images for each clock hand. (180 images in total, plus the clock face).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With