Java课程设计的图片浏览器的.docx
《Java课程设计的图片浏览器的.docx》由会员分享,可在线阅读,更多相关《Java课程设计的图片浏览器的.docx(11页珍藏版)》请在冰豆网上搜索。
![Java课程设计的图片浏览器的.docx](https://file1.bdocx.com/fileroot1/2022-11/24/a9123e66-848f-437f-8667-53f360fd2060/a9123e66-848f-437f-8667-53f360fd20601.gif)
Java课程设计的图片浏览器的
.课程设计的任务及要求
2.需求分析
图形化界面(GUI)编程,编写一个图片浏览器程序可以支持““.GIF”,
“.JPEG',“.jpeg”,“.TGA',“.JPG”,“.jpg”等格式,单打开图片,可以将同一目录下的图片按缩略图打开按“上一”“下一”按钮可以显示相应图片。
运行Applet时,图像不是一气呵成的,因为方法不是吧图像完整的装入存再显示的。
于此相反,方法创建一个线程,该线程与Applet的原有线程并发执行,
一边装入一边显示,从而产生上了不联需显示的现象。
为了提高图像才显示效果,可以采用双缓冲技术:
首先把图像装入存,然后再显示在屏幕上。
3.设计思路
3.1界面设计
选择图片按钮:
主要用dir函数实现图片的遍历。
上一,下一:
通过做标轴回调函数实现。
由于本软件为单机软件,不需要大量的数据读写和数据交换,实现上、下功能要求只能读取PictureBox控件当前加载的目录,读取当前路径,创建一维数组。
frame=newFrame("Pictureviewer");
Panelpb=newPanel();
Buttonselect=newButton("选择图片");
previous=newButton("上——");
next=newButton(”下一");select.addActionListener(this);
previous.addActionListener(this);
3.2.图像加载:
Applet常用来显示储存在文件中的图像,多数Applet使用的是GIF或JPEG格式的图像文件。
需Applet加载图像只需首先定义Image对象,然后使用
getlmage()方法把图像和文件结合起来即可。
image_width=bi.getWidth(this);
image_height=bi.getHeight(this);
doubleimage_proportion=1.0*image_height/image_width;
System.out.println("image:
w"+image_width+",h
"+image_height+",p1"+image_proportion);
if(image_proportion>screen_proportion){image_height=screen_height;
image_width=(int)(image_height/image_proportion);System.out.println(”p1>p0w="+image_width);}else{
image_width=screen_width;image_height=(int)(image_width*image_proportion);System.out.println(”p0>p1h="+image_height);}
4.详细设计
4.1.程序设计流程图
4.2.源程序代码
packageC;
importjava.io.File;
importjava.io.FilenameFilter;
publicclassMyFilterimplementsFilenameFilter{privateString[]extension;
publicMyFilter(){
extension=newString[]{".jpg",".JPG",".gif",".GIF",".png",".PNG",
".jpeg",".JPEG"};
}
publicMyFilter(String[]extension){
this.extension=extension;
}
publicbooleanaccept(Filedir,Stringname){
for(Strings:
extension){
if(name.endsWith(s)){
returntrue;
}
}
returnfalse;
}
}
packageC;
importjava.awt.*;
importjava.awt.event.*;
importjava.awt.image.*;
publicclassMyCanvasextendsCanvasimplementsComponentListener{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
privateBufferedlmagebi;
privateImageim;
privateintimage_width;
privateintimage_height;
publicvoidsetImage(BufferedImagebi){
this.bi=bi;
this.zoom();
}
publicvoidpaint(Graphicsg){
g.drawlmage(im,(this.getWidth()-image_width)/2,(this.getHeight()-image_height)/2,this);
}
publicvoidcomponentResized(ComponentEvente){
if(bi!
=null){
System.out.println("resize!
!
");
this.zoom();
this.repaint();
}
}
publicvoidcomponentMoved(ComponentEvente){}
publicvoidcomponentShown(ComponentEvente){}
publicvoidcomponentHidden(ComponentEvente){}
publicvoidzoom(){
if(bi==null)
return;
intscreen_width=this.getWidth();
intscreen_height=this.getHeight();
doublescreen_proportion=1.0*screen_height/screen_width;
System.out.println("screen:
w"+screen_width+",h
"+screen_height+",p0"+screen_proportion);
image_width=bi.getWidth(this);
image_height=bi.getHeight(this);
doubleimage_proportion=1.0*image_height/image_width;
System.out.println("image:
w"+image_width+",h
"+image_height+",p1"+image_proportion);
if(image_proportion>screen_proportion){
image_height=screen_height;
image_width=(int)(image_height/image_proportion);
System.out.println(”p1>p0w="+image_width);
}else{
image_width=screen_width;
image_height=(int)(image_width*image_proportion);
System.out.println(”p0>p1h="+image_height);
}_
im=
bi.getScaledlnstance(image_width,image_height,lmage.SCALE_SMOOTH);
}~~一
}
packageC;
importjava.awt.*;
importjava.awt.event.*;
importjava.awt.image.*;
importjava.io.*;
importjavax.imageio.*;
publicclassTimplementsActionListener{
privateprivate
Frameframe;MyCanvasmc;
private
String
fpath;
private
String
fname;
private
File[]
files;
private
intfindex;
privateFileDialogfd_load;
privateMyFilterfilter;
privateButtonprevious;
privateButtonnext;
publicstaticvoidmain(Stringargs[])throwsException{newT().init();
}
publicvoidinit(){
frame=newFrame("Pictureviewer");
Panelpb=newPanel();
Buttonselect=newButton("选择图片");previous=newButton("上——");next=newButton(”下一");
select.addActionListener(this);
previous.addActionListener(this);
next.addActionListener(this);
pb.add(select);
pb.add(previous);
pb.add(next);
mc=newMyCanvas();mcsetBackground(newColor(200,210,230));mcaddComponentListener(m();
frame.add(pb,"North");
frame.add(mc"Center");
frame.setSize(360,360);
frame.setLocation(400,200);
frame.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
frame.setVisible(true);
this.validateButton();
filter=newMyFilter();
fd」oad=newFileDialog(frame,"打开文件”,FileDialog.LOAD
fd」oad.setFilenameFilter(filter);
}_
publicvoidactionPerformed(ActionEvente){
Stringcommand=e.getActionCommand();
if(command.equals(”选择图片")){
fd」oad.setVisible(true);
fpath=fd」oad.getDirectory();
fname=fd_load.getFile();
if((fpath!
=null)&&(fname!
=null)){
this.display(newFile(fpath+fname));
files=newFile(fpath).listFiles(filter);
this.setIndex();
}
}elseif(command.equals(”上一")){
findex--;
if(findex<0)
findex=0;
this.display(files[findex]);
}elseif(command.equals(”下一")){
findex++;
if(findex>=files.length)
findex=files.length-1;
this.display(files[findex]);
}
this.validateButton();
}
publicvoiddisplay(Filef){
try{
BufferedImagebi=ImageIO.read(f);
mcsetImage(bi);
frame.setTitle("PictureViewer-["+f.getName()+"]");
}catch(Exceptione){
e.printStackTrace();
}
mcrepaint();
}
publicvoidsetIndex(){
Filecurrent=newFile(fpath+fname);
if(files!
=null){
for(inti=0;iif(current.equals(files[i])){
findex=i;
}
}
}
}
publicvoidvalidateButton(){
previous.setEnabled((files!
=null)&&(findex>0));
next.setEnabled((files!
=null)&&(findex<(files.length-1)));}
}
5.运行调试与分析讨论
5.1.将同一目录下的图片按缩略图打开
5.2.单打开图片
53按”上一”,”下一”按钮打开图片
厂[占」Piutu苗W合//卽-[気jpq][口I
[座Picture-Viewer-[955S_topjpgJ
回J'
迭择图片
戸則张
SriiBiH-iii+e-fai|
6.设计体会与小结
我通过这次编程实践学习到了Image和Griphics相关的类的使用。
首先我通过网上搜集资料和自己看jdkapi对Image、graphics、swing中的类有了实质的操练,对它们的理解有了进一步的理解。
不象以前只有模糊的记忆,根本不会运用到实际的情况中,仅仅照着课本超代码。
不过由于技术水平限制,写的代码有点乱,类的层次不清晰。
类之间的调用不明朗。
程序中的代码出现累赘,重复创建实例,浪费系统资源。
这是很早的了,现在我修改了里面的一些如:
如果图片文件中有不是图片格式的他不会显示,我让它自动跳过去。
还有当图片到达文件末尾,上下页按钮不能用并给出提示。
改观了软件的外观,稍微美观了些通过该课程设计,使我们掌握了程序设计的基本思路和方法,并使我们巩固和提高了java编程技术。
通过完成具有一定难度的课程设计题目的编写,调试运行工作,使我们进一步掌握了面向对象程序设计的基本方法和编译技巧,巩固所学的理论知识,使理论与实际相结合,从而提高自我分析问题,和
解决问题的能力。
培养研发和独立分析以及独立设计的能力。
具备初步的独立分析和设计能力;软件开发过程得问题分析,系统设计,程序编码,测试等基本方发和技巧;综合运用所学的理论知识和方法进行独立分析和解决问题的能力。
而且在这次设计中,把死板的课本知识变得生动有趣,激发了学习的积极性,强化了所学的知识。
把课堂上的知识通过自己设计的程序表示出来,加深了对理论知识的理解。
通过本次课程设计,我们还学到了不少动手实践的经验,对以后的学习,工作都是很有用的。
7.设计的主要参考文献
(1)丁振凡.《Java语言实用教程》邮电大学
(2)耿祥义.《Java2实用教程》清华大学
(3)付青.《Java面向对象程序设计》国防工业
(4)耿祥义.《Java课程设计》清华大学