java版任务管理器_Java编写个性版任务管理器(下)

import java.awt.Image;

import java.awt.MediaTracker;

import java.awt.Toolkit;

public class MovingImageThread extends Thread {

int i = 0;

int position = 170;

Image image1;

Image image2;

ImageJPanel performancePane;

public MovingImageThread(ImageJPanel

performancePane)

{

this.performancePane = performancePane;

image1 =

Toolkit.getDefaultToolkit().getImage("wuKong1.png");

image2 =

Toolkit.getDefaultToolkit().getImage("wuKong2.png");

MediaTracker tracker = new

MediaTracker(performancePane);

tracker.addImage(image1, 1);

tracker.addImage(image2, 1);

try

{

tracker.waitForID(1);

}

catch (InterruptedException exception){}

}

public void run()

{

while(true)

{

i++;

position = position -

20;

if(position <

-290)

{

position =

250;

i = 0;

}

if(i%2 == 1)

{

performancePane.repaint(image1, position, 170, 454, 340);

}

else

{

performancePane.repaint(image2, position, 170, 454, 340);

}

try

{

Thread.sleep(1000);

}

catch(InterruptedException e)

{}

}

}

}

import java.awt.*;

import javax.swing.*;

public class ImageJPanel extends JPanel {

private int x;

private int y;

private int width;

private int height;

Image image;

public ImageJPanel(Image image, int x, int y, int

width, int height)

{

this.x = x;

this.y = y;

this.width = width;

this.height = height;

this.image = image;

}

public void repaint(Image image, int x, int y,

int width, int height)

{

this.x = x;

this.y = y;

this.width = width;

this.height = height;

this.image = image;

super.repaint();

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

g.drawImage(image, x, y, width, height,

null);

}

}

import java.lang.Thread;

import javax.swing.JLabel;

import javax.swing.JProgressBar;

import javax.swing.JTextArea;

