Hi,

I have found what seems to be a bug in Java 3D (or jdk 1.4.0).

When running the attached code using javaw, the following happens:

The Canvas3D gets grey artifacts when you try to open a file using
the standard file opener. If you resize the application the entire
Canvas3D goes blank/grey. If you resize the window and releases
the mouse, everything looks good. But as soon as the mouse is
moved (inside the app), the Canvas3D goes blank/grey.

The problem is easily solved by starting the application using the
java command instead of the javaw command!

This program was tested on several machines running Windows 2000
and using jdk 1.4.0 and java 3D 1.3 beta1 (opengl).

Best Regards

Roger Berggren
package com.decim;

import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.*;

public class AppScene
{
  Canvas3D canvas;

  public AppScene()
  {
  }

  public Canvas3D createCanvas()
  {
    GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = env.getDefaultScreenDevice();
    GraphicsConfiguration config = device.getBestConfiguration(template);

    this.canvas = new Canvas3D(config);

    return this.canvas;
  }

  public SceneManager createScene()
  {
    SceneManager scene = SceneManager.getInstance();

    BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
    scene.addCamera(createCamera(bounds));

    scene.makeLive();

    BranchGroup startup = new BranchGroup();
    String message = "Application startup";

    float length = message.length();

    Transform3D transform = new Transform3D();
    TransformGroup scale = new TransformGroup();

    transform.setScale(1.0);
    transform.rotY(-0.75);
    scale.setTransform(transform);
    startup.addChild(scale);

    Font3D f3d = new Font3D(new Font("Courier", Font.PLAIN, 3), new FontExtrusion());
    Text3D txt = new Text3D(f3d, message, new Point3f(-length / 1.5f, -1f, -1f));
    Shape3D sh = new Shape3D();
    Appearance app = new Appearance();
    Material mm = new Material();
    mm.setLightingEnable(true);
    app.setMaterial(mm);
    sh.setGeometry(txt);
    sh.setAppearance(app);
    scale.addChild(sh);

    // Color3f ambientColor = new Color3f(0.8f, 0.8f, 0.0f);
    // AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    // ambientLightNode.setInfluencingBounds(bounds);
    // startup.addChild(ambientLightNode);
    Color3f ambientColor = new Color3f(0.8f, 0.8f, 0.0f);
    DirectionalLight ambientLightNode = new DirectionalLight();
    ambientLightNode.setColor(new Color3f(1, 1, 1));
    ambientLightNode.setDirection(-0.5f,1,-1);
    ambientLightNode.setInfluencingBounds(bounds);
    startup.addChild(ambientLightNode);

    startup.setCapability(BranchGroup.ALLOW_DETACH);
    startup.setCapability(Node.ALLOW_BOUNDS_READ);

    scene.changeWorldGroup(startup);

    return scene;
  }

  public TransformGroup createCamera(BoundingSphere bounds)
  {
    PhysicalEnvironment env;
    AmbientLight headlight = new AmbientLight();
    // DirectionalLight headlight;
    PhysicalBody body;
    View view;

    ViewPlatform platform = new ViewPlatform();
    Vector3f loc = new Vector3f(0, 0, 10.0f);

    Group head = new Group();
    head.setCapability(Group.ALLOW_CHILDREN_EXTEND);

    Transform3D location = new Transform3D();
    location.setTranslation(loc);

    TransformGroup transform = new TransformGroup();
    transform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    transform.setTransform(location);
    transform.addChild(platform);
    transform.addChild(head);

    // now create the headlight
    // headlight = new DirectionalLight();
    // headlight.setCapability(Light.ALLOW_STATE_WRITE);
    // headlight.setColor(new Color3f(1, 1, 1));
    headlight.setInfluencingBounds(bounds);
    headlight.setEnable(true);
    transform.addChild(headlight);

    body = new PhysicalBody();
    env = new PhysicalEnvironment();

    view = new View();
    view.setBackClipDistance(100);
    view.setPhysicalBody(body);
    view.setPhysicalEnvironment(env);
    view.attachViewPlatform(platform);

    view.addCanvas3D(canvas);

    return transform;
  }
}
package com.decim;

import javax.media.j3d.Canvas3D;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.File;

public class Designer extends JFrame implements ActionListener
{
  public Designer()
  {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Designer");

    JButton openButton = new JButton("Open ...");
    openButton.addActionListener(this);

    getContentPane().add(openButton, BorderLayout.SOUTH);

    this.setSize(640, 400);
  }

  public void addCanvas(Canvas3D canvas)
  {
    getContentPane().add(canvas, BorderLayout.CENTER);
  }

  public void actionPerformed(java.awt.event.ActionEvent evt)
  {
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int returnVal = fc.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
      File file = fc.getSelectedFile();
      String path = file.getAbsolutePath();
      this.setTitle("Designer [" + file.getName() + "]");
    }
  }

  public void displayFrame(boolean center)
  {
    this.validate();

    if (center == true)
    {
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension frameSize = this.getSize();

      if (frameSize.height > screenSize.height)
      {
        frameSize.height = screenSize.height;
      }

      if (frameSize.width > screenSize.width)
      {
        frameSize.width = screenSize.width;
      }

      this.setLocation((screenSize.width - frameSize.width) / 2,
                       (screenSize.height - frameSize.height) / 2);
    }

    this.setVisible(true);
  }

  public static void main(String args[])
  {
    AppScene scene = new AppScene();

    Designer frame = new Designer();
    frame.addCanvas(scene.createCanvas());

    SceneManager mgr = scene.createScene();

    frame.displayFrame(true);
  }
}
package com.decim;

import javax.media.j3d.*;

public class SceneManager extends VirtualUniverse
{
  private Locale locale;
  private BranchGroup viewGroup;
  private BranchGroup worldGroup;

  private static SceneManager instance;

  private SceneManager()
  {
    this.locale = new Locale(this);

    this.viewGroup = new BranchGroup();
    this.viewGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);

    this.worldGroup = new BranchGroup();
    this.worldGroup.setCapability(BranchGroup.ALLOW_DETACH);
  }

  public static synchronized SceneManager getInstance()
  {
    if (instance == null)
    {
      instance = new SceneManager();
    }

    return instance;
  }

  public void addCamera(TransformGroup cam)
  {
    this.viewGroup.addChild(cam);
  }

  public void makeLive()
  {
    this.viewGroup.compile();
    this.worldGroup.compile();

    this.locale.addBranchGraph(this.viewGroup);
    this.locale.addBranchGraph(this.worldGroup);
  }

  public synchronized void changeWorldGroup(BranchGroup newGroup)
  {
    newGroup.compile();

    this.locale.replaceBranchGraph(this.worldGroup, newGroup);
    this.worldGroup = newGroup;
  }
}

Reply via email to