⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 histogrambar.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
package com.sri.oaa2.agt.monitor;

/**
 * The contents of this file are subject to the OAA  Community Research
 * License Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License
 * at http://www.ai.sri.com/~oaa/.  Software distributed under the License
 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * rights and limitations under the License.  Portions of the software are
 * Copyright (c) SRI International, 1999.  All rights reserved.
 * "OAA" is a registered trademark, and "Open Agent Architecture" is a
 * trademark, of SRI International, a California nonprofit public benefit
 * corporation.
*/
 
import java.awt.*;
import javax.swing.*;

public class HistogramBar extends JComponent {
  public static final float LEFT = 0.0f;
  public static final float CENTER = 0.5f;
  public static final float RIGHT = 1.0f;

  private int spaceBefore;
  private int spaceAfter;
  private float value;
  private float maxValue = 100;
  private String textValue;

  private Color color;

  private boolean preferredSizeSet = false;

  private String prefix = "";
  private String suffix = "%";
  private boolean valueDisplayed = true;
  private int precision = 100;  // in fact, it's 10^precision
  private boolean zeroShown = false;
  private float textAlignmentX = 0.5f;
  private boolean shouldRoundValue = false;
  private boolean centered = false;
  private boolean leftJustified = false;


  public HistogramBar() {
    color = UIManager.getColor("Label.foreground");
  }

  public void setPreferredSize(Dimension dim) {
    super.setPreferredSize(dim);
    preferredSizeSet = true;
  }

  public Dimension getPreferredSize() {
    if (preferredSizeSet)
      return super.getPreferredSize();

    int height = (int)this.getFont().getLineMetrics("Ag", ((Graphics2D)this.getGraphics()).getFontRenderContext()).getHeight() + spaceBefore + spaceAfter;
    return new Dimension(150, height);
  }

  public void setValue(float value) {
    this.value = value;

    if (precision == 1)
       textValue = prefix + Math.round(value) + suffix;
    else
      textValue = prefix + ((float)Math.round(value*precision))/precision + suffix;
    repaint();
  }

  public float getValue() {
    return value;
  }

  public void setMaxValue(float value) {
    maxValue = value;
    repaint();
  }

  public float getMaxValue() {
    return maxValue;
  }

  public void setSpaceBefore(int space) {
    spaceBefore = space;
    repaint();
  }
  public void setSpaceAfter(int space) {
    spaceAfter = space;
    repaint();
  }
  public int getSpaceBefore() {
    return spaceBefore;
  }

  public int getSpaceAfter() {
    return spaceAfter;
  }

  public void setBarColor(Color color) {
    this.color = color;
    repaint();
  }

  public Color getBarColor() {
    return color;
  }

  public void setValueDisplayed(boolean b) {
    valueDisplayed = b;
    repaint();
  }

  public boolean isValueDisplayed() {
    return valueDisplayed;
  }

  public void setShowZeroValue(boolean b) {
  	setZeroShown(b);
  }

  public void setZeroShown(boolean b) {
    zeroShown = b;
    repaint();
  }

  public boolean showsZeroValue() {
  	return isZeroShown();
  }
  
  public boolean isZeroShown() {
     return zeroShown;
  }

  public void setPrecision(int precision) {
    this.precision = 1;
    for (int i = precision; i > 0; i--) {
      this.precision *= 10;
    }
    setValue(value);
  }

  public int getPrecision() {
    int numberOfZero = 0;
    int multiplicator = precision;
    while (multiplicator > 1) {
      multiplicator /= 10;
      numberOfZero++;
    }
     return numberOfZero;
  }

  public void setTextAlignmentX(float value) {
    textAlignmentX = value;
    repaint();
  }

  public float getTextAlignmentX() {
    return textAlignmentX;
  }

  /**
   * Sets the text displays before the value in the bar
   */
  public void setPrefix(String prefix) {
    this.prefix = prefix;
    setValue(value); // to redraw the text
  }

  /**
   * Returns the text displays before the value in the bar
   */
  public String getPrefix() {
    return prefix;
  }

  /**
   * Sets the text displays after the value in the bar
   */
  public void setSuffix(String suffix) {
    this.suffix = suffix;
    setValue(value); // to redraw the text
  }

  public void setRoundsValue(boolean b) {
    shouldRoundValue = b;
    setValue(value);
  }

  public boolean roundsValue() {
     return shouldRoundValue;
  }

  public void setCentered(boolean b) {
    centered = b;
    repaint();
  }

  public void setLeftJustified(boolean b) {
    leftJustified = b;
    repaint();
  }

  public boolean isCentered() {
     return centered;
  }

  public boolean isLeftJustified() {
     return leftJustified;
  }


  /**
   * Returns the text displays after the value in the bar
   */
  public String getSuffix() {
    return suffix;
  }

  public void paintComponent(Graphics gc) {
    int barHeight = getHeight()-(spaceBefore+spaceAfter);

    gc.setColor(getBarColor());
    gc.fillRect(0, spaceBefore, Math.round(getWidth()*value/maxValue), barHeight);

    gc.setColor(getForeground());
    gc.drawLine(0, 0, 0, getHeight());
    gc.drawLine(getWidth()-1, 0, getWidth()-1, getHeight());

    if (valueDisplayed && (zeroShown || (value != 0))) {
      FontMetrics fm = gc.getFontMetrics(gc.getFont());
      int y = spaceBefore + (barHeight-fm.getHeight()) /2 + fm.getAscent();
      //int maxWidth = getWidth()-fm.stringWidth(textValue);
      //int x = Math.round(getTextAlignmentX() * maxWidth);
      int x;
      if (centered)
         x = (getWidth()-fm.stringWidth(textValue))/2;
      else
      if (leftJustified)
         x = 10;
      else
         x = (getWidth()-fm.stringWidth(textValue)- 10);

      gc.drawString(textValue, x, y);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -