Java课程设计的图片浏览器的文档格式.docx
- 文档编号:16505138
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:11
- 大小:74.50KB
Java课程设计的图片浏览器的文档格式.docx
《Java课程设计的图片浏览器的文档格式.docx》由会员分享,可在线阅读,更多相关《Java课程设计的图片浏览器的文档格式.docx(11页珍藏版)》请在冰豆网上搜索。
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;
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.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;
screen:
+screen_width+"
+screen_height+"
p0"
+screen_proportion);
+image_proportion);
screen_proportion){
image_height=screen_height;
System.out.println(”p1>
}else{
image_height=(int)(image_width*image_proportion);
System.out.println(”p0>
}_
im=
bi.getScaledlnstance(image_width,image_height,lmage.SCALE_SMOOTH);
}~~一
importjava.awt.event.*;
importjava.io.*;
importjavax.imageio.*;
publicclassTimplementsActionListener{
privateprivate
Frameframe;
MyCanvasmc;
private
String
fpath;
fname;
File[]
files;
intfindex;
privateFileDialogfd_load;
privateMyFilterfilter;
privateButtonprevious;
privateButtonnext;
publicstaticvoidmain(Stringargs[])throwsException{newT().init();
publicvoidinit(){
previous=newButton("
next=newButton(”下一"
select.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;
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;
i<
files.length;
i++){
if(current.equals(files[i])){
findex=i;
publicvoidvalidateButton(){
previous.setEnabled((files!
(findex>
0));
next.setEnabled((files!
(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课程设计》清华大学
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 课程设计 图片 浏览器