📄 pourcentcpu.java
字号:
package fr.umlv.projet.fenetre.preformance;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
/**
* The Panel to display the current percent of cpu used by the image sticks
* it is contained in the Panel {@link fr.umlv.projet.fenetre.preformance.preformance Performance}.
* @author HUANG Wei & MA Xiao Jun
*
*/
public class PourcentCpu extends JPanel {
private String pourcent = " ";
private int pcGraph = 0;
private int tot = 0;
private int w = 0;
private int h = 0;
private int pc = 0;
protected@Override void paintComponent(Graphics graphic){
Graphics2D g = (Graphics2D)graphic;
super.paintComponent(g);
this.setBackground(Color.BLACK);
w = this.getWidth();
h = this.getHeight();
g.setBackground(Color.BLACK);
g.setPaint(new Color(0, 50, 0));
int y = h - 30;
tot = y/6;
int n = h - 30;
for(int i = 0;i<tot;i++){
g.fillRoundRect((int)(w*0.2),y,(int)(w*0.6),4,2,1);
y = y - 6;
}
if(pc>=50 && pc<70)
g.setColor(Color.YELLOW);
else if(pc>=70 && pc<90)
g.setColor(Color.ORANGE);
else if(pc>=90)
g.setColor(Color.RED);
else
g.setColor(Color.green);
g.drawString(pourcent,(int)((w-pourcent.length()*7)/2),h-10);
for(int j = 0;j<pcGraph;j++){
g.fillRoundRect((int)(w*0.2),n,(int)(w*0.6),4,2,1);
n = n - 6;
}
}
/**
* to refresh the number of image sticks
* @param pc the percent of cpu used
* @param s here we use a character string to display the percent beacouse it is
* easy to display the number contains decimal fraction
*/
public void refresh(int pc,String s){
this. pc = pc;
StringBuilder sb = new StringBuilder();
sb.append(s).append(" ").append("%");
pourcent = sb.toString();
setPcGraph(pc);
}
/**
* to calculate the number of stick to display for showing the percent of Cpu used.
* @param pc the percent of Cpu used.
*/
public void setPcGraph(int pc){
if(pc < 100)
this.pcGraph = pc*tot/100+1;
else
this.pcGraph = pc*tot/100;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -