Java Reference
In-Depth Information
database. I've cheated a bit here and actually graphed weight against
record number rather than date (otherwise the records would need to be
kept sorted) but it conveys the general idea.
When you work with a canvas, event handling is more generalized - all
events are passed to the processEvent(int type, int param)
method of the canvas and you handle or ignore them as required. The
type parameter is a static field from the Display class and the param
parameter varies with the event type. In other words, you do not need
to implement an interface to handle events in DoJa at a low level - you
simply override the processEvent() method of the Canvas class.
Looking at the source, you'll see a large amount of code in the
constructor - this is simply used to pre-calculate as much as possible
(such as range mapping and where labels need to go, for example) in one
hit and it isn't relevant to this discussion. However take note in the listing
below that Canvas still supports softkey events and, in fact, that's how
we handle navigating back to the main screen from here:
private class GraphView extends Canvas {
private int[] xpoints, ypoints;
private int ox,oy,xmax,ymax;
...
public GraphView() {
dataCount = database.size();
...
xmapFactor = (float) (xmax - ox) / (float) dataCount; // never zero
ymapFactor = (float) (oy - ymax) / (rangeMax[0] - rangeMin[0]);
xpoints = new int[dataCount];
ypoints = new int[dataCount];
...
setSoftLabel(Frame.SOFT_KEY_1, " ");
setSoftLabel(Frame.SOFT_KEY_2, "Back");
}
public void paint(Graphics g) {
g.lock();// double buffered drawing sequence
// draw axes
g.setColor(Graphics.getColorOfName(Graphics.BLACK));
g.drawLine(ox,oy,ox,ymax);
g.drawLine(ox,oy,xmax,oy);
// draw vertical axis labels
g.drawString(ymaxValue, maxLabelX, maxLabelY);
g.drawString(yminValue, minLabelX, minLabelY);
// draw graph
g.setColor(Graphics.getColorOfName(Graphics.RED));
g.drawPolyline(xpoints, ypoints, dataCount);
g.unlock(true);
} public void processEvent(int type, int param) {
if(type == Display.KEY_RELEASED_EVENT) {
if(param == Display.KEY_SOFT2) { // Back
 
Search WWH ::




Custom Search