-1

I need to render a bunch of things that are big (3240x3240). How could I draw this image so that it is scaled to a reasonable size? I am double buffering.

update method:

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);
Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
stasiomod
  • 21
  • 3
  • For [example](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928), [example](http://stackoverflow.com/questions/19648833/java-jpanel-background-not-scaling/19648889#19648889), [example](http://stackoverflow.com/questions/18396302/how-to-scale-image-using-getscaledinstance/18396317#18396317) – MadProgrammer May 31 '14 at 01:18

1 Answers1

0

Sample code can be seen at 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));

    }
}
DavidPostill
  • 7,734
  • 9
  • 41
  • 60