zhengyeshi的gravatar头像
zhengyeshi 2014-12-31 10:07:28

java swing为什么不显示图片?

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;

import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.util.logging.Filter;
import java.applet.*;
public class PicViewer extends JFrame implements ActionListener{
	JPanel pathBar,picContainer,ctrBar;
	JLabel pathInfo;
	JButton openbtn,prebtn,nextbtn,zoominbtn,zoomoutbtn,oripicbtn,angleleftbtn,anglerightbtn,exitbtn,deletebtn,savebtn;
	Image pic;//查看图片
	BufferedImage bufImage,filteredBufImage;//用于显示缓冲区图像
	BufferedImage oriBufImage;//原始缓冲区图像
	Graphics2D bufImageG;//缓冲区图像的图像环境
	String path,filename,dir;//需要查看图片的路径
	String[] pics;//目录下所有指定格式的图片
	int picWidth,picHeight;//图片大小
	int i = 0;
	final double ZOOM_PERCENT=0.15;//每次放大和缩小的倍率
	
	final String FILE_TYPE="jpg";
	boolean hasPic,isangled;
	double scaleX;//图像水平方向的缩放因子
	double scaleY;//图像垂直方向的缩放因子
	double radius;//旋转角度
	Filter filterJpg;
	
	public PicViewer(){
		JFrame frame=new JFrame("tupianchakanqi");
		pathBar= new JPanel();
		picContainer= new JPanel();
		ctrBar= new JPanel();
		pathInfo= new JLabel();
		frame.add(pathInfo);
		frame.setBounds(200, 100, 1000, 540);
		frame.setVisible(true);
		//顶部路径栏		
		pathBar.setLayout(new BorderLayout());
		
		
		pathBar.add(pathInfo);
		
		pathBar.setVisible(true);
		//图片显示区域
		
		picContainer.setBackground(Color.LIGHT_GRAY);
		picContainer.setVisible(true);
		
		//底部控制栏
		
		openbtn= new JButton("打开");
		openbtn.addActionListener(this);
		savebtn = new JButton("保存");
		savebtn.addActionListener(this);
		prebtn= new JButton("上一张");
		prebtn.addActionListener(this);
		nextbtn= new JButton("下一张");
		nextbtn.addActionListener(this);
		zoominbtn= new JButton("放大");
		zoominbtn.addActionListener(this);
		zoomoutbtn= new JButton("缩小");
		zoomoutbtn.addActionListener(this);
		oripicbtn = new JButton("原始图片");
		oripicbtn.addActionListener(this);
		angleleftbtn= new JButton("左旋");
		angleleftbtn.addActionListener(this);
		anglerightbtn= new JButton("右旋");
		anglerightbtn.addActionListener(this);
		deletebtn= new JButton("删除");
		deletebtn.addActionListener(this);
		exitbtn= new JButton("退出");
		exitbtn.addActionListener(this);
		
		ctrBar.add(openbtn);
		ctrBar.add(zoominbtn);
		ctrBar.add(zoomoutbtn);
		ctrBar.add(oripicbtn);
		ctrBar.add(prebtn);
		ctrBar.add(nextbtn);
		ctrBar.add(angleleftbtn);
		ctrBar.add(anglerightbtn);
		ctrBar.add(deletebtn);
		ctrBar.add(savebtn);
		ctrBar.add(exitbtn);
		setLayout(new BorderLayout());
		frame.add(ctrBar,BorderLayout.SOUTH);
	//界面总布局	
	//	
		frame.add(pathBar,BorderLayout.NORTH);
		frame.add(picContainer,BorderLayout.CENTER);
		
	}
	
	public void actionPerformed(ActionEvent e){
		JButton click=(JButton)e.getSource();//获取按键源
		if(click==openbtn){
			open();
		}
		if(click==prebtn&&pic!=null){
			pre();
		}
		if(click==nextbtn&&pic!=null){
			next();
		}
		if(click==zoominbtn&&pic!=null){
			pathInfo.setForeground(Color.BLACK);
			pathInfo.setText("放大图片");
			zoomin();
		}
		if(click==zoomoutbtn&&pic!=null){
			pathInfo.setForeground(Color.BLACK);
			pathInfo.setText("缩小图片");
			zoomout();
		}
		if(click==oripicbtn&&pic!=null){
			oripic();
		}
		if(click==angleleftbtn&&pic!=null){
			angleleft();
		}
		if(click==anglerightbtn&&pic!=null){
			angleright();
		}
		if(click==exitbtn){
			exit();
		}
		if(click==deletebtn&&pic!=null){
			pathInfo.setForeground(Color.BLACK);
			pathInfo.setText("删除图片");
			delete();
		}
		if(click==savebtn&&pic!=null){
			pathInfo.setForeground(Color.BLACK);
			pathInfo.setText("复制图片");
			try{
				save();
			}catch(Exception ee){}
			
		}
	}
	
