/** * high-level method for drawing a chart axis line plus labeled tic marks. introduces a dependancy * on AWT because it takes a Graphics parameter. perhaps this method belongs in some higher-level * class but i added it here since it's highly related with the tic lable generation code. * * @author Melinda Green * @param axis is one of Axis.X_AXIS or Axis.Y_AXIS. * @param maxTics is the maximum number of labeled tics to draw. note: the actual number drawn may * be less. * @param lowVal is the smallest value tic mark that may be drawn. note: the lowest valued tic * label may be greater than this limit. * @param highVal is the largest value tic mark that may be drawn. note: the highest valued tic * label may be less than this limit. * @param screenStart is the coordinate in the low valued direction. * @param screenEnd is the coordinate in the high valued direction. * @param offset is the coordinate in the direction perpendicular to the specified direction. * @param logScale is true if a log scale axis is to be drawn, false for a linear scale. * @param screenHeight is needed to flip Y coordinates. * @param g is the AWT Graphics object to draw into. note: all drawing will be done in the current * color of the given Graphics object. */ public static void drawAxis( int axis, int maxTics, int ticLength, float lowVal, float highVal, int screenStart, int screenEnd, int screenOffset, boolean logScale, int screenHeight, Graphics g) { if (logScale && (lowVal == 0 || highVal == 0)) throw new IllegalArgumentException("Axis.drawAxis: zero range value not allowed in log axes"); if (axis == X_AXIS) // horizontal baseline g.drawLine(screenStart, screenHeight - screenOffset, screenEnd, screenHeight - screenOffset); else // vertical baseline g.drawLine(screenOffset, screenStart, screenOffset, screenEnd); Vector tics = Axis.computeTicks(lowVal, highVal, maxTics, logScale); // nice // round // numbers // for // tic // labels int last_label_end = axis == X_AXIS ? -88888 : 88888; String dbgstr = "tics: "; for (Enumeration e = tics.elements(); e.hasMoreElements(); ) { String ticstr = (String) e.nextElement(); if (DEBUG) dbgstr += ticstr + ", "; float ticval = Float.parseFloat(ticstr); int tic_coord = screenStart; Dimension str_size = stringSize(ticstr, g); tic_coord += plotValue(ticval, lowVal, highVal, screenStart, screenEnd, logScale, screenHeight); if (axis == X_AXIS) { // horizontal axis == vertical tics g.drawLine( tic_coord, screenHeight - screenOffset, tic_coord, screenHeight - screenOffset + ticLength); if (tic_coord - str_size.width / 2 > last_label_end) { g.drawString( ticstr, tic_coord - str_size.width / 2, screenHeight - screenOffset + str_size.height + 5); last_label_end = tic_coord + str_size.width / 2 + str_size.height / 2; } } else { // vertical axis == horizontal tics tic_coord = screenHeight - tic_coord; // flips Y coordinates g.drawLine(screenOffset - ticLength, tic_coord, screenOffset, tic_coord); if (tic_coord - str_size.height / 3 < last_label_end) { g.drawString( ticstr, screenOffset - ticLength - str_size.width - 5, tic_coord + str_size.height / 3); last_label_end = tic_coord - str_size.height; } } } if (DEBUG) System.out.println(dbgstr); } // end drawAxis