日期:2014-05-20 浏览次数:20891 次
import javax.imageio.*; import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.io.*; public class ImgP extends JFrame{ ImageCanvas ic; final double culculateRatio(int width, int height){//计算缩放比例 double ratio; if(getWidth()>=width && getHeight()>=height){ ratio = 1; return ratio; } if((double)getWidth()/width > (double)getHeight()/height) ratio = (double)getHeight() / height; else ratio = (double)getWidth() / width; return ratio; } public ImgP(){ Image image; double ratio; setSize(700, 500); try { image = ImageIO.read(new File("G:\\Image\\IMG_0783.jpg")); ratio = culculateRatio(image.getWidth(null),image.getHeight(null)); ic = new ImageCanvas(ratio); ic.setImage(image); //如果将add(ic)一行替换成以下代码,画图有误 //JPanel panel = new JPanel(); //panel.add(ic); //add(panel); add(ic); ic.concateZoom(ratio); } catch (IOException ex) { } } public static void main(String[] args) { ImgP ip = new ImgP(); ip.setVisible(true); ip.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class ImageCanvas extends JComponent{ Image img; double ratio; AffineTransform af = AffineTransform.getScaleInstance(1,1); public ImageCanvas(double ratio){ this.ratio = ratio; } void setImage(Image m){ img = m; paintImmediately(getBounds()); //concateZoom(0.3); } void concateZoom(double scale){ if (img==null)return; af.preConcatenate(AffineTransform.getScaleInstance(scale,scale)); paintImmediately(getBounds()); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (img==null) { g.setColor(Color.BLACK); g.fillRect(0,0,getWidth(),getHeight()); } else { Graphics2D g2d = (Graphics2D)g; g2d.setTransform(af); g2d.drawImage(img, (int)(((getWidth()-img.getWidth(null)*ratio)/2)/ratio), (int)(((getHeight()-img.getHeight(null)*ratio)/2)/ratio), this); } } @Override public Dimension getPreferredSize() {return new Dimension(600, 400);} }