thanks to previous email from Don Casteel working from Mr. Kunszt's posted
class with my own small addition:
import com.mincom.util.resource.Resource;
import com.sun.j3d.utils.image.*;
import java.awt.print.*;
import java.awt.print.PrinterJob;
import java.awt.print.PrinterGraphics;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.net.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.filechooser.*;
import com.sun.image.codec.jpeg.*;
/** Class PrintCaptureCanvas3D, using the instructions from the Java3D
FAQ pages on how to capture a still image in jpeg format.
A capture button would call a method that looks like
public static void captureImage(PrintCaptureCanvas3D c)
{
c.writeJPEG_ = true;
c.saveJPEG_ = true;
c.repaint();
};
A print button would call a method that looks like
public static void printJPGImage(PrintCaptureCanvas3D c)
{
c.writeJPEG_ = true;
c.saveJPEG_ = false;
c.repaint();
};
Peter Z. Kunszt
Johns Hopkins University
Dept of Physics and Astronomy
Baltimore MD
Working from Mr. Kunszt's posted class, the following enhancements were
added:
BufferedImage img; was made global to the class.
The image width was made dynamic using this.getWidth() & this.getHeight()
methods.
Printing capability was added (this is still buggy however)
Don Casteel 10/24/1999
[EMAIL PROTECTED]
and of course set to match our environment + specific need by myself
[EMAIL PROTECTED]
*/
public class PrintCaptureCanvas3D extends Canvas3D implements Printable,
ImageObserver
{
private class ExampleFileFilter extends FileFilter {
/**
* Creates a file filter that accepts the jpg file.
*
*
* @see #addExtension
*/
public ExampleFileFilter() {
}
/**
* Return true if this file should be shown
* @see #getExtension
* @see FileFilter#accepts
*/
public boolean accept(File f) {
if (f != null) {
if (f.isDirectory()) {
return true;
}
String extension = getExtension(f);
if (extension != null) {
return extension.equals("jpg");
}
}
return false;
}
/**
* Return the extension portion of the file's name .
*
* @see FileFilter#accept
*/
public String getExtension(File f) {
if(f != null) {
String filename = f.getName();
int i = filename.lastIndexOf('.');
if(i > 0 && i < filename.length() - 1) {
return filename.substring(i + 1).toLowerCase();
};
}
return null;
}
public String getDescription() {
return "JPEG Image Files (*.jpg)";
}
}
// The resource bundle used to store the siteview configuration items
// such as the map info et cetera.
public static final String SITE_VIEW_RESOURCES =
"com.mincom.works.location.LocationViewer";
public boolean swapCompleted = false;
public boolean writeJPEG_;
private int postSwapCount_;
public boolean saveJPEG_;
BufferedImage img;
public PrintCaptureCanvas3D(GraphicsConfiguration gc)
{
super(gc);
postSwapCount_ = 0;
}
public void addNotify()
{
super.addNotify();
};
public void postSwap()
{
if(writeJPEG_)
{
GraphicsContext3D ctx = getGraphicsContext3D();
// The raster components need all be set!
Raster ras = new Raster
(
new Point3f(-1.0f,-1.0f,-1.0f),
javax.media.j3d.Raster.RASTER_COLOR,
0,0,
this.getWidth(),this.getHeight(),
new ImageComponent2D
(
ImageComponent.FORMAT_RGB,
new BufferedImage
(
this.getWidth(),
this.getHeight(),
BufferedImage.TYPE_INT_RGB
)
),
null
);
ctx.readRaster(ras);
// Now strip out the image info
img = ras.getImage().getImage();
// write that to disk....
if(saveJPEG_) {
try {
JFileChooser chooser = new JFileChooser(".");
chooser.setDialogTitle(Resource.getString(SITE_VIEW_RESOURCES,
"snapshottitle"));
chooser.setSelectedFile(new
File("Capture"+postSwapCount_+".jpg"));
chooser.setFileFilter(new ExampleFileFilter());
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
int returnVal = chooser.showOpenDialog(this);
FileOutputStream out;
if (returnVal == JFileChooser.APPROVE_OPTION) {
out = new
FileOutputStream(chooser.getSelectedFile());
} else
writeJPEG_ = false;
return;
}
JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(img);
param.setQuality(0.9f,false); // 90% qualith JPEG
encoder.setJPEGEncodeParam(param);
encoder.encode(img);
writeJPEG_ = false;
out.close();
}
catch ( IOException e ) {
Pkg.log.trace(0, e);
};
} else {
printMethod();
writeJPEG_ = false;
};
postSwapCount_++;
}
swapCompleted = true;
super.postSwap();
};
public int print(Graphics g, PageFormat pf, int pi)
throws PrinterException
{
if (pi >= 1)
{
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
drawObject(g2d);
return Printable.PAGE_EXISTS;
};
public void drawObject(Graphics2D g2d)
{
try {
g2d.drawImage(img,new AffineTransform(), this);
}
catch (Exception ex) {
Pkg.log.trace(0, ex);
};
};
public void printMethod(){
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printJob.defaultPage();
pageFormat = printJob.validatePage(pageFormat);
printJob.setPrintable(this);
if (printJob.printDialog())
{
try {
printJob.print();
} catch (Exception ex) {
Pkg.log.trace(0, ex);
};
};
};
}
Olivier Fillon Minestar Project
[EMAIL PROTECTED] Mincom Limited
Ph.+61-7-3303-3344 61 Wyandra Street
Fax+61-7-3303-3232 Teneriffe Qld. 4005.
Australia
Company home page: http://www.mincom.com/
Personal home page: http://www.powerup.com.au/~fillon
--- A word from our sponsor ---
This transmission is for the intended addressee only and is confidential
information. If you have received this transmission in error, please delete
it and notify the sender. The contents of this E-mail are the opinion of the
writer only and are not endorsed by Mincom Limited unless expressly stated
otherwise.
--
-----Original Message-----
From: Juan Miguel <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: Friday, February 25, 2000 12:23 PM
Subject: [JAVA3D] Capturing the Canvas3D
>Hi everybody:
>
>I'd like to know how can I capture an image from a Canvas3D and store it in
>any format.
>Imagine that I have a frame along with its JPanel object, which contains a
>Canvas3D which displays a 3D cube, and I want to now how can I save the
>cube's image via programming, and write it either into a JPG file or into a
>GIF file
>
>Thanks for any help
>
>Juan Miguel
>
>===========================================================================
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
>of the message "signoff JAVA3D-INTEREST". For general help, send email to
>[EMAIL PROTECTED] and include in the body of the message "help".
>
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".