Skip to main content

Canvas initially blank, on mimimise and restore image is shown

6 replies [Last post]
richteabiscuit
Offline
Joined: 2012-03-29
Points: 0

Whenever I've created java3D graphics they always seem to have this problem;

when the application first starts the image may be visible for an instant, then goes grey. When the application is minimised and then restored the image may or may not appear. If it does appear it will stay perminantly. Can anyone shead any light on this? If this a problem with my code? Or could it be a hardware issue?

This is the simplest application I could make that showed the problem. It positions a colorCube in the scene

import javax.swing.*;
import java.awt.*;

import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Simplest extends JFrame
// Holds the 3D canvas where the loaded image is displayed
{
  private static final int PWIDTH = 512;   // size of panel
  private static final int PHEIGHT = 512;

  private static final int BOUNDSIZE = 100;  // larger than world

  private SimpleUniverse su;
  private BranchGroup sceneBG;
  private BoundingSphere bounds;   // for environment nodes

  public Simplest()
  {
   
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(PWIDTH,PHEIGHT);
    this.setVisible(true);
     
    setLayout( new BorderLayout() );
    setPreferredSize( new Dimension(PWIDTH, PHEIGHT));
    GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas3D = new Canvas3D(config);
    add("Center", canvas3D);
    //canvas3D.setFocusable(true);     // give focus to the canvas
    //canvas3D.requestFocus();

    su = new SimpleUniverse(canvas3D);
    createSceneGraph();
    su.addBranchGraph( sceneBG );
  }


  private void createSceneGraph()
  {
    sceneBG = new BranchGroup();
    bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE);  
    colcube();
    sceneBG.compile();   // fix the scene
  }

  private void colcube()
  {
       ColorCube colCube=new ColorCube();
     
       Transform3D tfPosition=new Transform3D();
       tfPosition.setTranslation(new Vector3d(1,-2,-10));
       TransformGroup positionTG = new TransformGroup();
       positionTG.setTransform(tfPosition);
       positionTG.addChild(colCube);    
       sceneBG.addChild(positionTG);
       BranchGroup bg=new BranchGroup();
       sceneBG.addChild(bg);
   
  } 

  public static void main(String args[]){
      Simplest simp =new Simplest();
  }
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
interactivemesh
Offline
Joined: 2006-06-07
Points: 0

Within your Simplest's constructor: first Java 3D, second Swing; this.setVisible(true) should be the last call. This might solve the issue when your application starts. See also the sample 'MyFirstUniverse' http://www.interactivemesh.org/sourcecode/firstuniverse.html

August

mikelizzi2
Offline
Joined: 2011-10-13
Points: 0

I see the same problem with your 'MyFirstUniverse' program.

I downloaded the class file MyFirstUniverse_I.java
I commented out the animation (the Alpha and interpolator) compiled and ran the program under java jre 1.7. After the window opened, the 3d text "My First Universe" was there for a moment and then the canvas went blank. I resized it a few times and sometimes the canvas came back and sometimes it went blank again. This never happens in jre 1.6.0_31 !

I spent hours reading documentation and doing trial an error stuff. I cannot figure a solution. I'm betting a lot of people out there will see this problem in their programs too.

Anybody out there got a solution?

interactivemesh
Offline
Joined: 2006-06-07
Points: 0

In case of mixing heavyweight (Canvas3D) and lightweight components in JRE 7 it is helpful to set the system property "sun.awt.noerasebackground"

- in your batch file : -Dsun.awt.noerasebackground=true, or
- in the 'main' method before any awt/swing class is imported : System.setProperty("sun.awt.noerasebackground", "true");

This property is considered "secure" and can be used in jnlp-files on the web, too.

Give it a try. August

mikelizzi2
Offline
Joined: 2011-10-13
Points: 0

That worked! Thank You.

So I have to add that line to all my java 3d programs. And I guess everybody else will have to do the same if they want to use jre 1.7.

mikelizzi2
Offline
Joined: 2011-10-13
Points: 0

I now have a similar problem with Java 1.7.
Everything worked fine with previous Java versions.

I have a Canvas3D in a JScrollPane in a JPanel in a JTabbedPane. When I click on another tab and come back, the canvas has disappeared. When I run my simulation the canvas returns.

I tried every variation of putting setVisible(true) on every container. Still nothing. I am requiring Java Web Start to use Java 1.6.0_31 for now. What happened in 1.7?

There is a solution. See post from "interactivemesh" above.

richteabiscuit
Offline
Joined: 2012-03-29
Points: 0

Thanks, that completely solves the problem