.课程设计的任务及要求
二.需求分析
图形化界面(GUI)编程,编写一个图片浏览器程序可以支持“ “.GIF”,“.JPEG',“.jpeg ”,“.TGA',“.JPG”,“.jpg ”等格式,单张打开图片,可以将同一目录下的图片按缩略图打开按“上一张”“下一张”按钮可以显示相应
图片。运行Applet时,图像不是一气呵成的,因为方法不是吧图像完整的装入内存再显示的。于此相反,方法创建一个线程,该线程与Applet的原有线程并
发执行,一边装入一边显示,从而产生上了不联需显示的现象。为了提高图像才
显示效果,可以采用双缓冲技术:首先把图像装入内存,然后再显示在屏幕上。
三.设计思路
3.1界面设计
选择图片按钮:主要用dir函数实现图片的遍历。上一张,下一张:通过做标轴回调函数实现。
由于本软件为单机软件,不需要大量的数据读写和数据交换,实现上、下功能要求只能读取PictureBox控件当前加载的目录,读取当前路径,创建一维数组。
frame = n ewFrame( "Pictureviewer");
Panel pb = n ewPa nel();
Button select = 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);
double image_proporti on = 1.0 * image_height / image_width;
System.out.pri ntl n("image: w "+image_width+" ,h "+image_height+" ,p1 "+image_proportion);
if(image_proporti on > scree n_ proportio n){ image_height = scree n_height;
image_width = (i nt)(image_height / image_proportio n); System.out.pri ntl n(” p1>p0 w= "+image_width); }else{ image_width = scree n_width; image_height = (in
t)(image_width * image_proporti on); System.out.pri ntln (” p0>p1 h=
"+image_height); }
四.详细设计
4.1.程序设计流程图
4.2. 源程序代码
package C;
import java.io.File;
import java.io.File nameFilter;
public class MyFilter impleme nts File nameFilter{ private Strin g[] exte nsion; public MyFilter(){
extension = newString[]{".jpg", ".JPG", ".gif", ".GIF", ".png", ".PNG", ".jpeg", ".JPEG"};
}
public MyFilter(Stri ng[] exte nsio n){
< nsion = exte nsion;
}
public boolea n accept(File dir,Stri ng n ame){
for(Stri ng s : exte nsion){
if(n ame.e ndsWith(s)){
return true;
}
}
return false;
}
}
package C;
import java.awt.*;
import java.awt.eve nt.*;
import java.awt.image.*;
public class MyCa nvas exte nds Can vas impleme nts Comp onen tListe ner{ */
private static fin al l ong serialVersi on UID = 1L;
private Bufferedlmage bi;
private Image im;
private int image_width;
private int image_height;
public void setImage(BufferedImage bi){
this.bi = bi;
<();
}
public void pain t(Graphics g){
g.drawlmage(im,(Width()-image_width)/2,(Height()-imag
e_height)/2,this);
}
public void comp onen tResized(Comp onen tEve nt e){
if(bi != null){
System.out.pri ntln ("resize!!");
<();
}
}
public void comp onen tMoved(Comp onen tEve nt e){}
public void comp onen tShow n( Comp onen tEve nt e){}
public void componentHidden(ComponentEvent e){}
public void zoom(){
if(bi == n ull)
return;
int scree n_width = Width();
int scree n_height = Height();
double scree n_proporti on = 1.0 * scree n_height / scree n_width;
System.out.pri ntl n("scree n: w "+scree n_width+" ,h "+scree n_height+" ,p0 "+scree n_proporti on);
image_width = bi.getWidth(this);
image_height = bi.getHeight(this);
double image_proporti on = 1.0 * image_height / image_width;
System.out.pri ntl n("image: w "+image_width+" ,h "+image_height+" ,p1 "+image_proporti on);
if(image_proporti on > scree n_proportio n){
image_height = scree n_height;
image_width = (i nt)(image_height / image_proportio n);
System.out.pri ntl n(” p1>p0 w= "+image_width);
}else{
image_width = scree n_width;
image_height = (in t)(image_width * image_proporti on);
System.out.pri ntln (” p0>p1 h= "+image_height);
} _
im = bi.getScaledl nsta nce(image_width,image_height,lmage.SCALE_SMOOTH);
} ~ ~ 一
}
package C;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
public class T implements ActionListener{
private private
Frame frame; MyCa nvas mc;
private Stri ng fpath ; private Stri ng fn
ame; private File[] files ;
private int findex ;
private FileDialog fd_load ;
private MyFilter filter ;
private Butt on previous ;
private Butt on n ext ;
public static void main( String args[]) throws Exception { n ewT().i ni t();
}
public void in it(){
frame = n ewFrame( "Pictureviewer");
Panel pb = n ewPa nel();
Button select = newButton("选择图片"); previous = newButton("上——张"); next = newButton("下一张”);
select.addActionListener( this );
previous .addActionListener( this );
next .addActionListener( this );
pb.add(select);
pb.add( previous );
pb.add( n ext);
mc= n ewMyCa nvas(); mcsetBackground( newColor(200,210,230));
mcaddComponentListener( m();
frame.add(pb, "North");
frame .add( mc "Center");
frame .setSize(360,360);
frame .setLocati on (400,200);
frame .addWindowListener( newWindowAdapter(){
public void windowClosing(WindowEvent e){
System. exit (0);
}
});
frame.setVisible( true );
this .validateButton();
filter = newMyFilter();
fd」oad = n ewFileDialog( frame,"打开文件”,FileDialog. LOAD
fd」oad .setFilenameFilter( filter );
} _
public void action Performed(Acti on Eve nt e){
Stri ng comma nd = e.getActio nComma nd();
if (command.equals(” 选择图片")){
fd」oad .setVisible( true );
fpath = fd」oad .getDirectory();
fname = fd_load .getFile();
if ((fpath != null ) && ( fname != null )){java浏览器下载

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。