⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compressfilepane.java

📁 压缩目录
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.text.Format;


public class CompressFilePane extends JPanel {

	/**
	 * 
	 */
	private JLabel txtLbl=new JLabel();
	private static final long serialVersionUID = 1L;
//	private JProgressBar jpb=new JProgressBar();
	private long oriSize;
	private long comSize;
	private float comRate;
	private String unit="byte";
	
	private int h_margin=30;
	private int w_margin=20;
	private int span=35;
	private boolean isSet=false;
	private boolean isSetUnit=false;	
	private String path=null;
	
	public CompressFilePane(){
		/*	jpb.setValue(30);
		add(jpb);
		jpb.setVisible(true);*/
		this.add(txtLbl);
	}
	
	public void setPath(String path){
		this.path=path;
		
	}
	
	public void setOriginalSize(long ol){
		oriSize=ol;
		isSet=true;
	}
	
	
	public void setCompressedSize(long cl){
		comSize=cl;
		isSet=true;
		
	}
	
	public void setCompressedRate(float r){
		comRate=r;
	}
	
	private long changeFormat(long i){
		if(unit=="GB"){//65536
			i/=1024*1024*1024;			
		}
		else if(unit=="MB"){
			i/=1024*1024;			
		}
		else if(unit=="KB"){
			i/=1024;			
		}
		return i;
	}
	
	public void setUnit(){
		if(this.getCompressedSize()>1024*1024*1024)
			unit="GB";
		else if(this.getCompressedSize()>1024*1024)
			unit="MB";
		else if(this.getCompressedSize()>1024)
			unit="KB";
		isSetUnit=true;
	}
	
	
	public int getOriginalSize(){
		return (int)oriSize;
	}
	
	public int getCompressedSize(){
		return (int)comSize;
	}
	
	public int getCompressedRate(){
		return Math.round(comRate);
		
	}
	
	public void drawAxis(Graphics g) //draw X,Y axis
	{
		/*
		Polygon py=new Polygon();
		Polygon px=new Polygon();
		py.addPoint(w_margin-5, h_margin);
		py.addPoint(w_margin+5, h_margin);
		py.addPoint(w_margin, h_margin-10);		
		px.addPoint(this.getWidth()-w_margin-10, this.getHeight()-h_margin-5);
		px.addPoint(this.getWidth()-w_margin-10, this.getHeight()-h_margin+5);
		px.addPoint(this.getWidth()-w_margin, this.getHeight()-h_margin);
		g.fillPolygon(px);
		g.fillPolygon(py);
		
		g.drawLine(w_margin, this.getHeight()-h_margin, w_margin, h_margin);//y-axis
		*/
		
		
		g.drawLine(w_margin, this.getHeight()-h_margin,this.getWidth()-w_margin, this.getHeight()-h_margin);//x-axis
		
		
	
		
	}
	
	
	
	
	public void paint(Graphics g){
		super.paint(g);
		if(isSet){
			
			if(!isSetUnit){
				this.setUnit();
				comSize=changeFormat(comSize);
				oriSize=changeFormat(oriSize);
				System.out.println("comSize: "+comSize);
				System.out.println("oriSize: "+oriSize);
			}			
			int sum=this.getCompressedSize()+this.getOriginalSize();
			txtLbl.setText("<html><b>The size of Original file is:</b>"+this.getOriginalSize()+this.unit+"<br>" +
					"<b>The size of compressed file is:</b>"+this.getCompressedSize()+this.unit+"<br>"
					+"<b>The rate of compress is:</b>"+this.getCompressedRate()+"%<br>"+
					"</html>");
			this.drawAxis(g);
			Graphics2D g2d=(Graphics2D)g;
			g2d.setColor(Color.RED);
			g2d.fill3DRect(this.getWidth()/2-this.w_margin-span*2, this.getHeight()-this.h_margin-this.getCompressedSize()*100/sum, span,this.getCompressedSize()*100/sum , true);
			g2d.fill3DRect(this.getWidth()/2-this.w_margin+span, this.getHeight()-this.h_margin-this.getCompressedSize()*100/sum, span, this.getCompressedSize()*100/sum, true);
			g2d.setColor(Color.BLUE);
			g2d.fill3DRect(this.getWidth()/2-this.w_margin+span, this.getHeight()-this.h_margin-this.getOriginalSize()*100/sum, span, (this.getOriginalSize()-this.getCompressedSize())*100/sum, true);
			g2d.setColor(Color.MAGENTA);
			g2d.drawString("Compressed", this.getWidth()/2-this.w_margin-span*2-10, this.getHeight()-10);
			g2d.drawString(this.getCompressedSize()+this.unit, this.getWidth()/2-this.w_margin-span*2-10, this.getHeight()-this.h_margin-this.getCompressedSize());
			g2d.drawString("Origninal", this.getWidth()/2-this.w_margin+span-10, this.getHeight()-10);
			g2d.drawString(this.getCompressedSize()+this.unit, this.getWidth()/2-this.w_margin+span-10, this.getHeight()-this.getOriginalSize());
			g2d.drawString("Rate"+this.getCompressedRate()+"%", this.getWidth()/2-w_margin, this.getHeight()/2);
		}
		else if(path!=null){
		//	g.drawString("The directory going to be archived is:", 50, 50);
		//	g.drawString(path, 50, 70);
			txtLbl.setText("<html><b>The directory going to be archived is:</b></p><br>"+path+"</p></html>");
		}			
		else
			txtLbl.setText("<html><b><h2>Welcome to myArchiver</h2></b></html>");
		
		
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -