I guess this is really another Swing vs J3D sort of thing, but..
I have a nice JFrame used as a control panel with a bunch of stuff
(Jthis, Jthat, Jetc...) and a Canvas3D that I draw into. When I
close the JFrame using the clicking on Windows close buttons and
later set the JFrame to visible again later, the Canvas3D isn't
redrawn. If I resize the JFrame it gets repainted just fine.
So, as a hack, I do a:
Dimension dim = this.getSize ()
canvas.setSize (dim.width+1, dim.height+1);
this.setVisible (true);
canvas.setSize (dim.width, dim.height);
and it works for my situation. It's a hack, it works, but
it's just not right. Is there a better way? I've tried
canvas.repaint (), canvas.validate (), canvas.invalidate (),
canvas.doTheRightThing (), etc... with no luck.
Anyone know the answers?
-- John
VisTest.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.ColorCube;
public class VisTest extends JFrame implements ActionListener {
VisTest slave;
Canvas3D canvas;
JButton button;
public static void main (String[] argv) {
VisTest st = new VisTest ();
}
public VisTest () {
button = new JButton (OPEN);
button.setActionCommand (OPEN_CMD);
button.addActionListener (this);
// set up components
JPanel pane = new JPanel ();
getContentPane ().add (pane);
pane.setLayout (new BorderLayout ());
pane.add (button, BorderLayout.CENTER);
// display window
setTitle ("Master");
setSize (100, 100);
setVisible (true);
slave = new VisTest (1);
addWindowListener ((WindowListener) new WindowAdapter () {
public void windowClosing (WindowEvent e) {
System.exit (0);
}
});
}
public VisTest (int foo) {
canvas = new Canvas3D (null);
canvas.setSize (256, 256);
BranchGroup theBG = new BranchGroup ();
theBG.addChild (new ColorCube (0.4));
theBG.compile ();
SimpleUniverse u = new SimpleUniverse (canvas);
u.getViewingPlatform ().setNominalViewingTransform ();
u.addBranchGraph (theBG);
button = new JButton (CLOSE);
button.setActionCommand (CLOSE_CMD);
button.addActionListener (this);
addWindowListener ((WindowListener) new WindowAdapter () {
public void windowClosing (WindowEvent e) {
setVisible (false);
}
});
JPanel pane = new JPanel ();
getContentPane ().add (pane);
pane.setLayout (new BorderLayout ());
pane.add (canvas, BorderLayout.CENTER);
pane.add (button, BorderLayout.SOUTH);
// display window
setTitle ("Canvas");
setSize (400, 370);
setVisible (true);
}
private void makeMeVisible () {
if (canvas != null) {
Dimension dim = getSize ();
canvas.setSize (dim.width+1, dim.height+1);
setVisible (true);
canvas.setSize (dim.width, dim.height);
}
}
public void actionPerformed (ActionEvent evt) {
String cmd = evt.getActionCommand ();
if (cmd.equals (CLOSE_CMD)) {
this.setVisible (false);
} else if (cmd.equals (OPEN_CMD)) {
if (slave != null) {
slave.makeMeVisible ();
}
}
}
public static final String CLOSE = "Close";
public static final String CLOSE_CMD = CLOSE;
public static final String OPEN = "Open";
public static final String OPEN_CMD = OPEN;
}
=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 3D Home Page: http://java.sun.com/products/java-media/3D/