開發(fā)一個托盤程序《操作系統(tǒng)》實驗報告實驗_第1頁
開發(fā)一個托盤程序《操作系統(tǒng)》實驗報告實驗_第2頁
開發(fā)一個托盤程序《操作系統(tǒng)》實驗報告實驗_第3頁
開發(fā)一個托盤程序《操作系統(tǒng)》實驗報告實驗_第4頁
開發(fā)一個托盤程序《操作系統(tǒng)》實驗報告實驗_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

開發(fā)一個托盤程序《操作系統(tǒng)》實驗報告年級、專業(yè)、班級2010級信安一班姓名實驗題目開發(fā)一個托盤程序?qū)嶒灂r間2013.4.23實驗地點主教0416實驗成績實驗性質(zhì)□驗證性□設(shè)計性□綜合性教師評價:□算法/實驗過程正確;□源程序/實驗內(nèi)容提交□程序結(jié)構(gòu)/實驗步驟合理;□實驗結(jié)果正確;□語法、語義正確;□報告規(guī)范;其他:評價教師簽名:一、實驗?zāi)康?)了解操作系統(tǒng)API函數(shù)的結(jié)構(gòu)和調(diào)用基本知識;2)會使用API函數(shù)。3)加強程序設(shè)計練習(xí)。二、實驗項目內(nèi)容在Windows環(huán)境下開發(fā)一個完整的托盤程序。程序功能:1)獲取系統(tǒng)時間,每1秒鐘刷新1次。2)獲取CPU基本信息,至少包括:CPU主頻,CPU序列號,CPU型號3)獲取內(nèi)存信息,至少包括:內(nèi)存總量,可用內(nèi)存量三、實驗過程或算法(源程序)系統(tǒng)時鐘的編程實現(xiàn):程序通過調(diào)用系統(tǒng)時間的函數(shù)類型java.util.Calendar定義了一個時間變量System.DateTimecurrentTime=newSystem.DateTime(),并通過API函數(shù)CalendarcurrentTime=Calendar.getInstance()來獲得當前的系統(tǒng)時間,并且按照固定格式輸出。在時鐘時間的實現(xiàn)中,首先實現(xiàn)了對JPanel類的繼承類clock的定義,并且對其paintComponent(Graphicsg)函數(shù)進行重載。代碼如下:publicclassCalenderextendsJPanelimplementsActionListener{ publicstaticfinalStringHOUR_OF_DAY=null; //定義 JComboBoxMonth=newJComboBox(); JComboBoxYear=newJComboBox(); JLabelYear_l=newJLabel("年"); JLabelMonth_l=newJLabel("月"); Datenow_date=newDate(); JLabel[]Label_day=newJLabel[49]; intnow_year=now_date.getYear()+1900; intnow_month=now_date.getMonth(); booleanbool=false; Stringyear_int=null; intmonth_int; JPanelpane_ym=newJPanel(); JPanelpane_day=newJPanel(); publicCalender(){ super(); //設(shè)定年月 for(inti=now_year-10;i<=now_year+20;i++){ Year.addItem(i+""); } for(inti=1;i<13;i++){ Month.addItem(i+""); } Year.setSelectedIndex(10); pane_ym.add(newJLabel("")); 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(newJLabel("")); Month.addActionListener(this); Year.addActionListener(this); pane_day.setLayout(newGridLayout(7,7,10,10)); for(inti=0;i<49;i++){ Label_day[i]=newJLabel(""); pane_day.add(Label_day[i]); } this.setDay(); this.setLayout(newBorderLayout()); pane_day.setOpaque(false); pane_ym.setOpaque(false); this.add(pane_day,BorderLayout.CENTER); this.add(pane_ym,BorderLayout.NORTH); this.setSize(100,50); this.setBorder(newTitledBorder("日歷")); setSize(300,300); } voidsetDay(){ if(bool){ year_int=now_year+""; month_int=now_month; } else{ year_int=Year.getSelectedItem().toString(); month_int=Month.getSelectedIndex(); } intyear_sel=Integer.parseInt(year_int)-1900; Datedt=newDate(year_sel,month_int,1); GregorianCalendarcal=newGregorianCalendar(); cal.setTime(dt); Stringweek[]={"日","一","二","三","四","五","六"}; intday=0; intday_week=0; for(inti=0;i<7;i++){ Label_day[i].setText(week[i]); } //月份 if(month_int==0||month_int==2||month_int==4|| month_int==6|| month_int==9||month_int==11){ day=31; } elseif(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(); intcount=1; for(inti=day_week;i<day_week+day;count++,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_day[i].setForeground(Color.blue); Label_day[i].setText(count+""); }else{ Label_day[i].setForeground(Color.red); Label_day[i].setText(count+""); } }else{ if(i==day_week+now_date.getDate()-1){ Label_day[i].setForeground(Color.blue); Label_day[i].setText(count+""); }else{ Label_day[i].setForeground(Color.black); Label_day[i].setText(count+""); } } } if(day_week==0){ for(inti=day;i<49;i++){ Label_day[i].setText(""); } }else{ for(inti=7;i<day_week;i++){ Label_day[i].setText(""); } for(inti=day_week+day;i<49;i++){ Label_day[i].setText(""); } } } publicvoidactionPerformed(ActionEvente){ if(e.getSource()==Year||e.getSource()==Month){ bool=false; this.setDay(); } }}系統(tǒng)日歷的編程實現(xiàn):程序通過調(diào)用系統(tǒng)時間的函數(shù)類型java.util.Date定義了一個時間變量Datenow_date=newDate(),并通過API函數(shù)now_date.getYear()來獲得當前的系統(tǒng)年份,now_date.getMonth()獲取系統(tǒng)月份,并且按照固定格式輸出。在時鐘時間的實現(xiàn)中,首先實現(xiàn)了對JPanel類的繼承類Calender的定義,并且對其Calender()函數(shù)進行重載。publicclassclockextendsJPanel{ privatestaticfinalColorINTEGRAL_COLOR=newColor(0,128,128); privateintradius;//半徑 privatedoubles=0.03; privateCalendarcurrentTime=Calendar.getInstance(); publicclock(intradius){ this.radius=radius; } publicvoidsetCurrentTime(Datetime){ this.currentTime.setTime(time); } publicvoidsetCurrentTime(longmillis){ this.currentTime.setTimeInMillis(millis); } publicDimensiongetPreferredSize(){ Insetsinsets=getInsets(); intr=(int)(radius==-1?0:radius*(1+s))+1; returnnewDimension(r*2+insets.left+insets.right,r*2+insets.top+insets.bottom); } protectedvoidpaintComponent(Graphicsg){ super.paintComponent(g); Graphics2Dg2d=(Graphics2D)g; paintTime(g2d); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); Insetsinsets=getInsets(); intwid=getWidth()-insets.left-insets.right; inthei=getHeight()-insets.top-insets.bottom; intr=(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(inti=0;i<60;i++){ intangle=90-i*6; doublepos[]=calcPos(r,angle); paintMinuteDot(r,g2d,pos[0],pos[1],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)); } privatevoidpaintCenterPoint(Graphics2Dg2d){ g2d.setColor(Color.BLUE); Rectangle2Drect=newRectangle2D.Double(-2,-2,4,4); g2d.fill(rect); } privatevoidpaintMinutePointer(intr,Graphics2Dg2d){ intminute=currentTime.get(Calendar.MINUTE); intsecond=currentTime.get(Calendar.SECOND); doubleangle=90-(minute+second/60.0)*6; ShapepointerShape=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); } Stringtime(){ inthour=currentTime.get(Calendar.HOUR); intminute=currentTime.get(Calendar.MINUTE); intsecond=currentTime.get(Calendar.SECOND); return(""+hour+":"+minute+":"+second); } privatevoidpaintHourPointer(intr,Graphics2Dg2d){ inthour=currentTime.get(Calendar.HOUR); intminute=currentTime.get(Calendar.MINUTE); intsecond=currentTime.get(Calendar.SECOND); doubleangle=90-(hour+minute/60.0+second/3600.0)*30; ShapepointerShape=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); } privatevoidpaintTime(Graphics2Dg2d){ g2d.setColor(Color.RED); g2d.setFont(newFont("宋體",Font.BOLD,16)); g2d.drawString(time(),60,60); } privateShapecreatePointerShape(doubler1,doubler2,doubler3,doubleangle){ GeneralPathgp=newGeneralPath(); double[]pos=calcPos(r1,angle);//指針定點 double[]pos1=calcPos(r2,angle+90);//指針側(cè)邊斷點 gp.append(newLine2D.Double(pos[0],pos[1],pos1[0],pos1[1]),true); double[]pos2=calcPos(r3,angle+180);//指針尾部 gp.lineTo((float)pos2[0],(float)pos2[1]); double[]pos3=calcPos(r2,angle+270);//指針的另一側(cè)邊斷點 gp.lineTo((float)pos3[0],(float)pos3[1]); gp.closePath(); returngp; } privatevoidpaintSecondPointer(intr,Graphics2Dg2d){ g2d.setColor(Color.BLACK); intsecond=currentTime.get(Calendar.SECOND); intangle=90-second*6; doublepos[]=calcPos(r*0.9,angle); doublepos1[]=calcPos(r*0.2,angle+180); Line2Dline=newLine2D.Double(pos1[0],pos1[1],pos[0],pos[1]); g2d.draw(line); } privatevoidpaintMinuteDot(intr,Graphics2Dg2d,doublex,doubley,booleanflag){ g2d.setColor(flag?Color.RED:Color.BLACK); if(flag){ Ellipse2Drect=newEllipse2D.Double(x-r*s,y-r*s,r*s*2,r*s*2); g2d.fill(rect); }else{ Ellipse2Drect=newEllipse2D.Double(x-r*0.02,y-r*0.02,r*0.04,r*0.04); g2d.fill(rect); } } privatedouble[]calcPos(doubler,doubleangle){ doubleradian=Math.toRadians(angle); doublex=r*Math.cos(radian); doubley=r*Math.sin(radian); returnnewdouble[]{x,y}; } publicvoidshowUI(){ newThread(){ publicvoidrun(){ while(true){ try{ Thread.sleep(1000); }catch(InterruptedExceptionex){ ex.printStackTrace(); } setCurrentTime(System.currentTimeMillis()); repaint(); } } }.start(); }}system主類:實現(xiàn)了對JFrame類的繼承類system的定義,定義了三個選項卡,并定義了三個JPanel:TimePanel、CPUPanel、MEMPanel,并分別加到三個選項卡上,在每個JPanel上顯示時間、CPU、內(nèi)存的各項信息。代碼如下:publicclasssystemextendsJFrame{ privateJTabbedPanejtpFigures=newJTabbedPane(); privateJPanelCPUPanel=newJPanel(); privateJPanelMEMPanel=newJPanel(); privateJPanelTimePanel=newJPanel(); ImageIconimageIcon1=newImageIcon("image/mem.jpg"); ImageIconimageIcon=newImageIcon("image/cpu.jpg"); ImageIconimageIcon2=newImageIcon("image/time.jpg"); //ImageIconlogo=newImageIcon("image/logo.png"); ImageIconlogo=newImageIcon("image/logo.png"); publicstaticvoidmain(String[]args)throwsSigarException{ systemframe=newsystem(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } publicsystem()throwsSigarException{ setSize(520,430); setVisible(true); setLocation(280,280); this.setTitle("SystemTable"); add(jtpFigures,BorderLayout.CENTER); jtpFigures.setBackground(Color.ORANGE); jtpFigures.setOpaque(false); CPUPanel=newJPanel(){ publicvoidpaintComponent(Graphicsg){//重寫這個方法 super.paintComponent(g);//繼承超類繪制組件方法 Graphics2Dgg=(Graphics2D)g; if(imageIcon!=null){//繪制背景 gg.drawImage(imageIcon.getImage(),0,0,this.getWidth(),this.getHeight(),this); } } }; MEMPanel=newJPanel(){ publicvoidpaintComponent(Graphicsg){//重寫這個方法 super.paintComponent(g);//繼承超類繪制組件方法 Graphics2Dgg=(Graphics2D)g; if(imageIcon1!=null){//繪制背景 gg.drawImage(imageIcon1.getImage(),0,0,this.getWidth(),this.getHeight(),this); } } }; TimePanel=newJPanel(){ publicvoidpaintComponent(Graphicsg){//重寫這個方法 super.paintComponent(g);//繼承超類繪制組件方法 Graphics2Dgg=(Graphics2D)g; if(imageIcon1!=null){//繪制背景 gg.drawImage(imageIcon2.getImage(),0,0,this.getWidth(),this.getHeight(),this); } } }; clockclockimg=newclock(100); clockimg.showUI(); Calendercal=newCalender(); 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,"系統(tǒng)時鐘");jtpFigures.add(CPUPanel,"CPU信息");jtpFigures.add(MEMPanel,"內(nèi)存信息");Sigarsigar=newSigar();CpuInfoinfos[]=sigar.getCpuInfoList();CpuPerccpuList[]=null;cpuList=sigar.getCpuPercList();CpuPerccpu;JLabelsum=newJLabel("CPU總數(shù):"); //lb00.setFont(newFont("宋體",Font.BOLD,16)); //lb00.setForeground(Color.black); sum.setSize(120,25); sum.setLocation(100,10); CPUPanel.add(sum); JTextAreacpusun=newJTextArea(); //cpusun.setOpaque(false); cpusun.setFont(newFont("宋體",Font.BOLD,16)); cpusun.setForeground(Color.black); cpusun.setSize(250,25); cpusun.setLocation(200,10); CPUPanel.add(cpusun); cpusun.setText(""+infos.length); cpusun.setOpaque(false); JLabelhz=newJLabel("CPU主頻:"); hz.setSize(120,25); hz.setLocation(100,50); CPUPanel.add(hz); JTextAreahzn=newJTextArea(); hzn.setFont(newFont("宋體",Font.BOLD,16)); hzn.setForeground(Color.black); hzn.setSize(250,25); hzn.setLocation(200,50); CPUPanel.add(hzn); hzn.setText(""+infos[1].getMhz()+"MHz"); hzn.setOpaque(false); JLabelmade=newJLabel("CPU廠商:"); made.setSize(120,25); made.setLocation(100,90); CPUPanel.add(made); JTextAreamader=newJTextArea(); mader.setFont(newFont("宋體",Font.BOLD,16)); mader.setForeground(Color.black); mader.setSize(250,25); mader.setLocation(200,90); CPUPanel.add(mader); mader.setText(""+infos[1].getVendor()); mader.setOpaque(false); JLabelsort=newJLabel("CPU型號:"); sort.setSize(120,25); sort.setLocation(100,130); CPUPanel.add(sort); JTextAreausort=newJTextArea(); usort.setFont(newFont("宋體",Font.BOLD,16)); usort.setForeground(Color.black); usort.setSize(300,25); usort.setLocation(200,130); CPUPanel.add(usort); usort.setText(""+infos[1].getModel()); usort.setOpaque(false); JLabelusep=newJLabel("CPU使用率:"); usep.setSize(120,25); usep.setLocation(100,170); CPUPanel.add(usep); JTextAreausept=newJTextArea(); usept.setFont(newFont("宋體",Font.BOLD,16)); usept.setForeground(Color.black); usept.setSize(250,25); usept.setLocation(200,170); CPUPanel.add(usept); doubleup=0; up=cpuList[infos.length-1].getCombined(); DecimalFormatdf=newDecimalFormat(".00"); doublemm=up*100; usept.setText(""+df.format(mm)+"%"); usept.setOpaque(false); JLabelnetcd=newJLabel("網(wǎng)卡MAC:"); netcd.setSize(120,25); netcd.setLocation(100,210); CPUPanel.add(netcd); String[]ifaces=sigar.getNetInterfaceList(); NetInterfaceConfigcfg=sigar.getNetInterfaceConfig(ifaces[1]); JTextAreanetcard=newJTextArea(); netcard.setFont(newFont("宋體",Font.BOLD,16)); netcard.setForeground(Color.black); netcard.setSize(250,25); netcard.setLocation(200,210); CPUPanel.add(netcard); netcard.setText(""+cfg.getHwaddr()); netcard.setOpaque(false); JLabeldisk=newJLabel("硬盤總?cè)萘浚?); disk.setSize(120,25); disk.setLocation(100,250); CPUPanel.add(disk); File[]roots=File.listRoots();//獲取磁盤分區(qū)列表 intr=0; for(Filefile:roots){ r+=file.getTotalSpace()/1024/1024/1024; } JTextAreaTdisk=newJTextArea(); Tdisk.setFont(newFont("宋體",Font.BOLD,16)); Tdisk.setForeground(Color.black); Tdisk.setSize(250,25); Tdisk.setLocation(200,250); CPUPanel.add(Tdisk); Tdisk.setText(""+r+"G"); Tdisk.setOpaque(false); JLabelfile=newJLabel("文件系統(tǒng)ID:"); file.setSize(120,25); file.setLocation(100,290); CPUPanel.add(file); JTextAreaTfile=newJTextArea(); Tfile.setFont(newFont("宋體",Font.BOLD,16)); Tfile.setForeground(Color.black); Tfile.setSize(250,25); Tfile.setLocation(200,290); CPUPanel.add(Tfile); FileSystemfslist[]=sigar.getFileSystemList(); FileSystemfs=fslist[3]; Tfile.setText(""+fs.getSysTypeName()); Tfile.setOpaque(false); JLabelOS=newJLabel("操作系統(tǒng):"); OS.setSize(120,25); OS.setLocation(100,330); CPUPanel.add(OS); JTextAreaTOS=newJTextArea(); TOS.setFont(newFont("宋體",Font.BOLD,16)); TOS.setForeground(Color.black); TOS.setSize(250,25); TOS.setLocation(200,330); CPUPanel.add(TOS); Propertiesprops=System.getProperties(); TOS.setText(""+props.getProperty("")); TOS.setOpaque(false); JLabelmemuse=newJLabel("內(nèi)存使用率:"); memuse.setSize(120,25); memuse.setLocation(100,10); MEMPanel.add(memuse); Memmem=sigar.getMem(); JTextAreamemuses=newJTextArea(); memuses.setFont(newFont("宋體",Font.BOLD,16)); memuses.setForeground(Color.black); memuses.setSize(250,25); memuses.setLocation(200,10); MEMPanel.add(memuses); doublem=(double)mem.getUsed()/(double)mem.getTotal(); doublenn=m*100; memuses.setText(""+df.format(nn)+"%"); memuses.setOpaque(false); JLabelphmem=newJLabel("物理內(nèi)存:"); phmem.setSize(120,25); phmem.setLocation(100,50); MEMPanel.add(phmem); JTextAreaTphmem=newJTextArea(); Tphmem.setFont(newFont("宋體",Font.BOLD,16)); Tphmem.setForeground(Color.black); Tphmem.setSize(250,25); Tphmem.setLocation(200,50); MEMPanel.add(Tphmem); Tphmem.setText(""+mem.getTotal()); Tphmem.setOpaque(false); JLabelunuse=newJLabel("未使用物理內(nèi)存:"); unuse.setSize(120,25); unuse.setLocation(100,90); MEMPanel.add(unuse); JTextAreaTunuse=newJTextArea(); Tunuse.setFont(newFont("宋體",Font.BOLD,16)); Tunuse.setForeground(Color.black); Tunuse.setSize(250,25); Tunuse.setLocation(200,90); MEMPanel.add(Tunuse); Tunuse.setText(""+mem.getFree()/1024L+"K"); Tunuse.setOpaque(false); JLabelexchange=newJLabel("交換區(qū)總量:"); exchange.setSize(120,25); exchange.setLocation(100,130); MEMPanel.add(exchange); Swapswap=sigar.getSwap(); JTextAreaTexchange=newJTextArea(); Texchange.setFont(newFont("宋體",Font.BOLD,16)); Texchange.setForeground(Color.black); Texchange.setSize(250,25); Texchange.setLocation(200,130); MEMPanel.add(Texchange); Texchange.setText(""+swap.getTotal()/1024L+"K"); Texchange.setOpaque(false); JLabelexchu=newJLabel("當前已使用交換:"); exchu.setSize(120,25); exchu.setLocation(100,170); MEMPanel.add(exchu); JTextAreaTexchu=newJTextArea(); Texchu.setFont(newFont("宋體",Font.BOLD,16)); Texchu.setForeground(Color.black); Texchu.setSize(250,25); Texchu.setLocation(200,170); MEMPanel.add(Texchu); Texchu.setText(""+swap.getUsed()/1024L+"

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論