Swing GUI vanished with Xith 0.71?

Hey Xith Team,

first of all I really appreciate your work on Xith3D an I am happy that there was so much effort during the last time.

I develop a little Diablo-Style Maze Game with Xith and have some problems with the switch to version 0.7 or 0.71.
Screens: Screen1 Screen2 (before the switch to 0.71)

The application works but all my Swing GUI windows are vanished.
Of course I properly changed the import to org.xith3d.ui.swingui.UIWindowManager and it compiled without any error.

To solve it I copied the old com.xith3d.userinterface.UIWindowManager structure into the xith3d.jar. Then I got a compile error “setUIWindowmanager: no such function found in com.xith3d.scenegraph.Canvas3D” error. I copied the old canvas3D.class to the xith3d.jar. Then everything compiled fine, but still no GUI.
Also the Xith3DGuiTest.bat Demo doesn’t work anymore.

As you can see I put quite some effort in creating my GUI and I would really appreciate if there was a way to implement the Swing GUI again. I know it is not maintained anymore, but it is the only GUI at the moment and it worked well for me.
Because I switched from C to Java and this is my first project, I am not confident to change the sourcecode on my own.

`UIWindowManager windowMgr;

windowMgr = new UIWindowManager(canvas);
SwingFPSCounter counter = new SwingFPSCounter(windowMgr, canvas, WindowWidth-70, 0, false);`

The SwingFPSCounter class is made out of the Swing GUI demo class. So if someone could make the demo work again it would really help.

Thanks!

Hi Kaiworld and welcome to Xith forums.

First of all, congratulations your game is looking very nice and if it’s free I look forward to try it.

I did both 0.7.0 and 0.7.1 releases, and i changed nothing between them concerning the Swing GUI. Maybe another did a change on CVS and I included it by mistake.

Now I use myself the UI and it works perfectly. And Xith3DGuiTest also works for me (see 2 attached images, on of Xith3DGuiTest, and one of my current game).

`
UIWindowManager windowMgr;

		// ...

		// Initialization of the UI
		windowMgr = new UIWindowManager(graphic.getCanvas());
		TestWindow w = new TestWindow(200, 400, true);
		windowMgr.addOverlay(w);
		windowMgr.setPosition(w, 10, 10);
		windowMgr.setVisible(w, true);

		UIEventAdapter eventAdapter = new UIEventAdapter(windowMgr);
		graphic.getCanvas().get3DPeer().getComponent().addKeyListener(eventAdapter);
		graphic.getCanvas().get3DPeer().getComponent().addMouseListener(eventAdapter);
		graphic.getCanvas().get3DPeer().getComponent().addMouseMotionListener(
				eventAdapter);
		graphic.getCanvas().get3DPeer().getComponent().setFocusable(true);
		graphic.getLocale().addBranchGraph(windowMgr);

		// Main loop
		while (true) {

			// Render
			windowMgr.newFrame(graphic.getView().getTransform());
			view.renderOnce();

		}

`

Don’t forget the :
windowMgr.newFrame(graphic.getView().getTransform());
Or it won’t work.

Thanks for your help. The GUI Demo now works for me too, I noticed that I had some old libraries left in my sdk (normally I use the jre).

Yes, my game will be free. And if there is a playable release to test it, you guys in the forum will be the first to know.

Now I just have to figure out, how to adapt the code in the XithGuiDemo to my own project. My GUI code is based on the Swing FPS Counter by Jens Lehmann which is a little bit different to the code in the xith demo.
The following Demo worked before Xith 0.7 but not anymore:
(a rotating cube with the FPS displayed in the corner)

  • Sorry for the long code -

`import javax.vecmath.*;

// Xith3D
import com.xith3d.scenegraph.;
import com.xith3d.test.
;

// use Jogl
import com.xith3d.render.;
import com.xith3d.render.jsr231.
;

import java.awt.Color;

/**

  • This app displays a cube and a Swing component.

  • @author Jens Lehmann
    */
    public class SwingDemo
    {

    /**

    • Starts the application.
    • @param args command line parameters
      */
      public static void main(String[] args)
      {
      new SwingDemo();
      }

    /**

    • Draws a cube.
      */
      public SwingDemo()
      {
      // create the virtual universe
      VirtualUniverse universe = new VirtualUniverse();

      // add a view to the universe
      View view = new View();
      universe.addView(view);

      // add a locale
      Locale locale = new Locale();
      universe.addLocale(locale);

      // create a BranchGroup
      BranchGroup scene = new BranchGroup();
      locale.addBranchGraph(scene);

      // let objects along this path rotate
      Transform3D rotate = new Transform3D();
      TransformGroup objRotate = new TransformGroup(rotate);
      scene.addChild(objRotate);

      // create Cube
      Geometry geo = Cube.createCubeViaTriangles(0, 0, 0, 1, true);
      Shape3D sh = new Shape3D(geo, new Appearance());
      objRotate.addChild(sh);

      // create a canvas for our graphics
      RenderPeer rp = new RenderPeerImpl();
      CanvasPeer cp = rp.makeCanvas(null, 800, 600, 32, false);
      Canvas3D canvas = new Canvas3D();
      canvas.set3DPeer(cp);

      // modify our view so we can see the cube
      view.addCanvas3D(canvas);
      view.getTransform().lookAt(new Vector3f( 0, 0, 2), // location of eye
      new Vector3f( 0, 0, 0), // center of view
      new Vector3f( 0, 1, 0)); // vector pointing up

      // create a framerate counter
      SwingFPSCounter counter = new SwingFPSCounter(canvas,200,200,false);

      float rotY = 0.0f;

      // main render loop
      while(true)
      {
      // rotate around a bit
      rotY += 0.003;
      rotate.rotXYZ((float)Math.PI/4, rotY, (float)Math.PI/4);
      objRotate.setTransform(rotate);

       // render the whole scene
       view.renderOnce();
      

      }

    }

}
`

`// CLASS SwingFPSCounter

// use the hidden high-resolution timer
import sun.misc.Perf;

// Scenegraph
import com.xith3d.scenegraph.Canvas3D;

// using AWT and Swing
import java.awt.;
import java.awt.event.
;
import javax.swing.;
import org.xith3d.ui.swingui.
;

/**

  • A counter which creates a Swing component and renders it directly

  • on an Xith3D canvas.

  • @author Jens Lehmann
    */
    public class SwingFPSCounter
    {
    // the hight-resolution timer object we are using to work
    private Perf perf = sun.misc.Perf.getPerf();

    // the number of ticks per second
    private long ticksPerSecond = perf.highResFrequency();

    // temporary variables used to calculate the framerate
    private long lastSecondTicks = 0;
    private long newFrameTicks = 0;
    private int frameCounter = 0;

    // specifies if the counter should update automatically or not
    private boolean autoUpdate = true;

    // Swing components
    private JTextField textField;
    private JPanel panel;
    private TestWindow window;

    /**

    • This is the window for the frame counter and the exit button.
      */
      class TestWindow extends UIWindow
      {

      /**

      • Creates the window.
      • @param width The width of the window.
      • @param height The height of the window.
      • @param includeExitButton Specifies if an exit button
      •                       should be displayed.
        

      */
      public TestWindow(int width, int height, boolean includeExitButton)
      {
      super(width, height, false, false);

       // create a double buffered JPanel
       panel = new JPanel(true);
       panel.setSize(new Dimension(width,height));
       panel.setBackground(Color.darkGray);
      
       // create a text field with appropriate properties
       textField = new JTextField("0",4);
       textField.setBackground(new Color(0.0f, 0.0f, 0.0f));
       textField.setForeground(new Color(1.0f, 1.0f, 1.0f));
       textField.setHorizontalAlignment(JTextField.CENTER);
       panel.add(textField);
      
       // include the exit button if it's wanted
       if(includeExitButton)
       {
           JButton exitButton = new JButton();
           exitButton.setText("Exit");
           exitButton.addActionListener(new ActionListener()
           {
               public void actionPerformed(ActionEvent e)
               {
                   System.exit(0);
               }
           });
           panel.add(exitButton);
       }
      
       // specify the root component of the UI window
       setRoot(panel);
      

      }

      /**

      • This method is called once a frame. We use it to update the frame
      • counter. This can be disabled by calling setAutoUpdate(false).
        */
        public void update()
        {
        super.update();
        if(autoUpdate)
        nextFrame();
        }

    }

    /**

    • Simple constructor, which places the counter in the upper-left
    • corner and displays an exit button. Added for convenience.
    • @param canvas The Canvas3D the userinterface is responsible for.
      */
      public SwingFPSCounter(Canvas3D canvas)
      {
      this(canvas, 10, 10, true);
      }

    /**

    • Creates a framerate counter at a certain positon with or without
    • an exit button.
    • @param canvas The Canvas3D the userinterface is responsible for.
    • @param xPos The x-position of the framerate counter.
    • @param yPos The y-position of the framerate counter.
    • @param includeExitButton Specifies wether to display
    •                       an exit button or not.
      

    */
    public SwingFPSCounter(Canvas3D canvas,
    int xPos,
    int yPos,
    boolean includeExitButton)
    {
    // configure window manager
    UIWindowManager windowMgr = new UIWindowManager(canvas);
    // make the panel a bit smaller, if there’s no exit button
    if(includeExitButton)
    window = new TestWindow(80, 60, true);
    else
    window = new TestWindow(80, 30, false);
    windowMgr.addOverlay(window);
    windowMgr.setPosition(window, xPos, yPos);
    windowMgr.setVisible(window, true);

     if(includeExitButton)
     {
         // add a mouse listener for the exit button
         UIEventAdapter eventAdapter = new UIEventAdapter(windowMgr);
         canvas.get3DPeer().getComponent().addMouseListener(eventAdapter);
         canvas.get3DPeer().getComponent().setFocusable(true);
     }
    

    }

    /**

    • Method must be called by everytime a frame was rendered.
      */
      public void nextFrame()
      {
      if(newFrameTicks==0)
      {
      // start time measurement
      lastSecondTicks = perf.highResCounter();
      newFrameTicks = lastSecondTicks;
      frameCounter = 0;
      }
      else
      {
      // update number of frames and ticks
      frameCounter++;
      newFrameTicks = perf.highResCounter();

       // update the counter every second
       if(newFrameTicks - lastSecondTicks > ticksPerSecond)
       {
           // display the new framerate
           textField.setText(frameCounter+"");
           // set the root component, we do this here to mark the component
           // as "dirty", which means it has to be repainted
           window.setRoot(panel);
      
           // set the frame counter back
           frameCounter = 0;
      
           // add a "second"
           lastSecondTicks += ticksPerSecond;
       }
      

      }
      }

    /**

    • Tells the framerate counter wether it should update automatically or not.
    • Default is to it automatically.
    • If you don’t want to update the counter to be updated automatically you
    • have to call nextFrame() after each rendered frame.
    • @param autoUpdate Specifies wether the counter is updated automatically
    •                or not.
      

    */
    public void setAutoUpdate(boolean autoUpdate)
    {
    this.autoUpdate = autoUpdate;
    }

    /**

    • Returns the framerate text field.
    • @return The text field which displays the framerate.
      */
      public JTextField getTextField()
      {
      return textField;
      }

    /**

    • Returns the main panel.
    • @return The panel which holds the text field and the exit button.
      */
      public JPanel getPanel()
      {
      return panel;
      }

}`

Please add that and tell me if it still doesn’t work. I’m sorry this change hasn’t been noticed. I saw William Denniss made a page on the wiki some time ago but the wiki has been replaced so I don’t think anybody noticed it.

I found the error :slight_smile:
I just had to add

locale.addBranchGraph(windowMgr);

I don’t know why, but before it worked without that line.

I don’t know why either but I began to use it after the changes and I based my code on Xith3DGuiTest so it worked fine from beginning.