	//打开需要查看的图片文件
	public void open(){
		JFileChooser chooser= new JFileChooser();
		//只显示文件夹和jpg格式的文件
		//建立过滤器 只显示jpg
		FileNameExtensionFilter filter= new FileNameExtensionFilter("*.JPG","jpg");
		chooser.setFileFilter(filter);
		chooser.setCurrentDirectory(new File(""));
		Component msg = null;
		int returnVal=chooser.showOpenDialog(msg);
		//根据返回值判断是否选择了文件
		if(returnVal==JFileChooser.APPROVE_OPTION){
			//获取文件名
			filename=chooser.getSelectedFile().getName();
			//获取文件路径
			path=chooser.getCurrentDirectory().getPath();
			//获取文件路径下所有jpg文件
			PicViewer.Filter filterJpg = new Filter(FILE_TYPE);
			File file=new File(path);
			pics=file.list();
			pics=file.list(filterJpg);
			//跟中选中的图片
			for(int j=0;j<pics.length;j++)
				if(pics[j].equalsIgnoreCase(filename))
				{
					i=j;
					break;
				}
			dir=path+"\\"+filename;
			hasPic=true;
			loadImage(dir);
			pathInfo.setForeground(Color.BLACK);
			pathInfo.setText(path+"\\"+filename);
			prebtn.setEnabled(true);
			nextbtn.setEnabled(true);
			zoominbtn.setEnabled(true);
			zoomoutbtn.setEnabled(true);
			angleleftbtn.setEnabled(true);
			anglerightbtn.setEnabled(true);
			oripicbtn.setEnabled(true);
			deletebtn.setEnabled(true);
			savebtn.setEnabled(true);
		}
		else{
			hasPic=false;
			pathInfo.setForeground(Color.RED);
			pathInfo.setText("没有选择文件");
		}
	}
	
	//保存文件
	public void save()throws IOException{
		JButton msgsave= new JButton();
		JFileChooser chooser= new JFileChooser();//只显示文件夹和jpg格式的文件
		FileNameExtensionFilter filter= new FileNameExtensionFilter("*.JPG","jpg");//建立过滤器
		chooser.setFileFilter(filter);
		File fin= new File(path+"\\"+pics[i]);
		File f= new File(""+"\\"+pics[i]);
		chooser.setSelectedFile(f);
		int returnVal=chooser.showSaveDialog(msgsave);
		//根据返回值判断是否选择了文件路径
		if(returnVal==JFileChooser.APPROVE_OPTION){
			path=chooser.getCurrentDirectory().getPath();//获取文件路径
			File fileout = new File(path+"\\"+chooser.getSelectedFile().getPath());
			FileInputStream isr= new FileInputStream(fin);
			FileOutputStream fout= new FileOutputStream(fileout);
			byte b[]= new byte[200];
			int count= 0;
			while((count=isr.read(b, 0, 200))!=-1)
				fout.write(b, 0, count);
			isr.close();
			fout.flush();
			fout.close();
		}
	}
	
	//上一张
	public void pre(){
		//循环浏览图片
		if(i==0){
			i=pics.length-1;
		}
		else{
			i--;
		}
		dir=path+"\\"+pics[i];
		pathInfo.setText(dir);
		hasPic=true;
		isangled=false;
		scaleX=1.0;
		scaleY=1.0;
		loadImage(dir);
		oripicbtn.setEnabled(false);
	}
	
	//查看下一张
	public void next(){
		if(i==pics.length-1){
			i=0;
		}else{
			i++;
		}
		dir=path+"\\"+pics[i];
		pathInfo.setText(dir);
		hasPic=true;
		isangled=false;
		scaleX=1.0;
		scaleY=1.0;
		loadImage(dir);
		oripicbtn.setEnabled(false);
	}
	
	//放大图片
	public void zoomin(){
		if(scaleX<1.7){
			scaleX+=ZOOM_PERCENT;
			scaleY+=ZOOM_PERCENT;
		}else{
			scaleX=1.8;
			scaleY=1.8;
			pathInfo.setForeground(Color.RED);
			pathInfo.setText("已经放大最大");
		}
		hasPic=true;
		picTrans();
		repaint();//重绘组件
		pathInfo.setForeground(Color.BLACK);
		pathInfo.setText(dir);
		if(scaleX==1){
			oripicbtn.setEnabled(false);
		}else{
			oripicbtn.setEnabled(true);
		}
	}
	
//缩小图片	
	public void zoomout(){
		if(scaleX>0.8){
			scaleX-=ZOOM_PERCENT;
			scaleY-=ZOOM_PERCENT;
		}else{
			scaleX=0.2;
			scaleY=0.2;
			pathInfo.setForeground(Color.RED);
			pathInfo.setText("已经缩至最小");
		}
		hasPic=true;
		picTrans();
		repaint();//重绘组件
		pathInfo.setForeground(Color.BLACK);
		pathInfo.setText(dir);
		if(scaleX==1){
			oripicbtn.setEnabled(false);
		}else{
			oripicbtn.setEnabled(true);
		}
	}
	
