Printing of java3d applet
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Amit_Jindal
Posted On:   Friday, June 8, 2001 04:13 AM

I want to give the command throgh the button on an applet which includes all the code of java3d. I had gone through java.awt.print package..It gives that we have to pass the Graphics reference to the print() method of Printable interface. But in case of java3d I don't have the Graphics reference.. I can give my code as well as example code which is given in corejava2 by sun microsystems.. My code: package visualizer.client; import java.awt.event.*; import java.awt.event.WindowAdapter; import visualizer.util.SharedObject; import com.sun.j3d.utils.image.TextureLoader; import java.io.*; import java.rmi.*; import java.net.*; import java.applet.Applet; import com.   More>>

I want to give the command throgh the button on an applet which includes all the code of java3d. I had gone through java.awt.print package..It gives that we have to pass the Graphics reference to the print() method of Printable interface. But in case of java3d I don't have the Graphics reference.. I can give my code as well as example code which is given in corejava2 by sun microsystems..

My code:

package visualizer.client;

import java.awt.event.*;
import java.awt.event.WindowAdapter;

import visualizer.util.SharedObject;
import com.sun.j3d.utils.image.TextureLoader;
import java.io.*;
import java.rmi.*;
import java.net.*;
import java.applet.Applet;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.Scene;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.*;

import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import java.awt.*;
import java.awt.print.*;




//the applet class
public class Viewer extends Applet
implements Printable, ActionListener
{

private Canvas3D canvas;
private SimpleUniverse univ;
private BranchGroup baseGroup;
private VisualizationViewer visualizationViewer;

private String sessionId;
private Color3f bgColor;

private Button frontB = null;
private Button topB = null;
private Button leftB = null;
private Button rightB = null;
private Button printB = null;

private TransformGroup objTransform = null;

private Transform3D transform = null;

private Matrix3f rotx = null;
private Matrix3f rotz = null;
private Matrix3f rotation = null;

public void init()
{
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(config);
Graphics2D gr = (Graphics)config;
add("Center", canvas);

univ = new SimpleUniverse(canvas);
univ.getViewingPlatform().setNominalViewingTransform();

frontB = new Button("00000 View");
topB = new Button("Top View");
leftB = new Button("Left View");
rightB = new Button("Right Views");
printB = new Button("Print");

Panel p = new Panel();

frontB.addActionListener(this);
leftB.addActionListener(this);
rightB.addActionListener(this);
topB.addActionListener(this);
printB.addActionListener(this);

rotx = new Matrix3f();
rotz = new Matrix3f();
rotation = new Matrix3f();

sessionId = getParameter("Session");
String sBgColor = getParameter("BgColor");

float r = Float.parseFloat(sBgColor.substring(0, 3)) / 255.0f;
float g = Float.parseFloat(sBgColor.substring(3, 6)) / 255.0f;
float b = Float.parseFloat(sBgColor.substring(6, 9)) / 255.0f;
bgColor = new Color3f(r, g, b);

Color panelColor = new Color(r, g, b);
p.setBackground(panelColor);
p.add(frontB);
p.add(leftB);
p.add(rightB);
p.add(topB);
p.add(printB);
add("North", p);
}


public void start()
{

BranchGroup rootBranch = new BranchGroup();

BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 1.0);

BoundingSphere lightBounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 10.0);

Background bgNode = new Background(bgColor);
bgNode.setApplicationBounds(bounds);
rootBranch.addChild(bgNode);

transform = new Transform3D();

objTransform = new TransformGroup();

objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTransform.setTransform(transform);
rootBranch.addChild(objTransform);

MouseRotate myMouseRotate = new MouseRotate();
myMouseRotate.setTransformGroup(objTransform);
myMouseRotate.setSchedulingBounds(bounds);
rootBranch.addChild(myMouseRotate);

MouseTranslate myMouseTranslate = new MouseTranslate();
myMouseTranslate.setTransformGroup(objTransform);
myMouseTranslate.setSchedulingBounds(bounds);
rootBranch.addChild(myMouseTranslate);

MouseZoom myMouseZoom = new MouseZoom();
myMouseZoom.setTransformGroup(objTransform);
myMouseZoom.setSchedulingBounds(bounds);
rootBranch.addChild(myMouseZoom);

Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
rootBranch.addChild(ambientLightNode);

Color3f light1Color = new Color3f(0.8f, 0.8f, 0.7f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);

Color3f light2Color = new Color3f(0.4f, 0.4f, 0.5f);
Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);

Color3f light3Color = new Color3f(0.3f, 0.3f, 0.4f);
Vector3f light3Direction = new Vector3f(0.0f, 0.0f, -1.0f);

DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(lightBounds);
rootBranch.addChild(light1);

DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction);
light2.setInfluencingBounds(lightBounds);
rootBranch.addChild(light2);

DirectionalLight light3 = new DirectionalLight(light3Color, light3Direction);
light3.setInfluencingBounds(lightBounds);
rootBranch.addChild(light3);

