通过下面的代码可以显示一张图片
   Icon logoImage=new ImageIcon(".\\photo\\cb2.jpg");
   lblPhoto=new JLabel(logoImage);
不过显示的图片是原来图片的大小,我想在程序中改变图片的大小,
Icon , ImageIcon都没有改变大小的方法,
        应该做?

解决方案 »

  1.   

    可以试试覆写 ImageIcon 的 paintIcon(Component c, Graphics g, int x, int y) 方法,设置 Graphics 的 scale。或者使用 ImageIO.read() 读取图片,然后缩放图像,再将缩放后的图像做为参数实例化 ImageIcon 。
      

  2.   

    随便写了点,不知道组件有没提供更简单的方法。
    // 1 读图
    BufferedImage image = ImageIO.read("xxx.jpg");// 2 图像长宽缩小一半       double scale = 0.5d;
     
    int width = (int)(image .getWidth() * scale );
    int height = (int)(image .getHeight() * scale );
    BufferedImage dstImage = new BufferedImage(width, height, srcImage.getType());AffineTransform affineTransform = new AffineTransform();
    affineTransform.scale(scale , scale);AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, hints);affineTransformOp.filter(image, dstImage);// 3 实例化 ImageIcon   
    Icon icon = new ImageIcon(dstImage);
      

  3.   

    AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, hints);hints 是绘制提示,这里可以替换成 null
      

  4.   

       还是有点问题, BufferedImage image = ImageIO.read(".\\photo\\cb2.jpg");
    ImageIO.read好象没有String类型.
    您看下错误说明嘛:E:\练习题\学生情况管理系统\java\photoDemo.java:29: 找不到符号
    符号: 方法 read(java.lang.String)
    位置: 类 javax.imageio.ImageIO
            BufferedImage image = ImageIO.read(".\\photo\\cb2.jpg");
                                                 ^
    注意: E:\练习题\学生情况管理系统\java\photoDemo.java 使用或覆盖了已过时的 API。
    注意: 要了解详细信息,请使用 -Xlint:deprecation 重新编译。   还有,我发现这样也不能弄成100*100固定大小是不是?,,很麻烦的,不行就不管它算了,
    大不了就把空间留大点让它显示了.
      

  5.   

    随便写下的,也没太仔细看,ImageIO.read()的确没有String类型参数,你为图片新建一个File就可以了。
    ImageIO.read(new File("xxx.jpg"));你需要 100*100 大小的缩略图,可以做一个换算。int width = 100(int)(image .getWidth() * scale );
    int height = 100(int)(image .getHeight() * scale );double scaleWidth = (double) width / (double) image.getWidth();
    double scaleHeight = (double) height / (double) image.getHeight();BufferedImage dstImage = new BufferedImage(width, height, image.getType());AffineTransform affineTransform = new AffineTransform();
    affineTransform.scale(scaleWidth , scaleHeight );
    affineTransformOp.filter(image, dstImage);
      

  6.   

    上面2行写错了,width和height直接赋值100
    int   width   =   100;
    int   height   =   100; 
      

  7.   

    其实缩放完全可以不用AffineTransform
    Graphics类中有一个drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) 可以实现缩放的功能,可以把图片平铺在一个Buffer上面
      

  8.   

    ..chenweionline您真的好厉害了,,,这可算弄出来了,呵呵,好高兴.
    这是我编译过的程序代码:
    import java.awt.*;
    import javax.swing.*;
    import java.io.IOException;
    import java.io.File;
    import javax.imageio.*;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    public class phDemo extends JFrame{
    JPanel panel;
        JLabel lblPhotoDemo;
        
        public phDemo() { 
    super("photo demo");
         panel=new JPanel();
         lblPhotoDemo=new JLabel();
     try{
    File file=new File( "E:\\练习题\\学生情况管理系统\\java\\photo\\cb1.jpg ");
    BufferedImage image = ImageIO.read(file);
        
    int width = 100; 
    int height = 100;   

    double  scaleWidth  =  (double) width / (double)  image.getWidth(); 
    double  scaleHeight  =  (double) height / (double)  image.getHeight(); 

    BufferedImage  dstImage  =  new BufferedImage(width, height, image.getType()); 

    AffineTransform  affineTransform  =  new  AffineTransform(); 
    affineTransform.scale(scaleWidth  ,  scaleHeight); 
    AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform,2);//
    affineTransformOp.filter(image, dstImage); 

    Icon logoImage1 = new ImageIcon(dstImage);
    lblPhotoDemo=new JLabel(logoImage1);  }catch(IOException e){
      System.out.println(e.getMessage());
     }
    panel.add(lblPhotoDemo);
         this.add(panel);
         this.setSize(600,500);
         this.show();
        }   
         public static void main(String[] args){
         new phDemo();
         }
    }