📄 cpuandmemorydynamicchart.java
字号:
package shyhao.windows.explorer.start;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
import javax.swing.SpringLayout;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import shihao.windows.explorer.netpacket.NetPacketMonitor;
import shyhao.publicsources.LookAndFeel;
import shyhao.publicsources.MonitorData;
import shyhao.publicsources.ResIconImage;
import shyhao.publicsources.SaveData;
import shyhao.windows.explorer.CPU.CPUResources;
import shyhao.windows.explorer.GraphicalChart.ChartResources;
import shyhao.windows.explorer.GraphicalChart.LoadClass;
import shyhao.windows.explorer.Memory.PhysicalMemory;
public class CpuAndMemoryDynamicChart extends JFrame implements Runnable,
ActionListener, ItemListener {
// ------------------------------------
private ButtonGroup buttonGroup = new ButtonGroup();
private JRadioButton radio_highaccuracy, radio_commonaccuracy,
radio_lowaccuracy;
private JToggleButton toggle_control;
private SpringLayout springLayout;
final JPanel panel_precision, panel_view, panel_label;
private JLabel label_cpu, label_memory, label_netpacket;
// flag
private boolean highsign = false;
private boolean commonsign = false;
private boolean lowsign = false;
private boolean opensign = true;
private int lastCount = 0;
private int nowCount = 0;
//
private TimeSeries timeseries_cpu, timeseries_memory, timeseries_net;
//
private double cpu_value, memory_value,net_value;
private SaveData saveData1;
private HashMap<Date, MonitorData> dataMap1;
private ScheduledExecutorService scheduledExcutor1;
private SaveData saveData2;
private HashMap<Date, MonitorData> dataMap2;
private ScheduledExecutorService scheduledExcutor2;
private boolean isDataMap1 = true;
static Class<?> class$org$jfree$data$time$Millisecond;
static Thread process_thread;
private static final long serialVersionUID = -5227280391834054917L;
/**
* initial
*/
public CpuAndMemoryDynamicChart() {
super();
setTitle("pc resource monitor");
setIconImage(ResIconImage.getImage("images/view.gif"));
//start netpacket monitor thread
NetPacketMonitor monitor = new NetPacketMonitor();
new Thread(monitor).start();
process_thread = new Thread(this);
cpu_value = 100D;
springLayout = new SpringLayout();
getContentPane().setLayout(springLayout);
setBounds(100, 100, 652, 401);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// -------------------------------------------------------------------
// panel of adjust precision
panel_precision = new JPanel();
panel_precision.setLayout(new FlowLayout());
panel_precision.setBorder(new TitledBorder(new EtchedBorder(
EtchedBorder.LOWERED), "Adjust Precision",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));
getContentPane().add(panel_precision);
springLayout.putConstraint(SpringLayout.EAST, panel_precision, -8,
SpringLayout.EAST, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, panel_precision, 10,
SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, panel_precision, 80,
SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.NORTH, panel_precision, 10,
SpringLayout.NORTH, getContentPane());
// --------------
//
toggle_control = new JToggleButton();
toggle_control.addActionListener(this);
toggle_control.setText("Resume/Pause");
panel_precision.add(toggle_control);
radio_highaccuracy = new JRadioButton();
buttonGroup.add(radio_highaccuracy);
radio_highaccuracy.addItemListener(this);
radio_highaccuracy.setText("High Precision");
panel_precision.add(radio_highaccuracy);
radio_commonaccuracy = new JRadioButton();
buttonGroup.add(radio_commonaccuracy);
radio_commonaccuracy.addItemListener(this);
radio_commonaccuracy.setText("Normal Precision");
panel_precision.add(radio_commonaccuracy);
radio_lowaccuracy = new JRadioButton();
buttonGroup.add(radio_lowaccuracy);
radio_lowaccuracy.addItemListener(this);
radio_lowaccuracy.setText("Low Precision");
panel_precision.add(radio_lowaccuracy);
// default is Normal Precision
radio_lowaccuracy.setSelected(true);
// --------------------------------------------------------------------
// create total chart panel
panel_view = new JPanel();
panel_view.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
panel_view.setLayout(new GridLayout(3, 1));
getContentPane().add(panel_view);
springLayout.putConstraint(SpringLayout.EAST, panel_view, -8,
SpringLayout.EAST, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, panel_view, 90,
SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, panel_view, -5,
SpringLayout.SOUTH, getContentPane());
springLayout.putConstraint(SpringLayout.NORTH, panel_view, 5,
SpringLayout.SOUTH, panel_precision);
// ----------------------------------------------
//
// Create CPU chart
// -----------------------------------------------
timeseries_cpu = new TimeSeries(
"CPU",
class$org$jfree$data$time$Millisecond != null ? class$org$jfree$data$time$Millisecond
: (class$org$jfree$data$time$Millisecond = LoadClass
.getClass("org.jfree.data.time.Millisecond")));
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(
timeseries_cpu);
ChartPanel chartpanel = new ChartPanel(ChartResources.createChart(
timeseriescollection, "", "Time", "Percentage (%)"));
chartpanel.setPreferredSize(new Dimension(500, 270));
// ----------------------------------------------
// ram chart
// ----------------------------------------------
timeseries_memory = new TimeSeries(
"RAM",
class$org$jfree$data$time$Millisecond != null ? class$org$jfree$data$time$Millisecond
: (class$org$jfree$data$time$Millisecond = LoadClass
.getClass("org.jfree.data.time.Millisecond")));
TimeSeriesCollection timeseriescollection2 = new TimeSeriesCollection(
timeseries_memory);
ChartPanel chartpanel2 = new ChartPanel(ChartResources.createChart(
timeseriescollection2, "", "Time", "Percentage (%)"));
chartpanel2.setPreferredSize(new Dimension(500, 270));
// ----------------------------------------------
//
// Create netpackets chart
// -----------------------------------------------
timeseries_net = new TimeSeries(
"NetPackets",
class$org$jfree$data$time$Millisecond != null ? class$org$jfree$data$time$Millisecond
: (class$org$jfree$data$time$Millisecond = LoadClass
.getClass("org.jfree.data.time.Millisecond")));
TimeSeriesCollection timeseriescollection3 = new TimeSeriesCollection(
timeseries_net);
JFreeChart netChart = ChartResources.createChart(
timeseriescollection3, "", "Time", "Packets");
XYPlot net_XYPlot = netChart.getXYPlot();
ValueAxis net_Axis = net_XYPlot.getRangeAxis();
net_Axis.setRange(0, 150);
ChartPanel chartpanel3 = new ChartPanel(netChart);
chartpanel.setPreferredSize(new Dimension(500, 270));
// -------------------------------------
// add charts to panel_view
panel_view.add(chartpanel);
panel_view.add(chartpanel2);
panel_view.add(chartpanel3);
// ------------------------------------------------------------------------------
//display cpu in number
panel_label = new JPanel();
panel_label.setLayout(new GridLayout(3, 1));
getContentPane().add(panel_label);
springLayout.putConstraint(SpringLayout.SOUTH, panel_label, -5,
SpringLayout.SOUTH, getContentPane());
springLayout.putConstraint(SpringLayout.NORTH, panel_label, 5,
SpringLayout.SOUTH, panel_precision);
springLayout.putConstraint(SpringLayout.EAST, panel_label, -5,
SpringLayout.WEST, panel_view);
springLayout.putConstraint(SpringLayout.WEST, panel_label, 0,
SpringLayout.WEST, panel_precision);
//
label_cpu = new JLabel();
label_cpu.setVerticalAlignment(JLabel.CENTER);
label_cpu.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
label_memory = new JLabel();
label_memory.setVerticalAlignment(JLabel.CENTER);
label_memory.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
label_netpacket = new JLabel();
label_netpacket.setVerticalAlignment(JLabel.CENTER);
label_netpacket.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
panel_label.add(label_cpu);
panel_label.add(label_memory);
panel_label.add(label_netpacket);
// --------------------------------------------------------------------------
//
dataMap1 = new HashMap<Date, MonitorData>();
dataMap2 = new HashMap<Date, MonitorData>();
saveData1 = new SaveData(dataMap1, this, 1);
scheduledExcutor1 = Executors.newSingleThreadScheduledExecutor();
scheduledExcutor1.scheduleWithFixedDelay(saveData1, 2, 20, TimeUnit.SECONDS);
saveData2 = new SaveData(dataMap2, this, 2);
scheduledExcutor2 = Executors.newSingleThreadScheduledExecutor();
scheduledExcutor2.scheduleWithFixedDelay(saveData2, 12, 20, TimeUnit.SECONDS);
// start
startProcess();
LookAndFeel.changeLookAndFeel("Native", this);
}
//
public void run() {
while (true) {
if (opensign == true) {
try {
if (commonsign == true) {
int a = CPUResources.getCpuRatio();
int b = CPUResources.getCpuRatio();
cpu_value = (a + b) / 2;
}
if (highsign == true) {
cpu_value = CPUResources.getCpuRatio();
}
if (lowsign == true) {
int a = CPUResources.getCpuRatio();
int b = CPUResources.getCpuRatio();
int c = CPUResources.getCpuRatio();
int d = CPUResources.getCpuRatio();
int e = CPUResources.getCpuRatio();
cpu_value = (a + b + c + e + d) / 5;
}
String userm = null;
long use = PhysicalMemory.getUsedMemorySize() / 1024;
if (use < 1000) {
userm = use + "M";
}
if (use >= 1000) {
userm = new DecimalFormat("0.00").format(use / 1024.0)
+ "G";
}
long total = PhysicalMemory.getTotalMemorySize() / 1024;
memory_value = use * 1.0 * 100 / total;
Millisecond millisecond = new Millisecond();
timeseries_memory.add(millisecond, memory_value);
timeseries_cpu.add(millisecond, cpu_value);
nowCount = NetPacketMonitor.getPacketCount();
net_value = nowCount-lastCount;
timeseries_net.add(millisecond, net_value);
lastCount = nowCount;
label_cpu.setText("cpu:" + (int) (cpu_value) + "%");
label_memory.setText("Ram:" + "\n" + userm);
label_netpacket.setText("packets:"+ ((int)net_value));
if(isDataMap1) {
dataMap1.put(new Date(), new MonitorData(cpu_value, memory_value, net_value));
}else {
dataMap2.put(new Date(), new MonitorData(cpu_value, memory_value, net_value));
}
} catch (Exception e) {
}
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == toggle_control) {
if (opensign == true) {
opensign = false;
} else {
opensign = true;
}
}
}
public void itemStateChanged(ItemEvent e) {
if (radio_lowaccuracy.isSelected()) {
highsign = false;
commonsign = false;
lowsign = true;
}
if (radio_highaccuracy.isSelected()) {
highsign = true;
commonsign = false;
lowsign = false;
}
if (radio_commonaccuracy.isSelected()) {
highsign = false;
commonsign = true;
lowsign = false;
}
}
public void startProcess() {
process_thread.start();
}
public void setDataMapSwitch() {
isDataMap1 = isDataMap1?false:true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -