package test; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.text.MessageFormat; import java.util.TimerTask; import java.util.Timer; import javax.swing.*; public class Memory extends JComponent { private boolean gc_running = false; java.util.Timer timer; private final class ActionHandler implements ActionListener { public void actionPerformed(ActionEvent actionevent) { update(); } private ActionHandler() { } } private final class HeapGrowTimer extends javax.swing.Timer { public float getPercent() { return percent; } protected void fireActionPerformed(ActionEvent actionevent) { long l = System.currentTimeMillis(); long l1 = Math.max(0L, l - startTime); if (l1 > 1000L) { stopHeapAnimate(); } else { percent = (float) l1 / 1000F; repaint(); } } private final long startTime = System.currentTimeMillis(); private float percent; BufferedImage image; HeapGrowTimer() { super(30, null); setRepeats(true); percent = 0.0F; int i = getWidth() - 2; int j = getHeight() - 4; image = new BufferedImage(i, j, 2); Graphics2D graphics2d = image.createGraphics(); paintTicks(graphics2d, i, j); graphics2d.dispose(); } } public Memory() { byte byte0 = 3; byte byte1 = 3; float f = 0.1F; float af[] = new float[byte0 * byte1]; for (int i = 0; i < af.length; i++) { af[i] = f; } blur = new ConvolveOp(new Kernel(byte0, byte1, af)); heapSizeText = ""; enableEvents(16L); updateUI(); startTimer(); } private void startTimer() { timer = new Timer(); timer.schedule(new GCTask(), 3 * 60 * 1000, 3 * 60 * 1000); } private class GCTask extends TimerTask { public void run() { //System.out.print("start... "); gc_running = true; long free = 0; while (free < Runtime.getRuntime().freeMemory()) { free = Runtime.getRuntime().freeMemory(); Runtime.getRuntime().runFinalization(); Runtime.getRuntime().gc(); } gc_running = false; //System.out.println("stop!"); } } public boolean isOpaque() { return true; } public void updateUI() { Font font = (new JLabel()).getFont(); font = new Font(font.getName(), 1, font.getSize()); setFont(font); revalidate(); repaint(); } public void setFont(Font font) { super.setFont(font); updateTextWidth(); } Dimension heapViewPreferredSize() { Dimension dimension = new Dimension(maxTextWidth + 8, getFontMetrics(getFont()).getHeight() + 8); return dimension; } private void updateTextWidth() { String s = format.format(((Object) (new Object[]{ new Float(999.9F), new Float(999.9F) }))); maxTextWidth = getFontMetrics(getFont()).stringWidth(s) + 4; } protected void processMouseEvent(MouseEvent mouseevent) { super.processMouseEvent(mouseevent); if (!mouseevent.isConsumed()) { if (mouseevent.isPopupTrigger()) { } else { MouseEvent _tmp = mouseevent; if (mouseevent.getID() == 504) { containsMouse = true; cachedBorderVaild = false; repaint(); } else { MouseEvent _tmp1 = mouseevent; if (mouseevent.getID() == 505) { containsMouse = false; cachedBorderVaild = false; repaint(); } } } } if (mouseevent.getID() == 500 && SwingUtilities.isLeftMouseButton(mouseevent) && mouseevent.getClickCount() == 1) { if (!gc_running) { Runtime.getRuntime().gc(); } } } private int getGraphStartIndex() { if (graphFilled) { return graphIndex; } else { return 0; } } protected void paintComponent(Graphics g) { Graphics2D graphics2d = (Graphics2D) g; int i = getWidth(); int j = getHeight(); if (i - 2 > 0 && j - 4 > 0) { startTimerIfNecessary(); updateCacheIfNecessary(i, j); paintCachedBackground(graphics2d, i, j); g.translate(1, 2); if (containsMouse) { g.clipRect(1, 0, i - 4, j - 4); } else { g.clipRect(0, 0, i - 2, j - 4); } int k = i - 2; int l = j - 4; if (heapGrowTimer != null) { java.awt.Composite composite = ((Graphics2D) g).getComposite(); float f = 1.0F - heapGrowTimer.getPercent(); ((Graphics2D) g).setComposite(AlphaComposite.getInstance(3, f)); g.drawImage(heapGrowTimer.image, 0, 0, null); ((Graphics2D) g).setComposite(composite); } paintTicks(graphics2d, k, l); if (true) { graphics2d.drawImage(getGridOverlayImage(), 0, 0, null); } if (true) { if (true) { paintDropShadowText(g, k, l); } else { g.setColor(Color.WHITE); paintText(g, k, l); } } g.translate(-1, -2); } else { stopTimerIfNecessary(); g.setColor(getBackground()); g.fillRect(0, 0, i, j); } } private void paintTicks(Graphics2D graphics2d, int i, int j) { if (graphIndex > 0 || graphFilled) { int k = getGraphStartIndex(); int l = 0; if (!graphFilled) { l = i - graphIndex; } float f = graph[k]; for (k = (k + 1) % graph.length; k != graphIndex; k = (k + 1) % graph.length) { f = Math.min(f, graph[k]); } int i1 = (int) (f * (float) j); if (i1 > 0) { graphics2d.drawImage(tickGradientImage, l, j - i1, i, j, l, j - i1, i, j, null); } k = getGraphStartIndex(); do { int j1 = (int) (graph[k] * (float) j); if (j1 > i1) { graphics2d.drawImage(tickGradientImage, l, j - j1, l + 1, j - i1, l, j - j1, l + 1, j - i1, null); } k = (k + 1) % graph.length; l++; } while (k != graphIndex); } } private void paintText(Graphics g, int i, int j) { g.setFont(getFont()); String s = getHeapSizeText(); FontMetrics fontmetrics = g.getFontMetrics(); int k = fontmetrics.stringWidth(s); g.drawString(s, (i - maxTextWidth) / 2 + (maxTextWidth - k), j / 2 + fontmetrics.getAscent() / 2); } private void paintDropShadowText(Graphics g, int i, int j) { if (textImage == null) { textImage = new BufferedImage(i, j, 2); dropShadowImage = new BufferedImage(i, j, 2); } Graphics2D graphics2d = textImage.createGraphics(); graphics2d.setComposite(AlphaComposite.Clear); graphics2d.fillRect(0, 0, i, j); graphics2d.setComposite(AlphaComposite.SrcOver); graphics2d.setColor(TEXT_BLUR_COLOR); paintText(graphics2d, i, j); graphics2d.dispose(); Graphics2D graphics2d1 = dropShadowImage.createGraphics(); graphics2d1.setComposite(AlphaComposite.Clear); graphics2d1.fillRect(0, 0, i, j); graphics2d1.setComposite(AlphaComposite.SrcOver); graphics2d1.drawImage(textImage, blur, 0, 1); graphics2d1.setColor(TEXT_COLOR); graphics2d1.setFont(getFont()); paintText(graphics2d1, i, j); graphics2d1.dispose(); g.drawImage(dropShadowImage, 0, 0, null); } private String getHeapSizeText() { return heapSizeText; } private void paintGridOverlay(Graphics2D graphics2d, int i, int j) { int k = GRID_COLORS.length / 2; int l = (j - k - 1) / k; int i1 = 0xd0ccbc; int j1 = 0xeae7d7; graphics2d.setPaint(new GradientPaint(0.0F, 0.0F, new Color(i1 >> 16 & 0xff, i1 >> 8 & 0xff, i1 & 0xff, 48), 0.0F, j, new Color(j1 >> 16 & 0xff, j1 >> 8 & 0xff, j1 & 0xff, 64))); for (int k1 = 0; k1 < i; k1 += l + 1) { graphics2d.fillRect(k1, 0, 1, j); } for (int l1 = j - l - 1; l1 >= 0; l1 -= l + 1) { graphics2d.fillRect(0, l1, i, 1); } } private void paintCachedBackground(Graphics2D graphics2d, int i, int j) { if (bgImage != null) { graphics2d.drawImage(bgImage, 0, 0, null); } } private void paintBackgroundTiles(Graphics2D graphics2d, int i, int j) { graphics2d.translate(1, 2); i -= 2; j -= 4; int k = GRID_COLORS.length / 2; int l = (j - k - 1) / k; for (int i1 = 0; i1 < k; i1++) { int j1 = i1; int k1 = j - l * (i1 + 1) - i1; int l1 = 1; graphics2d.setPaint(new GradientPaint(0.0F, k1, GRID_COLORS[j1 * 2], 0.0F, (k1 + l) - 1, GRID_COLORS[j1 * 2 + 1])); int i2; for (; l1 < i; l1 = i2 + 1) { i2 = Math.min(i, l1 + l); graphics2d.fillRect(l1, k1, i2 - l1, l); } k1 += l + 1; } graphics2d.translate(-1, -2); } private void paintBackground(Graphics2D graphics2d, int i, int j) { graphics2d.setPaint(new GradientPaint(0.0F, 0.0F, BACKGROUND1_COLOR, 0.0F, j, BACKGROUND2_COLOR)); graphics2d.fillRect(0, 0, i, j); } private void paintBorder(Graphics g, int i, int j) { if (containsMouse) { g.setColor(Color.WHITE); g.drawRect(0, 0, i - 1, j - 1); g.drawRect(1, 1, i - 3, j - 3); } else { g.setColor(BORDER1_COLOR); g.drawRect(0, 0, i - 1, j - 2); g.setColor(BORDER2_COLOR); g.fillRect(1, 1, i - 2, 1); g.setColor(Color.WHITE); g.fillRect(0, j - 1, i, 1); } } private void updateCacheIfNecessary(int i, int j) { if (cachedWidth != i || cachedHeight != j || !cachedBorderVaild) { cachedWidth = i; cachedHeight = j; cachedBorderVaild = true; updateCache(i, j); } } private Image getGridOverlayImage() { if (gridOverlayImage == null) { gridOverlayImage = new BufferedImage(getInnerWidth(), getInnerHeight(), 2); Graphics2D graphics2d = gridOverlayImage.createGraphics(); paintGridOverlay(graphics2d, getInnerWidth(), getInnerHeight()); graphics2d.dispose(); } return gridOverlayImage; } private void updateCache(int i, int j) { disposeImages(); textImage = new BufferedImage(i, j, 2); dropShadowImage = new BufferedImage(i, j, 2); bgImage = createImage(i, j); Graphics2D graphics2d = (Graphics2D) bgImage.getGraphics(); paintBackground(graphics2d, i, j); paintBackgroundTiles(graphics2d, i, j); paintBorder(graphics2d, i, j); graphics2d.dispose(); i -= 2; j -= 4; if (graph == null || graph.length != i) { graph = new float[i]; graphFilled = false; graphIndex = 0; } GradientPaint gradientpaint = new GradientPaint(0.0F, j, MIN_TICK_COLOR, i, 0.0F, MAX_TICK_COLOR); tickGradientImage = createImage(i, j); graphics2d = (Graphics2D) tickGradientImage.getGraphics(); graphics2d.setPaint(gradientpaint); graphics2d.fillRect(0, 0, i, j); graphics2d.dispose(); if (gridOverlayImage != null) { gridOverlayImage.flush(); gridOverlayImage = null; } } public void removeNotify() { super.removeNotify(); stopTimerIfNecessary(); } private void startTimerIfNecessary() { if (!AUTOMATIC_REFRESH) { return; } if (updateTimer == null) { updateTimer = new javax.swing.Timer(1500, new ActionHandler()); updateTimer.setRepeats(true); updateTimer.start(); } } private void stopTimerIfNecessary() { if (updateTimer != null) { graph = null; graphFilled = false; updateTimer.stop(); updateTimer = null; lastTotal = 0L; disposeImages(); cachedHeight = cachedHeight = -1; if (heapGrowTimer != null) { heapGrowTimer.stop(); heapGrowTimer = null; } } } private void disposeImages() { if (bgImage != null) { bgImage.flush(); bgImage = null; } if (textImage != null) { textImage.flush(); textImage = null; } if (dropShadowImage != null) { dropShadowImage.flush(); dropShadowImage = null; } if (tickGradientImage != null) { tickGradientImage.flush(); tickGradientImage = null; } if (gridOverlayImage != null) { gridOverlayImage.flush(); gridOverlayImage = null; } } private void update() { if (!isShowing()) { stopTimerIfNecessary(); return; } Runtime runtime = Runtime.getRuntime(); long l = runtime.totalMemory(); if (l != lastTotal) { if (lastTotal != 0L) { startHeapAnimate(); int i = getGraphStartIndex(); do { graph[i] = (float) (((double) graph[i] * (double) lastTotal) / (double) l); i = (i + 1) % graph.length; } while (i != graphIndex); } lastTotal = l; } if (heapGrowTimer == null) { long l1 = l - runtime.freeMemory(); graph[graphIndex] = (float) ((double) l1 / (double) l); graphIndex = (graphIndex + 1) % graph.length; if (graphIndex == 0) { graphFilled = true; } heapSizeText = format.format(((Object) (new Object[]{ new Double((double) l1 / 1024D / 1024D), new Double((double) l / 1024D / 1024D) }))); } repaint(); } private void startHeapAnimate() { if (heapGrowTimer == null) { heapGrowTimer = new HeapGrowTimer(); heapGrowTimer.start(); } } private void stopHeapAnimate() { if (heapGrowTimer != null) { heapGrowTimer.stop(); heapGrowTimer = null; } } private int getInnerWidth() { return getWidth() - 2; } private int getInnerHeight() { return getHeight() - 4; } private static final boolean AUTOMATIC_REFRESH = System.getProperty("org.netbeans.log.startup") == null; private static final Color GRID_COLORS[] = { new Color(0xe3dfcf), new Color(0xe7e4d3), new Color(0xdad7c6), new Color(0xdfdccb), new Color(0xd3cfbf), new Color(0xd7d3c3), new Color(0xcecaba), new Color(0xd0ccbc) }; private static final Color BORDER1_COLOR = new Color(0xa6a295); private static final Color BORDER2_COLOR = new Color(0xc0bcad); private static final Color MIN_TICK_COLOR = new Color(0xc7d6ad); private static final Color MAX_TICK_COLOR = new Color(0x615d0f); private static final Color TEXT_BLUR_COLOR; private static final Color TEXT_COLOR; private static final Color BACKGROUND1_COLOR = new Color(0xd0ccbc); private static final Color BACKGROUND2_COLOR = new Color(0xeae7d7); private final ConvolveOp blur; private final MessageFormat format = new MessageFormat("{0,choice,0#{0,number,0.0}|999<{0,number,0}}/{1,choice,0#{1,number,0.0}|999<{1,number,0}}MB"); //private final MessageFormat format = new MessageFormat("{0,number,0.0}/{1,number,0.0}MB"); private float graph[]; private int graphIndex; private boolean graphFilled; private long lastTotal; private javax.swing.Timer updateTimer; private Image bgImage; private int cachedWidth; private int cachedHeight; private BufferedImage textImage; private BufferedImage dropShadowImage; private HeapGrowTimer heapGrowTimer; private int maxTextWidth; private String heapSizeText; private Image tickGradientImage; private BufferedImage gridOverlayImage; private boolean containsMouse; private boolean cachedBorderVaild; static { TEXT_BLUR_COLOR = Color.WHITE; TEXT_COLOR = Color.WHITE; } }