public class SystemInformationThread extends Thread {

int cpuRatio = 0;

int memoryRatio = 0;

//用来获得系统信息

IMonitorService service;

//主变量

SystemInformationMonitorsim;

//需要动态改变的变量

SystemStatusRecordCanvascpuRecordCanvas;

SystemStatusRecordCanvasmemoryRecordCanvas;

JProgressBar cpuProgressBar;

JProgressBar memoryProgressBar;

JLabel cpuProgressLabel;

JLabel memoryProgressLabel;

JLabel statusLabel;

JTextArea t1;

JTextArea t2;

JTextArea t3;

JTextArea t4;

int i = 0;

public

SystemInformationThread(SystemInformationMonitorsim)

{

this.sim = sim;

this.cpuRecordCanvas =

sim.cpuRecordCanvas;

this.memoryRecordCanvas =

sim.memoryRecordCanvas;

this.cpuProgressBar = sim.cpuProgressBar;

this.memoryProgressBar =

sim.memoryProgressBar;

this.cpuProgressLabel =

sim.cpuProgressLabel;

this.memoryProgressLabel =

sim.memoryProgressLabel;

this.statusLabel = sim.statusLabel;

this.t1 = sim.t1;

this.t2 = sim.t2;

this.t3 = sim.t3;

this.t4 = sim.t4;

service = new MonitorServiceImpl();

}

public void run()

{

while(true)

{

try

{

SystemInformationBean monitorInfo =

service.getSystemInformationBean();

cpuRatio =

(int)monitorInfo.getCpuRatio();

memoryRatio

= (int)(monitorInfo.getUsedMemory() * 100

/monitorInfo.getTotalMemorySize());

cpuProgressBar.setValue(cpuRatio);

cpuProgressLabel.setText(cpuRatio + "%");

cpuRecordCanvas.update(cpuRecordCanvas.getGraphics(),

cpuRatio);

memoryProgressBar.setValue(memoryRatio);

memoryProgressLabel.setText(memoryRatio + "%");

memoryRecordCanvas.update(memoryRecordCanvas.getGraphics(),

memoryRatio);

t1.setText(monitorInfo.getProcessCaptionInformation());

t2.setText(monitorInfo.getProcessExecutablePathInformation());

t3.setText(monitorInfo.getProcessCreationDateInformation());

t4.setText(monitorInfo.getProcessProcessIdInformation());

statusLabel.setText("Process Number: " +

monitorInfo.getProcessNumber() +

" " +

"CPU Ratio: " + cpuRatio +

"% " +

"Physical Memory (using/total) : " +

monitorInfo.getUsedMemory()/1024 + "M" + "/" +

monitorInfo.getTotalMemorySize()/1024 + "M");

//Thread.sleep(200); }

catch(Exception e) {}

}

}

}

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.LineNumberReader;

import java.lang.management.ManagementFactory;

import com.sun.management.OperatingSystemMXBean;

public class MonitorServiceImpl implements IMonitorService {

private final int CPUTIME = 10;

private final int PERCENT = 100;

private final int FAULTLENGTH = 10;

private int processNumber = 0;

private long totalMemorySize;

private long freePhysicalMemorySize;

private long usedMemory;

private double cpuRatio = 0;

private String caption = null;

private String executablePath = null;

private String creationDate = null;

private String priority = null;

private String processId = null;

private String sessionId = null;

private String kernelModeTime = null;

private String userModeTime = null;

private String osName =

System.getProperty("os.name");

private Process process = null;

private OperatingSystemMXBean osmxb =

(OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();

private SystemInformationBean infoBean = new

SystemInformationBean();

public SystemInformationBean

getSystemInformationBean() throws Exception

{

totalMemorySize =

osmxb.getTotalPhysicalMemorySize()/1024;

freePhysicalMemorySize =

osmxb.getFreePhysicalMemorySize()/1024;

usedMemory = totalMemorySize -

freePhysicalMemorySize;

if(osName.toLowerCase().startsWith("windows"))

{

cpuRatio =

this.getCpuRatioForWindows();

caption =

this.getProcessCaptionInformation();

executablePath =

this.getProcessExecutablePathInformation();

creationDate =

this.getProcessCreationDateInformation();

processId =

this.getProcessProcessIdInformation();

}

else

{

System.out.println("It is not

windows OS");

}

infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);

infoBean.setTotalMemorySize(totalMemorySize);

infoBean.setUsedMemory(usedMemory);

infoBean.setCpuRatio(cpuRatio);

infoBean.setProcessNumber(processNumber);

infoBean.setProcessCaptionInformation(caption);

infoBean.setProcessExecutablePathInformation(executablePath);

infoBean.setProcessCreationDateInformation(creationDate);

infoBean.setProcessProcessIdInformation(processId);

return infoBean;

}

private double getCpuRatioForWindows()

{

try

{

String procCmd =

System.getenv("windir") + "\\system32\\wbem\\wmic.exe

process get

Caption,KernelModeTime,UserModeTime,WriteOperationCount";

// 取进程信息

long[] c0 =

readCpu(Runtime.getRuntime().exec(procCmd));

Thread.sleep(CPUTIME);

long[] c1 =

readCpu(Runtime.getRuntime().exec(procCmd));

if (c0 != null

&& c1 != null)

{

long

idletime = c1[0] - c0[0];

long

busytime = c1[1] - c0[1];

return

Double.valueOf(PERCENT * (busytime) / (busytime +

idletime)).doublue();

} else

{

return

0.0;

}

}

catch (Exception ex)

{

ex.printStackTrace();

return 0.0;

}

}

private long[] readCpu(final Process proc)

{

long[] retn = new long[2];

try

{

proc.getOutputStream().close();

InputStreamReader ir = new

InputStreamReader(proc.getInputStream());

LineNumberReader input = new

LineNumberReader(ir);

String line =

input.readLine();

if (line == null ||

line.length() < FAULTLENGTH)

{

return

null;

}

int capidx =

line.indexOf("Caption");

int kmtidx =

line.indexOf("KernelModeTime");

int umtidx =

line.indexOf("UserModeTime");

int wocidx =

line.indexOf("WriteOperationCount");

long idletime = 0;

long kneltime = 0;

long usertime = 0;

while ((line =

input.readLine()) != null)

{

if

(line.length() < wocidx)

{

continue;

}

String

caption = Bytes.substring(line, capidx, kmtidx - 1).trim();

String s1 =

Bytes.substring(line, kmtidx, umtidx - 1).trim();

String s2 =

Bytes.substring(line, umtidx, wocidx - 1).trim();

if

(caption.equals("System Idle Process") ||

caption.equals("System"))

{

if (s1.length() > 0)

idletime += Long.valueOf(s1).longValue();

if (s2.length() > 0)

idletime += Long.valueOf(s2).longValue();

continue;

}

if

(s1.length() > 0)

kneltime += Long.valueOf(s1).longValue();

if

(s2.length() > 0)

usertime += Long.valueOf(s2).longValue();

}

retn[0] = idletime;

retn[1] = kneltime +

usertime;

return retn;

}

catch (Exception ex)

{

ex.printStackTrace();

}

finally

{

try

{

proc.getInputStream().close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

return null;

}

private String

getProcessCaptionInformation()

{

try

{

String cmd =

System.getenv("windir") + "\\system32\\wbem\\wmic.exe

process get

Caption";//,ExecutablePath,ProcessId,SessionId,Priority,KernelModeTime,UserModeTime,CreationDate";

process =

Runtime.getRuntime().exec(cmd);

process.getOutputStream().close();

InputStreamReader ir = new

InputStreamReader(process.getInputStream());

LineNumberReader input = new

LineNumberReader(ir);

String caption = "";

String line =

input.readLine();

processNumber = 0;

while ((line =

input.readLine()) != null)

{

caption +=

line + "\n";

processNumber++;

}

processNumber =

processNumber/2 - 1;

return caption;

}

catch(Exception e)

{

e.printStackTrace();

return null;

}

finally

{

try

{

process.getInputStream().close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

private String

getProcessExecutablePathInformation()

{

try

{

String cmd =

System.getenv("windir") + "\\system32\\wbem\\wmic.exe

process get ExecutablePath";

process =

Runtime.getRuntime().exec(cmd);

process.getOutputStream().close();

InputStreamReader ir = new

InputStreamReader(process.getInputStream());

LineNumberReader input = new

LineNumberReader(ir);

String executablePath =

"";

String line =

input.readLine();

while ((line =

input.readLine()) != null)

{

executablePath += line + "\n";

}

return executablePath;

}

catch(Exception e)

{

e.printStackTrace();

return null;

}

finally

{

try

{

process.getInputStream().close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

private String

getProcessCreationDateInformation()

{

try

{

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值