	//回复图片原始状态
	public void oripic(){
		scaleX=1;
		scaleY=1;
		radius=0;
		hasPic=true;
		picTrans();
		repaint();//重绘组件
		pathInfo.setForeground(Color.BLACK);
		pathInfo.setText(path+"\\"+filename);
		oripicbtn.setEnabled(false);
	}
	
	//将图片逆时针旋转
	public void angleleft(){
		isangled=true;
		radius+=-Math.PI/2;
		repaint();
		oripicbtn.setEnabled(true);
	}
	
	//将图片顺时针旋转
	public void angleright(){
		isangled=true;
		radius+=Math.PI/2;//每次右旋转45度
		repaint();
		oripicbtn.setEnabled(true);
	}
	
	//删除图片
	public void delete(){
		File f= new File(path+"\\"+pics[i]);
		if(f.exists())
			f.delete();
		else{
			pathInfo.setForeground(Color.BLACK);
			pathInfo.setText("文件不存在");
		}
		for(int j=i;j<pics.length-1;j++)
			pics[j]=pics[j+1];
		if(i==pics.length-1)
			i=0;
		dir=path+"\\"+pics[i];
		loadImage(dir);
	}
	
	//退出图片查看器
	public void exit(){
		hasPic=false;
		System.exit(0);
	}
	
	//加载图像
	public void loadImage(String fileName){
		pic=this.getToolkit().getImage(fileName);//取得图像
		MediaTracker mt= new MediaTracker(this);//实例化媒体加载器,跟踪多种媒体对象状态
		mt.addImage(pic, 0);//增加图像到加载器
		picWidth=pic.getWidth(this);
		picHeight=pic.getHeight(this);
		try{
			mt.waitForAll();//等待图片加载
		}catch(Exception e){
			e.printStackTrace();//输出错误信息
		}
		
		//创建原始缓冲区图像
		 oriBufImage = new BufferedImage(pic.getWidth(this),pic.getHeight(this),BufferedImage.TYPE_INT_ARGB);
		bufImage= oriBufImage;
		 bufImageG = bufImage.createGraphics();//创建bufImage的图形环境
		bufImageG.drawImage(pic, 0, 0, this);//传输源图像数据到缓冲区
		repaint();//重绘组件
	}
	
	//图像转换处理
	public void picTrans(){
		if(bufImage==null)
			return;//如果bufImage为空则直接返回
		//过滤后的图像
		BufferedImage fileteredBufImage = new BufferedImage((int)(pic.getWidth(this)*scaleX),(int)(pic.getHeight(this)*scaleY),BufferedImage.TYPE_INT_ARGB);
		AffineTransform transform= new AffineTransform();//2D放射变换
		transform.setToScale(scaleX, scaleY);//设置放射变换的比例因子
		AffineTransformOp imageOp = new AffineTransformOp(transform,null);//创建放射变换操作对象
	   imageOp.filter(oriBufImage, filteredBufImage);
	   bufImage=filteredBufImage;
		//	BufferedImage oriBufImage;
	//	BufferedImage filteredBufImage;
		imageOp.filter(oriBufImage,filteredBufImage);
		bufImage=filteredBufImage;
	}
	
	//重写update(),消除闪烁
	public void update(Graphics g){
		this.paint(g);
	}
	
	//图片绘制
	public void paint(Graphics g){
		int panelWidth= this.getWidth();
		int panelHeight= this.getHeight();
		super.paint(g);
		//判断是否需要旋转图片
		if(bufImage!=null &&isangled==true){
			Graphics2D g2= (Graphics2D)g;
			AffineTransform tx= new AffineTransform();
			//按照角度,根据中心旋转
			tx.rotate(radius,panelWidth/2,panelHeight/2);
			g2.setTransform(tx);
			picTrans();
			g2.drawImage(bufImage,(panelWidth-bufImage.getWidth())/2,(panelHeight-bufImage.getHeight())/2,this);//绘制图片
			pathInfo.setForeground(Color.BLACK);
			pathInfo.setText(path+"\\"+filename);
			isangled=false;
		}
		else if(bufImage!=null){
			Graphics2D g2=(Graphics2D)g;
			picTrans();
			g2.drawImage(bufImage,(panelWidth-bufImage.getWidth())/2,(panelHeight-bufImage.getHeight())/2,this);//绘制图片
			
		}
	}
	
	//文件类型
	public class Filter implements FilenameFilter{
		String extension;
		Filter(String extension){
			this.extension=extension;
		}
		public boolean accept(File directory,String filename){
			return filename.endsWith(extension);
		}
	}
	/**
	 * @param args
	 */
	
	
		public static void main(String args[])
			throws Exception{   
				PicViewer a=new PicViewer();   
			  }

	/**	@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
		}	
*/
}


	  
	  
所有回答列表(0)
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友