/* * @(#)MemoryMonitor.java 1.16 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import com.sun.java.swing.JPanel; import com.sun.java.swing.border.EtchedBorder; import com.sun.java.swing.border.TitledBorder; /** * Tracks Memory allocated & used, displayed in graph form. */ public class MemoryMonitor extends JPanel { public MemoryMonitorCanvas mmc; public MemoryMonitor() { setLayout(new BorderLayout()); setBorder(new TitledBorder(new EtchedBorder(), "Memory Monitor")); mmc = new MemoryMonitorCanvas(); add(mmc); } public class MemoryMonitorCanvas extends Canvas implements Runnable { Thread thread; int w, h; BufferedImage offImg; Graphics2D offG; Font font = new Font("Times New Roman", Font.PLAIN, 11); Runtime r = Runtime.getRuntime(); int columnInc; int pts[]; int num_pts; int ascent, descent; public MemoryMonitorCanvas() { setBackground(Color.black); start(); } public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getMaximumSize() { return getPreferredSize(); } public Dimension getPreferredSize() { return new Dimension(140,80); } public void paint(Graphics g) { if (offG == null) return; offG.setBackground(getBackground()); offG.clearRect(0,0,w,h); float freeMemory = (float) r.freeMemory(); float totalMemory = (float) r.totalMemory(); // .. Draw allocated and used strings .. offG.setColor(Color.green); offG.drawString(String.valueOf((int) totalMemory/1024) + "K allocated", 4.0f, (float) ascent+0.5f); offG.drawString(String.valueOf(((int) (totalMemory - freeMemory))/1024) + "K used", 4, h-descent); // Calculate remaining size float ssH = ascent + descent; float remainingHeight = (float) (h - (ssH*2) - 0.5f); float blockHeight = remainingHeight/10; float blockWidth = 20.0f; float remainingWidth = (float) (w - blockWidth - 10); // .. Memory Free .. offG.setColor(new Color(0, 100, 0)); int MemUsage = (int) ((freeMemory / totalMemory) * 10); int i = 0; for ( ; i < MemUsage ; i++) { offG.fill(new Rectangle2D.Float(5,(float) ssH+i*blockHeight, blockWidth,(float) blockHeight-1)); } // .. Memory Used .. offG.setColor(Color.green); for ( ; i < 10; i++) { offG.fill(new Rectangle2D.Float(5,(float) ssH+i*blockHeight, blockWidth,(float) blockHeight-1)); } // .. Draw History Graph .. offG.setColor(new Color(46, 139, 87)); int graphX = 30; int graphY = (int) ssH; int graphW = w - graphX - 5; int graphH = (int) remainingHeight; Rectangle graphOutline = new Rectangle(graphX, graphY, graphW, graphH); offG.draw(graphOutline); int graphRow = graphH/10; // .. Draw row .. for (int j=graphY; j <= graphH+graphY;j+=graphRow) offG.draw(new Line2D.Float(graphX,j,graphX+graphW,j)); // .. Draw animated column movement .. int graphColumn = graphW/15; if (columnInc == 0) columnInc = graphColumn; for (int j=graphX+columnInc; j < graphW+graphX; j+= graphColumn) offG.draw(new Line2D.Float(j,graphY,j,graphY+graphH)); --columnInc; if (pts == null) { pts = new int[graphW]; num_pts = 0; } else { offG.setColor(Color.yellow); pts[num_pts] = (int) (graphY+ graphH * (freeMemory / totalMemory)); for (int j=graphX+graphW-num_pts, k = 0;k < num_pts; k++, j++) { if (k != 0) { if (pts[k] != pts[k-1]) offG.drawLine(j-1, pts[k-1], j, pts[k]); else offG.fillRect(j, pts[k], 1, 1); } } if (num_pts+2 == pts.length) { // throw out oldest point for (int j = 1;j < num_pts; j++) pts[j-1] = pts[j]; --num_pts; } else num_pts++; } g.drawImage(offImg, 0, 0, this); } public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("MemoryMonitor"); thread.start(); } public synchronized void stop() { thread = null; notify(); } public void run() { Thread me = Thread.currentThread(); while (thread == me && !isShowing() || getSize().width == 0) { try { Thread.sleep(999); } catch (InterruptedException e) { thread = null; return; } } while (thread == me && isShowing()) { if (getSize().width != w || getSize().height != h) { w = getSize().width; h = getSize().height; offImg = (BufferedImage) createImage(w, h); offG = offImg.createGraphics(); offG.setFont(font); FontMetrics fm = offG.getFontMetrics(font); ascent = (int) fm.getAscent(); descent = (int) fm.getDescent(); } Graphics g = getGraphics(); if (g == null) continue; paint(g); g.dispose(); try { thread.sleep(999); } catch (InterruptedException e) { break; } } thread = null; } } public static void main(String s[]) { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }; Frame f = new Frame("Java 2D Demo - MemoryMonitor"); f.addWindowListener(l); f.add("Center", new MemoryMonitor()); f.pack(); f.setSize(new Dimension(200,200)); f.show(); } }