nullPointerException while using renderOffScreenBuffer

N

nachikethg

hi all,
i have written a java code to convert a 3D image on the canvas into a
jpeg image n store it in the file systems... but it doesnt seem to work
coz of this exception which i have come across while executing it... i
have copy pasted my code and the exception which occured.. if sm1 could
plz help me out with this.. it would b of great help... thnks.. bye
-nachi

CODE:

import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;

import java.awt.*;
import java.io.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.applet.*;
import com.sun.j3d.utils.universe.*;
import com.sun.image.codec.jpeg.*;

public class jpgSaver extends Applet
{
public jpgSaver() {
setLayout(new BorderLayout());
GraphicsConfiguration gf =
SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(gf);
add("Center",c);
JPanel jp = new JPanel(new FlowLayout());
class al implements ActionListener
{
public void actionPerformed(ActionEvent e){
JFileChooser jf = new JFileChooser();
int rval = jf.showSaveDialog(jpgSaver.this);
if(rval == JFileChooser.APPROVE_OPTION){
File f = new File(jf.getSelectedFile().getName());
saveJpgFile(f);
System.out.println("Here is the
filename:"+jf.getSelectedFile().getName());
}
}
}
al event = new al();
JButton save = new JButton("Save");
save.addActionListener(event);
jp.add(save);
add("East",jp);


//save.addActionListener();

BranchGroup bg = new BranchGroup();
Transform3D t = new Transform3D();
t.rotX(Math.PI/4);
t.rotY(Math.PI/4);
TransformGroup tg = new TransformGroup(t);
tg.addChild(new ColorCube(0.4f));

bg.addChild(tg);

SimpleUniverse su = new SimpleUniverse(c);
su.getViewingPlatform().setNominalViewingTransform();
su.addBranchGraph(bg);
}
public void saveJpgFile(File f)
{
Canvas3D offScreenCanvas = null;
ImageComponent2D imageComponent = null;
int offScreenWidth = 400;
int offScreenHeight = 400;
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
offScreenCanvas = new Canvas3D(config, true);
Screen3D scr = offScreenCanvas.getScreen3D();
scr.setSize(new Dimension(1280, 1024));
scr.setPhysicalScreenWidth(0.36124);
scr.setPhysicalScreenHeight(0.28899555);

RenderedImage renderedImage = new BufferedImage( offScreenWidth,
offScreenHeight, BufferedImage.TYPE_3BYTE_BGR );
imageComponent = new ImageComponent2D( ImageComponent.FORMAT_RGB8,
renderedImage );
imageComponent.setCapability( ImageComponent2D.ALLOW_IMAGE_READ );
offScreenCanvas.setOffScreenBuffer( imageComponent );

View v = new View();
v.addCanvas3D(offScreenCanvas);
try
{
offScreenCanvas.renderOffScreenBuffer( );
offScreenCanvas.waitForOffScreenRendering( );
}
catch (Exception e)
{
e.printStackTrace();
}

System.out.println( "Rendered to offscreen" );

try
{
FileOutputStream fileOut = new FileOutputStream( f );

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( fileOut );
encoder.encode( imageComponent.getImage( ) );

fileOut.flush( );
fileOut.close( );
}
catch( Exception e )
{
System.err.println( "Failed to save image: " + e );
}

System.out.println( "Saved image." );
}





public static void main(String[] args)
{
Frame f = new MainFrame(new jpgSaver(),500,500);
}

}

Exception at Run time:

---------- execute ----------
java.lang.NullPointerException
at javax.media.j3d.Canvas3D.renderOffScreenBuffer(Canvas3D.java:1914)
at jpgSaver.saveJpgFile(jpgSaver.java:89)
at jpgSaver$1$al.actionPerformed(jpgSaver.java:31)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1767)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1820)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:258)
at java.awt.Component.processMouseEvent(Component.java:5021)
at java.awt.Component.processEvent(Component.java:4818)
at java.awt.Container.processEvent(Container.java:1525)
at java.awt.Component.dispatchEventImpl(Component.java:3526)
at java.awt.Container.dispatchEventImpl(Container.java:1582)
at java.awt.Component.dispatchEvent(Component.java:3367)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3359)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:3074)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3004)
at java.awt.Container.dispatchEventImpl(Container.java:1568)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:191)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:144)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)
Rendered to offscreen
Saved image.
Here is the filename:test
Normal Termination
Output completed (27 sec consumed).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top