如何将一个较大的图像加载到一个面积较小的组件上,就是希望图像能按比例缩小。

解决方案 »

  1.   

    用StretchBlt贴图到,你要使用的组件上。
      

  2.   

    to   smzh8(王新国):
          你能再说详细点吗,举个例子怎么弄,万分感谢。
      

  3.   

    import java.awt.BorderLayout;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;public class LoadImageTest
    {
    public static void main(String[] args) throws IOException 
    {
    Image image = loadImage(new File("C:/test.jpg"), 1.2);
    ImageIcon icon = new ImageIcon(image); JLabel label = new JLabel(icon);
    JFrame f = new JFrame();
    f.getContentPane().add(label, BorderLayout.CENTER);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true); } private static Image loadImage(File imgFile, double scale) throws IOException
    {
    BufferedImage image = ImageIO.read(imgFile);
    RenderingHints renderingHints = new RenderingHints(
    RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); AffineTransformOp scaleOp = new AffineTransformOp(
    AffineTransform.getScaleInstance(scale, scale), renderingHints);
    BufferedImage targetImage = new BufferedImage(
    (int)(image.getWidth() * scale), 
    (int)(image.getHeight() * scale), image.getType());
    scaleOp.filter(image, targetImage);
    return targetImage;
    }
    }
      

  4.   

    我的意思是,如果我有一个100 * 100 的图像,现在我希望能将其按比例在10*10大小的组件上显示,比如说在JLabel 上,显示的图像希望是按比例缩小的完整的图像,该如何实现,谢谢。
      

  5.   

    Image 类中有一个getScaledInstance方法:public Image getScaledInstance(int width,
                                   int height,
                                   int hints)
    Creates a scaled version of this image. A new Image object is returned which will render the image at the specified width and height by default. The new Image object may be loaded asynchronously even if the original source image has already been loaded completely. 
    If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used. 
    Parameters:
    width - the width to which to scale the image.
    height - the height to which to scale the image.
    hints - flags to indicate the type of algorithm to use for image resampling. 
    Returns:
    a scaled version of the image. 
      

  6.   

    其实我就是想问,java里有没有什么类提供了方法可以用来缩放图像的,或是有其它什么方法可以做到