Here's a fairly easy solution, though it will be a bit slow.

Allocate one BufferedImage of a manageable size.  Call it a "tile".  Draw your 
huge image on that tile, relying on the graphics routines to automatically clip 
out the parts of the image that are outside the bounds of the small tile.  
Write the tile to a file.  Now clear out the tile, call Graphics2D.translate() 
to shift the drawing surface, and draw your huge image again into the tile.  
Since you shifted the drawing surface over, a different region of your huge 
diagram will get drawn onto the tile.

This will require calling your big draw function many times--once for each 
tile.  I'm pretty sure the Graphics2D routines are optimized to do nothing if 
you ask them to draw completely outside the bounds of the drawable surface 
though, so it might not be too slow.

Here's an example of the technique:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;


/*
  Create four 1000x1000 image files making up a virtual 2000x2000 image, 
  and draw a large circle on the virtual image by drawing the circle
  four times with different translation offsets.  Write the images to
  the files tile[1-4].png
*/
public class draw_tiles {
  
  public static void clearImage(Graphics2D g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 1000, 1000);
  }


  // this is where you'd put your huge draw routine that draws the graph
  public static void drawBigImage(Graphics2D g) {
    g.setColor(Color.BLACK);
    g.drawOval(0, 0, 2000, 2000);
  }
  
  
  public static void main(String[] args) throws Exception {
    BufferedImage image = 
      new BufferedImage(1000, 1000, BufferedImage.TYPE_BYTE_INDEXED);
    Graphics2D g = image.createGraphics();

    // save the state of the graphics context before shifting
    AffineTransform xform = g.getTransform();

    // draw the upper left tile
    clearImage(g);
    drawBigImage(g);
    ImageIO.write(image, "png", new File("tile1.png"));

    // shift to the right and draw the upper right tile
    clearImage(g);
    g.translate(-1000, 0);
    drawBigImage(g);
    ImageIO.write(image, "png", new File("tile2.png"));
    // restore the state of the graphics context so clearImage() works right
    g.setTransform(xform);

    // shift down and draw the lower left tile
    clearImage(g);
    g.translate(0, -1000);
    drawBigImage(g);
    ImageIO.write(image, "png", new File("tile3.png"));
    g.setTransform(xform);

    // shift down and right and draw the lower right tile
    clearImage(g);
    g.translate(-1000, -1000);
    drawBigImage(g);
    ImageIO.write(image, "png", new File("tile4.png"));
  }
}
[Message sent by forum member 'karrels' (karrels)]

http://forums.java.net/jive/thread.jspa?messageID=258288

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to