Java Reference
In-Depth Information
dos.writeInt (date);
int size = entries.size();
dos.writeInt (size);
for (int i = 0; i < size; i++) {
dos.writeInt (getTime (i));
dos.writeInt (getValue (i));
}
dos.close();
return bos.toByteArray();
}
Helper Methods for the User Interface
Although the user interface itself is specific to the target profile, we can simplify the user interface code
by providing some helper methods in the DayLog class. Here, we add a set of methods for two
purposes:
• Converting the date of the DayLog to a readable string
• Scaling the entries to a given display size for simplified drawing
In order to convert the date to a String , we just extract the year, month, and day of month using the
corresponding shift and mask operations. Those parts are concatenated with hyphens:
public String getTitle() {
return "" + (date >> 16)
+ "-" + ((date >> 8) & 0x0ff)
+ "-" + (date & 0x0ff);
}
In order to scale a value to the size of the screen, we would usually normalize it by subtracting the
MIN_VALUE and dividing by the difference of MAX_VALUE and MIN_VALUE , and then scale it up to
the space available by multiplying by the size available. Because we do not have floating-point
numbers in CLDC, we swap the normalization and scaling steps. Thus, we first multiply by the screen
size and then divide by the value range. By first multiplying and then dividing, we make sure that we
do not leave the scope of the integer data type:
public static int getY (int value, int size) {
return - (value - MIN_VALUE) * size / (MAX_VALUE - MIN_VALUE);
}
In the getYPoints() method, we build an array of all values of the DayLog , scaled to the given
screen size, utilizing the getY() method:
public int [] getYPoints (int size) {
int [] y = new int [getCount()];
for (int i = 0; i < getCount(); i++) {
y [i] = getY (getValue (i), size);
}
return y;
}
The getXPoints() method is analogous to getYPoints() , except that we do not define a
getX() helper method but perform the scaling step immediately in the loop:
Search WWH ::




Custom Search