开发一个托盘程序《操作系统》实验报告 实验1.docx
- 文档编号:6783447
- 上传时间:2023-01-10
- 格式:DOCX
- 页数:27
- 大小:937.23KB
开发一个托盘程序《操作系统》实验报告 实验1.docx
《开发一个托盘程序《操作系统》实验报告 实验1.docx》由会员分享,可在线阅读,更多相关《开发一个托盘程序《操作系统》实验报告 实验1.docx(27页珍藏版)》请在冰豆网上搜索。
开发一个托盘程序《操作系统》实验报告实验1
开发一个托盘程序《操作系统》实验报告
年级、专业、班级
2010级信安一班
姓名
实验题目
开发一个托盘程序
实验时间
2013.4.23
实验地点
主教0416
实验成绩
实验性质
□验证性□设计性□综合性
教师评价:
□算法/实验过程正确;□源程序/实验内容提交□程序结构/实验步骤合理;
□实验结果正确;□语法、语义正确;□报告规范;
其他:
评价教师签名:
一、实验目的
1)了解操作系统API函数的结构和调用基本知识;
2)会使用API函数。
3)加强程序设计练习。
二、实验项目内容
在Windows环境下开发一个完整的托盘程序。
程序功能:
1)获取系统时间,每1秒钟刷新1次。
2)获取CPU基本信息,至少包括:
CPU主频,CPU序列号,CPU型号
3)获取内存信息,至少包括:
内存总量,可用内存量
三、实验过程或算法(源程序)
系统时钟的编程实现:
程序通过调用系统时间的函数类型java.util.Calendar定义了一个时间变量
System.DateTimecurrentTime=newSystem.DateTime(),并通过API函数CalendarcurrentTime=Calendar.getInstance()来获得当前的系统时间,并且按照固定格式输出。
在时钟时间的实现中,首先实现了对JPanel类的继承类clock的定义,并且对其paintComponent(Graphicsg)函数进行重载。
代码如下:
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();
//设定年月
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 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 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(); } } } 系统日历的编程实现: 程序通过调用系统时间的函数类型java.util.Date定义了一个时间变量 Datenow_date=newDate(),并通过API函数now_date.getYear()来获得当前的系统年份,now_date.getMonth()获取系统月份,并且按照固定格式输出。 在时钟时间的实现中,首先实现了对JPanel类的继承类Calender的定义,并且对其Calender()函数进行重载。 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);//指针侧边断点 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);//指针的另一侧边断点 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主类: 实现了对JFrame类的继承类system的定义,定义了三个选项卡,并定义了三个JPanel: TimePanel、CPUPanel、MEMPanel,并分别加到三个选项卡上,在每个JPanel上显示时间、CPU、内存的各项信息。 代码如下: 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=newImageIc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 开发一个托盘程序操作系统实验报告 实验1 开发 一个 托盘 程序 实验 报告