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

📄 utilitiesmeter.java

📁 This code is trend chart for realtime monitor
💻 JAVA
字号:
/**
 *
 *  $Id: CpuMeter.java,v 1.4 2006/08/14 10:03:16 liulidong Exp $
 *
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Vector;

public class UtilitiesMeter
  extends java.awt.Canvas //Panel
{
	/**  interface bandwidth 在使用前在外部已经被赋初始值100*/
	private int iBandWidth;
	/** currentBandWidth 默认值为0*/
	private int iCurrentBandWidth = 0;
	/** utilized Rate */
	private int ur = 0;
	/** 占的比例 的格数 */
	private int count = 0;
	/** y轴的最大值also is bandwidth, default value 100M*/
	private int iMaxY = 100;
	/** boolean input */
	private boolean input;
	/** color of the chart's background */
	Color m_clrBkgnd = SystemColor.control;
	/** color of the chart's grid */
	Color m_clrGrid = SystemColor.controlShadow;

	/** # of horizontals in the grid */
	int m_iHors = 50;
	/** # of verticals in the grid */
	int m_iVers = 2;

  public UtilitiesMeter()
  {
    setColors(null, null, null);
    setFont(new Font("Dialog", Font.PLAIN, 11));
  }
  
  public void setColors(Color clrBkgnd, Color clrGrid, Color clrHighlight)
  {
    if(clrBkgnd != null)
	    m_clrBkgnd = clrBkgnd;
    if(clrGrid != null)
	    m_clrGrid = clrGrid;
	  setBackground(m_clrBkgnd);
  }

  /**
   * 设置带宽
   * @param bandWidth
   */
  public void setBandWidth(int bandWidth){
	  this.iBandWidth = bandWidth;
  }
 
  /**
   * 加入新的数据,并刷新图形
   * @param tuple 代表当前占用的带宽
   */
  public void addReading(int currentBandWidth)
  {

	  /**
	   * 对绘图所使用到的数据进行赋值
	   */
	  this.iCurrentBandWidth = currentBandWidth;
	  count = m_iHors * currentBandWidth / iBandWidth;
	  ur = (currentBandWidth* 100) / iBandWidth ;
  	  paint(getGraphics());
  }
  
  /**
   * override update to *not* erase the background 
   * before painting
   * 这些是继承的方法
   */
  public void update(Graphics g) 
  {
    paint(g);
  }
  /** offscreen drawing support */
  Image m_imageOffscr;
  /**
   * null out the offscreen buffer as part of invalidation
   * 该函数标志当前的component为无效
   * This component and all parents
   * above it are marked as needing to be laid out.
   */
  public void invalidate() 
  {
    super.invalidate();
    if(m_imageOffscr != null) {
      m_imageOffscr.flush();
      m_imageOffscr = null;
    }
  }
  
  /** 
   * 覆盖该方法,绘制组件
   */
	public synchronized void paint(Graphics g)
	{
	  Dimension dim = getSize();
	  if(m_imageOffscr == null)
	  {
      m_imageOffscr = createImage(dim.width, dim.height);
	  }
	  Image imageOffscr = m_imageOffscr;
	  if(imageOffscr != null) {
	    Graphics og = imageOffscr.getGraphics();
      paintInternal(og, dim);
      g.drawImage(imageOffscr, 0, 0, null);
      og.dispose();
    }
	}
  
	private void paintInternal(Graphics g, Dimension dimChart)
	{
		// clear background
	  g.setColor(m_clrBkgnd);
	  g.fillRect(0, 0, dimChart.width, dimChart.height);

    //g.setFont(m_font);
    FontMetrics fm = g.getFontMetrics();
	  int iHeaderFooterHeight = fm.getAscent();
	  int iLegendYWidth = fm.stringWidth("10000M");
	  int iRightGapWidth = 2;//iHeaderFooterHeight;
    
	  int dy = (dimChart.height - iHeaderFooterHeight) / m_iHors;
	  int iDataHeight = m_iHors * dy;
	  int iDataTop = (dimChart.height - iDataHeight) / 2;
	  int iDataBot = iDataTop + iDataHeight;
	  //dx是x轴 每个单位刻度的长度
	  int dx = (dimChart.width - iLegendYWidth - iRightGapWidth) / m_iVers;
	  int iDataWidth = dx * m_iVers;
	  int iDataLeft = iLegendYWidth;
	  int iDataRight = iDataLeft + iDataWidth;
	  int iGridLeft = iDataLeft;

    // draw the grid
	  g.setColor(m_clrGrid);
	  for(int y = iDataTop; y <= iDataBot; y += dy)
      g.drawLine(iDataLeft, y, iDataRight, y);
	  for(int x = iGridLeft; x <= iDataRight; x += dx)
      g.drawLine(x, iDataTop, x, iDataTop + iDataHeight);
	  // draw Y-axis legend (图例)
	  //y-axis 
	  {
	    iMaxY = iBandWidth;
	    String scale = "";
	    if(iMaxY >= 10000000) {
	      iMaxY /= 1000000;
	      scale = "M";
	    } else if(iMaxY >= 10000) {
	      iMaxY /= 1000;
	      scale = "K";
	    }
	    drawStringRightAligned(g, fm, iMaxY+scale+" ", iDataLeft, iDataTop + (iHeaderFooterHeight / 2));
	    drawStringRightAligned(g, fm, (iMaxY / 2)+scale+" ", iDataLeft, (iDataBot + iDataTop + iHeaderFooterHeight)/2);
	    drawStringRightAligned(g, fm, "0 ", iDataLeft, iDataBot + (iHeaderFooterHeight / 2));
	  }
/**
 * 填充相应的区域,画出当前的使用率
 */
	  
	  if(input){//如果是输入,绘成蓝色
		  g.setColor(new java.awt.Color(102,153,255));
		  for(int i = 0;i<count;i++){
			  //绘制左边的格
			  g.fillRect(iGridLeft,iDataBot - dy*i,dx - 1,dy - 1); 
			  //绘制右边的格  
			  g.fillRect(iGridLeft + dx +1,iDataBot - dy*i,dx - 1,dy - 1);
		  }
	  }else{
		  g.setColor(Color.green);
		  for(int i = 0;i<count;i++){
			  //绘制左边的格
			  g.fillRect(iGridLeft,iDataBot - dy*i,dx - 1,dy - 1); 
			  //绘制右边的格  
			  g.fillRect(iGridLeft + dx +1,iDataBot - dy*i,dx - 1,dy - 1);
		  }
	  }
/**画使用率 100% */
	  String utilizedRate = String.valueOf(ur) + '%';
	  g.setColor(Color.black);
	  g.drawString(utilizedRate,iGridLeft + 2*dx/6,iDataBot + 4*dy);
	}

  private void drawStringRightAligned(Graphics g, FontMetrics fm, String str, int x, int y)
  {
	  int iWidth = fm.stringWidth(str);
	  g.drawString(str, x - iWidth, y);
  }
  public void setInput(boolean input){
	  this.input = input;
  }
}

⌨️ 快捷键说明

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