baseGroup = new BranchGroup();
baseGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);
baseGroup.setCapability(Group.ALLOW_CHILDREN_WRITE);

objTransform.addChild(baseGroup);


visualizationViewer = new VisualizationViewer( baseGroup,
sessionId,
getCodeBase().getHost(),
this,
getCodeBase().toString(),
bgColor);
visualizationViewer.start();

// makes 'baseGroup' node alive
univ.addBranchGraph(rootBranch);

showStatus("Applet Started Successfully ...");
}

public void stop()
{
visualizationViewer = null;
}

public int print(Graphics g, PageFormat pf, int pi) throws PrinterException
{
if (pi >= 1)
{
return Printable.NO_SUCH_PAGE;
}
//drawShapes((Graphics2D) g);
return Printable.PAGE_EXISTS;
}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == topB)
{
rotz.rotZ(0.0f);
transform.setRotation(rotz);
}
else if (e.getSource() == frontB)
{
rotz.rotZ(3.14f);
rotx.rotX(-1.57f);//anti clockwise is negative

rotation.mul(rotx, rotz);
transform.setRotation(rotation);
}
else if (e.getSource() == leftB)
{
rotz.rotZ(1.57f);
rotx.rotX(-1.57f);//anti clockwise is negative

rotation.mul(rotx, rotz);
transform.setRotation(rotation);
}

else if (e.getSource() == rightB)
{
rotz.rotZ(-1.57f);
rotx.rotX(-1.57f);//anti clockwise is negative

rotation.mul(rotx, rotz);
transform.setRotation(rotation);
}
else if(e.getSource() == printB)
{
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
{
try
{
printJob.print();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
objTransform.setTransform(transform);

}

}


u can not compile my code becoz it needs the full package and also some tools of our company..

example code:

/**
* @version 1.00 1999-09-11
* @author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
import javax.swing.*;

public class PrintTest
{ public static void main(String[] args)
{ JFrame frame = new PrintTestFrame();
frame.show();
}
}

class PrintTestFrame extends JFrame
implements ActionListener
{ public PrintTestFrame()
{ setTitle("PrintTest");
setSize(300, 300);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );

Container contentPane = getContentPane();
canvas = new PrintPanel();
contentPane.add(canvas, "Center");

JPanel buttonPanel = new JPanel();
printButton = new JButton("Print");
buttonPanel.add(printButton);
printButton.addActionListener(this);

pageSetupButton = new JButton("Page setup");
buttonPanel.add(pageSetupButton);
pageSetupButton.addActionListener(this);

contentPane.add(buttonPanel, "North");
}

public void actionPerformed(ActionEvent event)
{ Object source = event.getSource();
if (source == printButton)
{ PrinterJob printJob = PrinterJob.getPrinterJob();
if (pageFormat == null)
pageFormat = printJob.defaultPage();
printJob.setPrintable(canvas, pageFormat);
if (printJob.printDialog())
{ try
{ printJob.print();
}
catch (PrinterException exception)
{ JOptionPane.showMessageDialog(this, exception);
}
}
}
else if (source == pageSetupButton)
{ PrinterJob printJob = PrinterJob.getPrinterJob();
if (pageFormat == null)
pageFormat = printJob.defaultPage();
pageFormat = printJob.pageDialog(pageFormat);
}
}

private JButton printButton;
private JButton pageSetupButton;

private PrintPanel canvas;
private PageFormat pageFormat;
}

class PrintPanel extends JPanel
implements Printable
{ public void paintComponent(Graphics g)
{ super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
drawPage(g2);
}

public int print(Graphics g, PageFormat pf, int page)
throws PrinterException
{ if (page >= 1) return Printable.NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.black);
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.draw(new Rectangle2D.Double(0, 0,
pf.getImageableWidth(), pf.getImageableHeight()));

drawPage(g2);
return Printable.PAGE_EXISTS;
}

public void drawPage(Graphics2D g2)
{ FontRenderContext context = g2.getFontRenderContext();
Font f = new Font("Serif", Font.PLAIN, 72);
GeneralPath clipShape = new GeneralPath();

TextLayout layout = new TextLayout("Hello", f, context);
AffineTransform transform
= AffineTransform.getTranslateInstance(0, 72);
Shape outline = layout.getOutline(transform);
clipShape.append(outline, false);

layout = new TextLayout("World", f, context);
transform
= AffineTransform.getTranslateInstance(0, 144);
outline = layout.getOutline(transform);
clipShape.append(outline, false);

g2.draw(clipShape);
g2.clip(clipShape);

final int NLINES =50;
Point2D p = new Point2D.Double(0, 0);
for (int i = 0; i < NLINES; i++)
{ double x = (2 * getWidth() * i) / NLINES;
double y = (2 * getHeight() * (NLINES - 1 - i))
/ NLINES;
Point2D q = new Point2D.Double(x, y);
g2.draw(new Line2D.Double(p, q));
}
}
}

   <<Less
About | Sitemap | Contact