public static Color gradientColor(double value) { if (value < 0.0) return Color.gray; if (value > 1.0) value = 1.0; int red = Math.min(255, (int) (512.0 - (value * 512.0))); int green = Math.min(255, (int) (value * 512.0)); int blue = 0; return new Color(red, green, blue); }
public static void drawValueBar( int x, int y, String labelhead, double value, double percentage, int colorindex, Graphics g) { g.setColor(GetColor(colorindex)); g.drawRect(x, y, VALUE_BAR_WIDTH, VALUE_BAR_HEIGHT); int barwidth; if (percentage < 0.0) { // Unknown value barwidth = VALUE_BAR_WIDTH - 1; } else { barwidth = (int) ((VALUE_BAR_WIDTH - 1) * Math.min(1.0, percentage)); } // g.setColor(gradientColor(percentage)); g.fillRect(x + 1, y + 1, barwidth, VALUE_BAR_HEIGHT - 1); if (labelhead != null) { g.setFont(MainFrame.defaultFont); // g.setColor(MainFrame.labelColor); int off = 2; g.drawString(labelhead, x + VALUE_BAR_WIDTH + 2, y + VALUE_BAR_HEIGHT); // off += (labelhead.length()+1) * 4; // Just a guess String maxStr = new String("DateRate [/sec]:"); off += (int) ((maxStr.length() + 1) * 5.5); if (value < 0.0) { g.drawString("?", x + VALUE_BAR_WIDTH + off, y + VALUE_BAR_HEIGHT); } else { if (percentage < 0.0) { g.drawString(format(value), x + VALUE_BAR_WIDTH + off, y + VALUE_BAR_HEIGHT); } else { g.drawString( format(value) + " (" + format(percentage * 100) + "%)", x + VALUE_BAR_WIDTH + off, y + VALUE_BAR_HEIGHT); } } } }