📄 systemmonitor.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.gui;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.GeneralPath;
public class SystemMonitor extends JPanel {
private static final Color MEMORY_COLOR = new Color(0,200,0); //,128);
private static final Color TEXT_COLOR = new Color(0,220,0);
private static final Color GRID_COLOR = Color.gray;
private static final Color BACKGROUND = new Color(0,60,0);
private static final String[] MEMORY_UNITS = { "b", "kB", "MB", "GB", "TB" };
private static final int NUMBER_OF_MEASUREMENTS = 20;
private static final int GRID_X = 10;
private static final int GRID_Y = 10;
private static final int MARGIN = 10;
private long delay = 1000;
private long[] memory = new long[NUMBER_OF_MEASUREMENTS];
private int currentMeasurement = 0;
public SystemMonitor() {
setBackground(BACKGROUND);
new Thread() {
{
setDaemon(true);
}
public void run() {
setPriority(MIN_PRIORITY);
while (true) {
SystemMonitor.this.memory[SystemMonitor.this.currentMeasurement] =
Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
SystemMonitor.this.currentMeasurement =
(SystemMonitor.this.currentMeasurement+1) %
SystemMonitor.this.memory.length;
SystemMonitor.this.repaint();
try {
sleep(delay);
} catch (InterruptedException e) {
}
}
}
}.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
GeneralPath path = new GeneralPath();
Dimension d = getSize();
int monitorWidth = (int)d.getWidth() - 2*MARGIN;
int monitorHeight = (int)d.getHeight() - 2*MARGIN;
long total = Runtime.getRuntime().totalMemory();
path.moveTo(MARGIN, MARGIN+monitorHeight);
for (int i = 0; i < memory.length; i++) {
int index = (currentMeasurement + i) % memory.length;
path.lineTo(MARGIN + i * monitorWidth / (memory.length-1),
MARGIN + monitorHeight - monitorHeight*memory[index]/total);
}
path.lineTo(MARGIN + monitorWidth,
MARGIN + monitorHeight);
path.closePath();
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(GRID_COLOR);
for (int x = 0; x < GRID_X+1; x++) {
g2d.drawLine(MARGIN + x * monitorWidth / GRID_X, MARGIN,
MARGIN + x * monitorWidth / GRID_X, MARGIN + monitorHeight);
}
for (int y = 0; y < GRID_Y+1; y++) {
g2d.drawLine(MARGIN, MARGIN + y * monitorHeight / GRID_Y,
MARGIN + monitorWidth, MARGIN + y * monitorHeight / GRID_Y);
}
g2d.setColor(MEMORY_COLOR);
g2d.fill(path);
g2d.setColor(Color.green);
g2d.draw(path);
g2d.setColor(TEXT_COLOR);
Font font = new Font("Courier", Font.PLAIN, 12);
g2d.setFont(font);
g2d.drawString(" Total: " + humanReadable(total), MARGIN, MARGIN+3*font.getSize()/2);
g2d.drawString(" Max: " + humanReadable(Runtime.getRuntime().maxMemory()), MARGIN, MARGIN + 5*font.getSize()/2);
}
private String humanReadable(long bytes) {
long result = bytes;
long rest = 0;
int unit = 0;
while (result > 1024) {
rest = result % 1024;
result /= 1024;
unit++;
if (unit >= MEMORY_UNITS.length-1) break;
}
if ((result < 10) && (unit > 0)) {
return result+"."+(10*rest/1024)+" "+MEMORY_UNITS[unit];
} else {
return result+" "+MEMORY_UNITS[unit];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -