Forum Stats

  • 3,875,875 Users
  • 2,266,977 Discussions
  • 7,912,363 Comments

Discussions

One canvas3D within JPanel within JTabbedPane

843799
843799 Member Posts: 49,999
edited Dec 21, 2009 6:39PM in Java 3D
Hi, I have one canvas within one Jpanel within one JTabbedPane.
sometimes (almost all the time) when I cycle through the tabs, the canvas jumps to the upper left corner of my frame (outside of the aforementioned panel).
canvas still works as intended however, but again, it misplaces itself. If I continue to cycle through the panes, it might jump back in place.

similar problem at this forum: [similar forum item|http://forums.sun.com/thread.jspa?threadID=729810] with a solution that I do not understand (i will try it right now anyway), probably this has to do with swing threading that sun has forewarned about in this one and only note [sun swing/3d faq|http://java3d.j3d.org/faq/swing.html#events].

I dont think I have graphics hardware acceleration, could this be a problem?

I am looking for someone to explain the above /quote solution /quote [similar forum item|http://forums.sun.com/thread.jspa?threadID=729810]

Thank you all in advance,
Vas

Comments

  • User_64CKJ
    User_64CKJ Member Posts: 7,279 Silver Badge
    Bacek wrote:
    Hi, I have one canvas within one Jpanel within one JTabbedPane.
    sometimes (almost all the time) when I cycle through the tabs, the canvas jumps to the upper left corner of my frame (outside of the aforementioned panel).
    Please consistently use a single upper case letter at the start of each sentence. It helps the reader to quickly scan the text, looking for ways to help. You would not want to make it harder for someone to help, would you?
    canvas still works as intended however, but again, it misplaces itself. If I continue to cycle through the panes, it might jump back in place.

    similar problem at this forum: [similar forum item|http://forums.sun.com/thread.jspa?threadID=729810] ..
    My assessment of that thread is that the OP did not have the first clue what they were doing. If their 'solution' worked, it was only by accident.
    ..with a solution that I do not understand (i will try it right now anyway), probably this has to do with swing threading that sun has forewarned about in this one and only note [sun swing/3d faq|http://java3d.j3d.org/faq/swing.html#events].
    But that is some good advice. True of almost any work with to do with GUIs.

    If you cannot solve this by wrapping your calls in invokeLater(), I suggest you prepare and post an SSCCE of failing code.

    And be warned that if you fail to read that document and go posting code, I will likely 'smack you upside the head'. I have about had my fill of posters who do not read the links I provide them.
  • 843799
    843799 Member Posts: 49,999
    Thank you for your reply, I put this problem on a back burner since and havent checked this thread as soon as I should have. I have placed the canvas into its own frame but this did not fix the problem.

    1. I do not understand how I would fix this with invokeLater, at least not yet.
    2. I have an SSCCE file below: to reproduce this effect, click on canvas tab then click refresh button
    import javax.swing.*;
    import java.awt.*;
    import javax.vecmath.Color3f;
    import javax.media.j3d.*;
    import javax.media.j3d.Canvas3D;
    import com.sun.j3d.utils.universe.*;
    import com.sun.j3d.utils.geometry.ColorCube;
    import com.sun.j3d.utils.geometry.Sphere;
    import javax.media.j3d.ColoringAttributes;
    import com.sun.j3d.utils.geometry.GeometryInfo;
    import java.awt.event.*;
    
    public class canvasproblem extends JFrame{	 
    	 					
    	private		JTabbedPane tabbedPane;	
    		
    	public canvasproblem(){
    		super("Canvas3D problem: TAB -> REFRESH.");		
    		/* Application Layout */			
    		setPreferredSize(new Dimension(400, 100));
    		tabbedPane = new JTabbedPane();		
    		tabbedPane.addTab( "Empty", new JPanel());
    		tabbedPane.addTab( "Canvas Problem", new canvasPanelWrapper());
    		getContentPane().add( tabbedPane );		
    		pack();					
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);				
    	}
    	
    	private class canvasPanelWrapper extends JPanel implements ActionListener{
    		
    		canvaspanel cp;  /** simply initialized here, it will be set vis/invis by jtabbedpane **/
    		JButton b;
    		
    		public canvasPanelWrapper(){
    			
    			cp = new canvaspanel();			
    			/** button to refresh canvas3D **/			
    			b = new JButton("Refresh");add(b);
    			b.addActionListener(this);
    		}		
    		
    		public void setVisible(boolean v){
    			super.setVisible(v); /** this is to hide canvas3D when paging in/out tabbed pane **/
    			if(cp != null) cp.setVisible(v);
    		}		
    		
    		public void actionPerformed(ActionEvent event){			
    			if(event.getSource() == b)cp.refresh();
    		}		
    	}
    	
    	public static void main(String[] args){
    		canvasproblem mainFrame = new canvasproblem();		
    	}
    	
    	
    	/**canvas panel class, implements canvas enclosed in its own frame**/
    	private class canvaspanel extends JFrame {		
    		
    		BranchGroup objRoot;
    		SimpleUniverse universe;
    		Canvas3D canvas;
    				
    		public canvaspanel(){
    								
    			setPreferredSize(new Dimension(200, 200));			
    			canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration(/*new GraphicsConfigTemplate3D()*/));
    			universe = new SimpleUniverse(canvas);
    			objRoot = getSplashGraphic();
    			objRoot.setCapability(BranchGroup.ALLOW_DETACH);
    			objRoot.compile();
    			universe.addBranchGraph(objRoot);
    			universe.getViewingPlatform().setNominalViewingTransform();
    			setLayout(new BorderLayout());
    			getContentPane().add(canvas, BorderLayout.CENTER);
    			pack();				
    			setVisible(false);
    		}
    		
    		public void refresh(){/* this is called from swing components <- this is the root of the problem */			
    			objRoot.detach();
    			universe.addBranchGraph(objRoot);
    		}
    
    		
    		BranchGroup getSplashGraphic(){/** DRAW SPHERE **/
    			
    			Appearance defaultAppear = new Appearance();						
    			PolygonAttributes pa = new PolygonAttributes();
    			pa.setPolygonMode(PolygonAttributes.POLYGON_LINE); /*POLYGON_POINT*/
    			ColoringAttributes ca = new ColoringAttributes(new Color3f(1.0f, 1.0f, 1.0f), ColoringAttributes.SHADE_FLAT);			
    			defaultAppear.setPolygonAttributes(pa);
    			defaultAppear.setColoringAttributes(ca);			
    			BranchGroup splashGraphic = new BranchGroup();
    			splashGraphic.addChild(new Sphere(0.5f, defaultAppear));			
    			return splashGraphic;
    		}		
    	}
    	
    }
This discussion has been closed.