《开发一个托盘程序《操作系统》实验报告实验.doc》由会员分享,可在线阅读,更多相关《开发一个托盘程序《操作系统》实验报告实验.doc(20页珍藏版)》请在三一办公上搜索。
1、开发一个托盘程序操作系统实验报告年级、专业、班级2010级信安一班姓名 实验题目开发一个托盘程序实验时间 2013.4.23实验地点主教0416实验成绩 实验性质验证性 设计性 综合性教师评价:算法/实验过程正确; 源程序/实验内容提交 程序结构/实验步骤合理;实验结果正确; 语法、语义正确; 报告规范; 其他: 评价教师签名:一、实验目的1)了解操作系统API函数的结构和调用基本知识;2)会使用API函数。3)加强程序设计练习。二、实验项目内容在Windows环境下开发一个完整的托盘程序。程序功能:1)获取系统时间,每1秒钟刷新1次。2)获取CPU基本信息,至少包括:CPU主频,CPU序列号
2、,CPU型号3)获取内存信息,至少包括:内存总量,可用内存量三、实验过程或算法(源程序)系统时钟的编程实现: 程序通过调用系统时间的函数类型java.util.Calendar 定义了一个时间变量System.DateTime currentTime = new System.DateTime(),并通过API函数Calendar currentTime = Calendar.getInstance()来获得当前的系统时间,并且按照固定格式输出。在时钟时间的实现中,首先实现了对JPanel类的继承类clock的定义,并且对其paintComponent(Graphics g)函数进行重载。代码
3、如下:public class Calender extends JPanel implements ActionListener public static final String HOUR_OF_DAY = null;/定义JComboBox Month = new JComboBox(); JComboBox Year = new JComboBox(); JLabel Year_l = new JLabel(年); JLabel Month_l = new JLabel(月); Date now_date = new Date(); JLabel Label_day = new JL
4、abel49; int now_year = now_date.getYear() + 1900;int now_month = now_date.getMonth();boolean bool = false;String year_int = null; int month_int; JPanel pane_ym = new JPanel(); JPanel pane_day = new JPanel(); public Calender() super();/设定年月 for (int i = now_year - 10; i = now_year + 20; i+) Year.addI
5、tem(i + ); for (int i = 1; i 13; i+) Month.addItem(i + ); Year.setSelectedIndex(10); pane_ym.add(new JLabel( );pane_ym.add(Year);pane_ym.add(Year_l);Month.setSelectedIndex(now_month);pane_ym.add(Month); pane_ym.add(Month_l); pane_ym.add(new JLabel( );Month.addActionListener(this); Year.addActionList
6、ener(this);pane_day.setLayout(new GridLayout(7, 7, 10, 10); for (int i = 0; i 49; i+) Label_dayi = new JLabel( ); pane_day.add(Label_dayi); this.setDay();this.setLayout(new BorderLayout(); pane_day.setOpaque(false);pane_ym.setOpaque(false); this.add(pane_day, BorderLayout.CENTER); this.add(pane_ym,
7、BorderLayout.NORTH); this.setSize(100,50);this.setBorder(new TitledBorder(日历);setSize(300,300); void setDay() if (bool) year_int = now_year + ; month_int = now_month; else year_int = Year.getSelectedItem().toString(); month_int = Month.getSelectedIndex(); int year_sel = Integer.parseInt(year_int) -
8、1900; Date dt = new Date(year_sel, month_int, 1); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(dt); String week = 日, 一,二, 三, 四, 五, 六 ; int day = 0;int day_week = 0; for (int i = 0; i 7; i+) Label_dayi.setText(weeki); /月份if (month_int = 0|month_int = 2 |month_int = 4 |month_int = 6 |
9、month_int = 9 |month_int = 11) day = 31; else if (month_int = 3 |month_int = 5 | month_int = 7|month_int = 8 |month_int = 10| month_int = 1) day = 30; else if (cal.isLeapYear(year_sel) day = 29; else day = 28; day_week = 7 + dt.getDay(); int count = 1; for (int i = day_week; i day_week + day; count+
10、, i+) if (i % 7 = 0 |i = 13|i = 20|i = 27| i = 48 |i = 34 |i = 41) if (i = day_week + now_date.getDate() - 1) Label_dayi.setForeground(Color.blue);Label_dayi.setText(count + ); else Label_dayi.setForeground(Color.red); Label_dayi.setText(count + ); else if (i = day_week + now_date.getDate() - 1) Lab
11、el_dayi.setForeground(Color.blue); Label_dayi.setText(count + ); else Label_dayi.setForeground(Color.black); Label_dayi.setText(count + ); if (day_week = 0) for (int i = day; i 49; i+) Label_dayi.setText( ); else for (int i = 7; i day_week; i+) Label_dayi.setText( ); for (int i = day_week + day; i 4
12、9; i+) Label_dayi.setText( ); public void actionPerformed(ActionEvent e) if (e.getSource() = Year | e.getSource() = Month) bool = false; this.setDay(); 系统日历的编程实现:程序通过调用系统时间的函数类型java.util.Date 定义了一个时间变量Date now_date = new Date(),并通过API函数now_date.getYear()来获得当前的系统年份,now_date.getMonth()获取系统月份,并且按照固定格式输
13、出。在时钟时间的实现中,首先实现了对JPanel类的继承类Calender的定义,并且对其Calender()函数进行重载。public class clock extends JPanel private static final Color INTEGRAL_COLOR = new Color(0, 128, 128);private int radius;/半径private double s = 0.03;private Calendar currentTime = Calendar.getInstance();public clock(int radius) this.radius
14、= radius;public void setCurrentTime(Date time) this.currentTime.setTime(time);public void setCurrentTime(long millis) this.currentTime.setTimeInMillis(millis);public Dimension getPreferredSize() Insets insets = getInsets();int r = (int) (radius = -1 ? 0 : radius * (1 + s) + 1;return new Dimension(r
15、* 2 + insets.left + insets.right, r * 2 + insets.top + insets.bottom);protected void paintComponent(Graphics g) super.paintComponent(g);Graphics2D g2d = (Graphics2D) g;paintTime(g2d);g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);Insets insets = getInsets();
16、int wid = getWidth() - insets.left - insets.right;int hei = getHeight() - insets.top - insets.bottom;int r = (int) (Math.min(wid, hei) / 2 / (1 + s);g2d.translate(insets.left + r * (1 + s), insets.top + r * (1 + s);g2d.scale(1, -1);for (int i = 0; i 60; i+) int angle = 90 - i * 6;double pos = calcPo
17、s(r, angle);paintMinuteDot(r, g2d, pos0, pos1, i % 5 = 0);paintHourPointer(r, g2d);paintMinutePointer(r, g2d);paintSecondPointer(r, g2d);paintCenterPoint(g2d);g2d.scale(1, -1);g2d.translate(-insets.left - r * (1 + s), -insets.top - r * (1 + s);private void paintCenterPoint(Graphics2D g2d) g2d.setCol
18、or(Color.BLUE);Rectangle2D rect = new Rectangle2D.Double(-2, -2, 4, 4);g2d.fill(rect);private void paintMinutePointer(int r, Graphics2D g2d) int minute = currentTime.get(Calendar.MINUTE);int second = currentTime.get(Calendar.SECOND);double angle = 90 - (minute + second / 60.0) * 6;Shape pointerShape
19、 = createPointerShape(r * 0.8, r * 0.04, r * 0.08, angle);g2d.setColor(Color.LIGHT_GRAY);g2d.fill(pointerShape);g2d.setColor(Color.DARK_GRAY);g2d.draw(pointerShape);String time()int hour = currentTime.get(Calendar.HOUR);int minute = currentTime.get(Calendar.MINUTE);int second = currentTime.get(Calen
20、dar.SECOND);return( +hour+:+minute+:+second);private void paintHourPointer(int r, Graphics2D g2d) int hour = currentTime.get(Calendar.HOUR);int minute = currentTime.get(Calendar.MINUTE);int second = currentTime.get(Calendar.SECOND);double angle = 90 - (hour + minute / 60.0 + second / 3600.0) * 30;Sh
21、ape pointerShape = createPointerShape(r * 0.6, r * 0.06, r * 0.1, angle);g2d.setColor(Color.LIGHT_GRAY);g2d.fill(pointerShape);g2d.setColor(Color.DARK_GRAY);g2d.draw(pointerShape);private void paintTime(Graphics2D g2d)g2d.setColor(Color.RED);g2d.setFont(new Font(宋体, Font.BOLD, 16);g2d.drawString(tim
22、e(),60, 60);private Shape createPointerShape(double r1, double r2, double r3, double angle) GeneralPath gp = new GeneralPath();double pos = calcPos(r1, angle);/指针定点double pos1 = calcPos(r2, angle + 90);/指针侧边断点gp.append(new Line2D.Double(pos0, pos1, pos10, pos11), true);double pos2 = calcPos(r3, angl
23、e + 180);/指针尾部gp.lineTo(float) pos20, (float) pos21);double pos3 = calcPos(r2, angle + 270);/指针的另一侧边断点gp.lineTo(float) pos30, (float) pos31);gp.closePath();return gp;private void paintSecondPointer(int r, Graphics2D g2d) g2d.setColor(Color.BLACK);int second = currentTime.get(Calendar.SECOND);int ang
24、le = 90 - second * 6;double pos = calcPos(r * 0.9, angle);double pos1 = calcPos(r * 0.2, angle + 180);Line2D line = new Line2D.Double(pos10, pos11, pos0, pos1);g2d.draw(line);private void paintMinuteDot(int r, Graphics2D g2d, double x, double y, boolean flag) g2d.setColor(flag ? Color.RED : Color.BL
25、ACK);if (flag) Ellipse2D rect = new Ellipse2D.Double(x - r * s, y - r * s, r * s * 2, r * s * 2);g2d.fill(rect); else Ellipse2D rect = new Ellipse2D.Double(x - r * 0.02, y - r * 0.02, r * 0.04, r * 0.04);g2d.fill(rect);private double calcPos(double r, double angle) double radian = Math.toRadians(ang
26、le);double x = r * Math.cos(radian);double y = r * Math.sin(radian);return new double x, y ;public void showUI()new Thread() public void run() while (true) try Thread.sleep(1000); catch (InterruptedException ex) ex.printStackTrace();setCurrentTime(System.currentTimeMillis();repaint();.start();system
27、主类: 实现了对JFrame类的继承类system的定义,定义了三个选项卡,并定义了三个JPanel:TimePanel、CPUPanel、MEMPanel,并分别加到三个选项卡上,在每个JPanel上显示时间、CPU、内存的各项信息。代码如下:public class system extends JFrameprivate JTabbedPane jtpFigures = new JTabbedPane();private JPanel CPUPanel = new JPanel();private JPanel MEMPanel = new JPanel();private JPanel
28、 TimePanel = new JPanel();ImageIcon imageIcon1 =new ImageIcon(image/mem.jpg);ImageIcon imageIcon =new ImageIcon(image/cpu.jpg);ImageIcon imageIcon2 =new ImageIcon(image/time.jpg);/ImageIcon logo =new ImageIcon(image/logo.png);ImageIcon logo =new ImageIcon(image/logo.png);public static void main(Stri
29、ng args) throws SigarExceptionsystem frame = new system();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true); public system() throws SigarException setSize(520, 430);setVisible(true);setLocation(280, 280);this.setTitle(SystemTable); add(jtpFigures, BorderLayout.CENTER); jtpF
30、igures.setBackground(Color.ORANGE); jtpFigures.setOpaque(false); CPUPanel = new JPanel() public void paintComponent(Graphics g) /重写这个方法 super.paintComponent(g);/继承超类绘制组件方法 Graphics2D gg=(Graphics2D)g; if (imageIcon != null) / 绘制背景 gg.drawImage(imageIcon.getImage(), 0, 0,this.getWidth(),this.getHeigh
31、t(), this); ; MEMPanel = new JPanel() public void paintComponent(Graphics g) /重写这个方法 super.paintComponent(g);/继承超类绘制组件方法 Graphics2D gg=(Graphics2D)g; if (imageIcon1 != null) / 绘制背景 gg.drawImage(imageIcon1.getImage(), 0, 0,this.getWidth(),this.getHeight(), this); ; TimePanel = new JPanel() public voi
32、d paintComponent(Graphics g) /重写这个方法 super.paintComponent(g);/继承超类绘制组件方法 Graphics2D gg=(Graphics2D)g; if (imageIcon1 != null) / 绘制背景 gg.drawImage(imageIcon2.getImage(), 0, 0,this.getWidth(),this.getHeight(), this); ; clock clockimg = new clock(100); clockimg.showUI(); Calender cal = new Calender();
33、clockimg.setOpaque(false); cal.setOpaque(false); TimePanel.add(cal,BorderLayout.EAST); TimePanel.add(clockimg,BorderLayout.WEST); TimePanel.setOpaque(false); CPUPanel.setOpaque(false); CPUPanel.setLayout(null); MEMPanel.setOpaque(false); MEMPanel.setLayout(null); jtpFigures.add(TimePanel, 系统时钟); jtp
34、Figures.add(CPUPanel, CPU信息); jtpFigures.add(MEMPanel, 内存信息); Sigar sigar = new Sigar(); CpuInfo infos = sigar.getCpuInfoList(); CpuPerc cpuList = null; cpuList = sigar.getCpuPercList(); CpuPerc cpu; JLabel sum = new JLabel(CPU总数:); /lb00.setFont(new Font(宋体, Font.BOLD, 16); /lb00.setForeground(Colo
35、r.black); sum.setSize(120, 25); sum.setLocation(100, 10); CPUPanel.add(sum); JTextArea cpusun = new JTextArea(); /cpusun.setOpaque(false); cpusun.setFont(new Font(宋体, Font.BOLD, 16); cpusun.setForeground(Color.black); cpusun.setSize(250, 25); cpusun.setLocation(200, 10); CPUPanel.add(cpusun); cpusun
36、.setText( +infos.length); cpusun.setOpaque(false); JLabel hz = new JLabel(CPU主频:); hz.setSize(120, 25); hz.setLocation(100, 50); CPUPanel.add(hz); JTextArea hzn = new JTextArea(); hzn.setFont(new Font(宋体, Font.BOLD, 16); hzn.setForeground(Color.black); hzn.setSize(250, 25); hzn.setLocation(200, 50);
37、 CPUPanel.add(hzn); hzn.setText( +infos1.getMhz()+ MHz); hzn.setOpaque(false); JLabel made = new JLabel(CPU厂商:); made.setSize(120, 25); made.setLocation(100, 90); CPUPanel.add(made); JTextArea mader = new JTextArea(); mader.setFont(new Font(宋体, Font.BOLD, 16); mader.setForeground(Color.black); mader
38、.setSize(250, 25); mader.setLocation(200, 90); CPUPanel.add(mader); mader.setText( +infos1.getVendor(); mader.setOpaque(false); JLabel sort = new JLabel(CPU型号:); sort.setSize(120, 25); sort.setLocation(100, 130); CPUPanel.add(sort); JTextArea usort = new JTextArea(); usort.setFont(new Font(宋体, Font.
39、BOLD, 16); usort.setForeground(Color.black); usort.setSize(300, 25); usort.setLocation(200, 130); CPUPanel.add(usort); usort.setText( +infos1.getModel(); usort.setOpaque(false); JLabel usep = new JLabel(CPU使用率:); usep.setSize(120, 25); usep.setLocation(100, 170); CPUPanel.add(usep); JTextArea usept
40、= new JTextArea(); usept.setFont(new Font(宋体, Font.BOLD, 16); usept.setForeground(Color.black); usept.setSize(250, 25); usept.setLocation(200, 170); CPUPanel.add(usept); double up=0; up=cpuListinfos.length-1.getCombined(); DecimalFormat df = new DecimalFormat(.00); double mm = up*100; usept.setText( +df.format(mm) + %); usept.setOpaque(false); JLabel netcd = new JLabel(网卡MAC:); netcd.setSize(120, 25); netcd.setLocation(100, 210); CPUPanel.add(netcd); String ifaces = sigar.getNetInterfaceList(); NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces1); JTextAr