Java applet:将图像缩放到合理的大小

时间:2014-05-31 00:58:33

标签: java applet render

我需要渲染一大堆(3240x3240)的东西。我如何绘制此图像以使其缩放到合理的大小?我是双缓冲。

更新方法:

super.update(g);
//Verify image exists
if (doubleBufferImage == null){
    doubleBufferImage = createImage(this.getSize().width, this.getSize().height);
doubleBufferGraphics = doubleBufferImage.getGraphics();
}
//Update Bufer Image
doubleBufferGraphics.setColor(getBackground());
doubleBufferGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
    doubleBufferGraphics.setColor(getForeground());
paint(doubleBufferGraphics);
g.drawImage(doubleBufferImage, x, y, this.getSize().width, this.getSize().height, this);

1 个答案:

答案 0 :(得分:0)

示例代码可以在Java Demos

看到
/*
This method is inherited to BufferedImage class from java.awt.Image class
public Image getScaledInstance(int width,int height,int hints);
*/

import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
class ResizeImage
{
    public static void main(String args[]) throws Exception
    {
        // The first argument is the input file
        String file=args[0];

        // Take the output file
        String output=args[1];

        // Without extension? Go back
        if(!output.contains(".")) return;

        // Take the width,height as 2,3 args
        int w=Integer.parseInt(args[2]);
        int h=Integer.parseInt(args[3]);

        // Get the BufferedImage object by reading the image
        // from the given input stream
        BufferedImage bim=ImageIO.read(new FileInputStream(file));

        // I am using fast scaling
        Image resizedImg=bim.getScaledInstance(w,h,Image.SCALE_FAST);

        // Create a BufferedImage object of w,h width and height
        // and of the bim type
        BufferedImage rBimg=new BufferedImage(w,h,bim.getType());

        // Create Graphics object
        Graphics2D g=rBimg.createGraphics();

        // Draw the resizedImg from 0,0 with no ImageObserver
        g.drawImage(resizedImg,0,0,null);

        // Dispose the Graphics object, we no longer need it
        g.dispose();

        // Now, what? Just write to another file

        // The first argument is the resized image object
        // The second argument is the image file type, So i got the
        // extension of the output file and passed it
        // The next argument is the FileOutputStream to where the resized
        // image is to be written.
        ImageIO.write(rBimg,output.substring(output.indexOf(".")+1),new FileOutputStream(output));

    }
}
相关问题