home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Java2D / MemoryMonitor.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  8.5 KB  |  259 lines

  1. /*
  2.  * @(#)MemoryMonitor.java    1.24 98/09/13
  3.  * 
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import java.awt.*;
  16. import java.awt.event.*;
  17. import java.awt.image.BufferedImage;
  18. import java.awt.geom.Line2D;
  19. import java.awt.geom.Rectangle2D;
  20. import javax.swing.JPanel;
  21. import javax.swing.border.EtchedBorder;
  22. import javax.swing.border.TitledBorder;
  23.  
  24.  
  25. /**
  26.  * Tracks Memory allocated & used, displayed in graph form.
  27.  */
  28. public class MemoryMonitor extends JPanel {
  29.  
  30.     public MonitorComponent mc;
  31.  
  32.     public MemoryMonitor() {
  33.         setLayout(new BorderLayout());
  34.         setBorder(new TitledBorder(new EtchedBorder(), "Memory Monitor"));
  35.         mc = new MonitorComponent();
  36.         add(mc);
  37.     }
  38.  
  39.  
  40.     public class MonitorComponent extends JPanel implements Runnable
  41.     {
  42.         public Thread thread;
  43.         private int w, h;
  44.         private BufferedImage bimg;
  45.         private Graphics2D big;
  46.         private Font font = new Font("Times New Roman", Font.PLAIN, 11);
  47.         private Runtime r = Runtime.getRuntime();
  48.         private int columnInc;
  49.         private int pts[];
  50.         private int ptNum;
  51.         private int ascent, descent;
  52.         private float freeMemory, totalMemory;
  53.         private Rectangle graphOutlineRect = new Rectangle();
  54.         private Rectangle2D mfRect = new Rectangle2D.Float();
  55.         private Rectangle2D muRect = new Rectangle2D.Float();
  56.         private Line2D graphLine = new Line2D.Float();
  57.         private Color graphColor = new Color(46, 139, 87);
  58.         private Color mfColor = new Color(0, 100, 0);
  59.  
  60.  
  61.         public MonitorComponent() {
  62.             setBackground(Color.black);
  63.             addMouseListener(new MouseAdapter() {
  64.                 public void mouseClicked(MouseEvent e) {
  65.                     if (thread == null) start(); else stop();
  66.                 }
  67.             });
  68.             start();
  69.         }
  70.  
  71.         public Dimension getMinimumSize() {
  72.             return getPreferredSize();
  73.         }
  74.  
  75.         public Dimension getMaximumSize() {
  76.             return getPreferredSize();
  77.         }
  78.  
  79.         public Dimension getPreferredSize() {
  80.             return new Dimension(140,80);
  81.         }
  82.  
  83.             
  84.         public void paint(Graphics g) {
  85.  
  86.             if (big == null) {
  87.                 return;
  88.             }
  89.  
  90.             big.setBackground(getBackground());
  91.             big.clearRect(0,0,w,h);
  92.  
  93.             float freeMemory = (float) r.freeMemory();
  94.             float totalMemory = (float) r.totalMemory();
  95.  
  96.             // .. Draw allocated and used strings ..
  97.             big.setColor(Color.green);
  98.             big.drawString(String.valueOf((int) totalMemory/1024) + "K allocated",  4.0f, (float) ascent+0.5f);
  99.             big.drawString(String.valueOf(((int) (totalMemory - freeMemory))/1024) + "K used", 4, h-descent);
  100.  
  101.             // Calculate remaining size
  102.             float ssH = ascent + descent;
  103.             float remainingHeight = (float) (h - (ssH*2) - 0.5f);
  104.             float blockHeight = remainingHeight/10;
  105.             float blockWidth = 20.0f;
  106.             float remainingWidth = (float) (w - blockWidth - 10);
  107.  
  108.             // .. Memory Free ..
  109.             big.setColor(mfColor);
  110.             int MemUsage = (int) ((freeMemory / totalMemory) * 10);
  111.             int i = 0;
  112.             for ( ; i < MemUsage ; i++) { 
  113.                 mfRect.setRect(5,(float) ssH+i*blockHeight,
  114.                                 blockWidth,(float) blockHeight-1);
  115.                 big.fill(mfRect);
  116.             }
  117.  
  118.             // .. Memory Used ..
  119.             big.setColor(Color.green);
  120.             for ( ; i < 10; i++)  {
  121.                 muRect.setRect(5,(float) ssH+i*blockHeight,
  122.                                 blockWidth,(float) blockHeight-1);
  123.                 big.fill(muRect);
  124.             }
  125.  
  126.             // .. Draw History Graph ..
  127.             big.setColor(graphColor);
  128.             int graphX = 30;
  129.             int graphY = (int) ssH;
  130.             int graphW = w - graphX - 5;
  131.             int graphH = (int) remainingHeight;
  132.             graphOutlineRect.setRect(graphX, graphY, graphW, graphH);
  133.             big.draw(graphOutlineRect);
  134.  
  135.             int graphRow = graphH/10;
  136.  
  137.             // .. Draw row ..
  138.             for (int j = graphY; j <= graphH+graphY; j += graphRow) {
  139.                 graphLine.setLine(graphX,j,graphX+graphW,j);
  140.                 big.draw(graphLine);
  141.             }
  142.         
  143.             // .. Draw animated column movement ..
  144.             int graphColumn = graphW/15;
  145.  
  146.             if (columnInc == 0) {
  147.                 columnInc = graphColumn;
  148.             }
  149.  
  150.             for (int j = graphX+columnInc; j < graphW+graphX; j+=graphColumn) {
  151.                 graphLine.setLine(j,graphY,j,graphY+graphH);
  152.                 big.draw(graphLine);
  153.             }
  154.  
  155.             --columnInc;
  156.  
  157.             if (pts == null) {
  158.                 pts = new int[graphW];
  159.                 ptNum = 0;
  160.             } else if (pts.length != graphW) {
  161.                 int tmp[] = null;
  162.                 if (ptNum < graphW) {     
  163.                     tmp = new int[ptNum];
  164.                     System.arraycopy(pts, 0, tmp, 0, tmp.length);
  165.                 } else {        
  166.                     tmp = new int[graphW];
  167.                     System.arraycopy(pts, pts.length-tmp.length, tmp, 0, tmp.length);
  168.                     ptNum = tmp.length - 2;
  169.                 }
  170.                 pts = new int[graphW];
  171.                 System.arraycopy(tmp, 0, pts, 0, tmp.length);
  172.             } else {
  173.                 big.setColor(Color.yellow);
  174.                 pts[ptNum] = (int)(graphY+graphH*(freeMemory/totalMemory));
  175.                 for (int j=graphX+graphW-ptNum, k=0;k < ptNum; k++, j++) {
  176.                     if (k != 0) {
  177.                         if (pts[k] != pts[k-1]) {
  178.                             big.drawLine(j-1, pts[k-1], j, pts[k]);
  179.                         } else {
  180.                             big.fillRect(j, pts[k], 1, 1);
  181.                         }
  182.                     }
  183.                 }
  184.                 if (ptNum+2 == pts.length) {
  185.                     // throw out oldest point
  186.                     for (int j = 1;j < ptNum; j++) {
  187.                         pts[j-1] = pts[j];
  188.                     }
  189.                     --ptNum;
  190.                 } else {
  191.                     ptNum++;
  192.                 }
  193.             }
  194.             g.drawImage(bimg, 0, 0, this);
  195.         }
  196.  
  197.  
  198.         public void start() {
  199.             thread = new Thread(this);
  200.             thread.setPriority(Thread.MIN_PRIORITY);
  201.             thread.setName("MemoryMonitor");
  202.             thread.start();
  203.         }
  204.  
  205.  
  206.         public synchronized void stop() {
  207.             thread = null;
  208.             notify();
  209.         }
  210.  
  211.  
  212.         public void run() {
  213.  
  214.             Thread me = Thread.currentThread();
  215.  
  216.             while (thread == me && !isShowing() || getSize().width == 0) {
  217.                 try {
  218.                     Thread.sleep(500);
  219.                 } catch (InterruptedException e) { thread = null; return; }
  220.             }
  221.  
  222.             while (thread == me && isShowing()) {
  223.                 Dimension d = getSize();
  224.                 if (d.width != w || d.height != h) {
  225.                     w = d.width;
  226.                     h = d.height;
  227.                     bimg = (BufferedImage) createImage(w, h);
  228.                     big = bimg.createGraphics();
  229.                     big.setFont(font);
  230.                     FontMetrics fm = big.getFontMetrics(font);
  231.                     ascent = (int) fm.getAscent();
  232.                     descent = (int) fm.getDescent();
  233.                 }
  234.                 repaint();
  235.                 try {
  236.                     thread.sleep(999);
  237.                 } catch (InterruptedException e) { break; }
  238.             }
  239.             thread = null;
  240.         }
  241.     }
  242.  
  243.  
  244.     public static void main(String s[]) {
  245.         final MemoryMonitor demo = new MemoryMonitor();
  246.         WindowListener l = new WindowAdapter() {
  247.             public void windowClosing(WindowEvent e) {System.exit(0);}
  248.             public void windowDeiconified(WindowEvent e) { demo.mc.start(); }
  249.             public void windowIconified(WindowEvent e) { demo.mc.stop(); }
  250.         };
  251.         Frame f = new Frame("Java2D Demo - MemoryMonitor");
  252.         f.addWindowListener(l);
  253.         f.add("Center", demo);
  254.         f.pack();
  255.         f.setSize(new Dimension(200,200));
  256.         f.show();
  257.     }
  258. }
  259.