Hi, Everyone
I am glad to know the availability of Java3D 1.2.
But who can tell me the difference between V1.2 and V1.1.3. Thanks a lot.
Just now, I replace the v1.1.3 with v1.2. My printUtility (print Canvas3D)
doesnot work.
I attatched my PrintableCanvas3D.java file here.
Thanks very much!
yangyuexiang
package gui;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.vecmath.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.awt.print.*;
class PrintableCanvas3D extends Canvas3D implements Printable{
BufferedImage printImage = null;
AffineTransform printTransform = null;
Raster printRaster;
boolean readRaster;
public PrintableCanvas3D(GraphicsConfiguration config)
{
super(config);
}
public int print( Graphics graphics, PageFormat pageFormat, int pageIndex)
{
if (pageIndex > 0)
{
printImage = null;
printTransform = null;
return Printable.NO_SUCH_PAGE;
}
if( printImage == null)
{
printImage = getBufferedImage();
Rectangle2D.Double pS = new
Rectangle2D.Double(pageFormat.getImageableX(), pageFormat.getImageableY(),
pageFormat.getImageableWidth(), pageFormat.getImageableHeight());
Rectangle2D.Double iS = new Rectangle2D.Double(0,
0,printImage.getWidth(null), printImage.getHeight(null));
double xScale = pS.getWidth()/iS.getWidth();
double yScale = pS.getHeight()/iS.getHeight();
printTransform = new AffineTransform();
// printTransform.translate(-iS.getX(), -iS.getY());
double aScale = xScale;
if(aScale > yScale)
aScale = yScale;
printTransform.scale(aScale, aScale);
printTransform.translate(pS.getX()/xScale, pS.getY()/yScale);
}
((Graphics2D)graphics).drawImage(printImage, printTransform, null);
return Printable.PAGE_EXISTS;
}
public BufferedImage getBufferedImage()
{
readRaster = true;
setSize( getWidth()-1, getHeight());
while( readRaster == true)
Thread.currentThread().yield();
return printRaster.getImage().getImage();
}
public void postSwap()
{
super.postSwap();
if ( readRaster)
{
BufferedImage bImage = new BufferedImage( getWidth(), getHeight(),
BufferedImage.TYPE_INT_ARGB);
ImageComponent2D imageComponent = new
ImageComponent2D(ImageComponent.FORMAT_RGB, bImage);
printRaster = new Raster(new
Point3f(0.0f,0.0f,0.0f),Raster.RASTER_COLOR, 0, 0, bImage.getWidth(),
bImage.getHeight(),imageComponent, null);
getGraphicsContext3D().readRaster( printRaster);
readRaster = false;
}
}
}