Canvas don't show in the JMEDesktop

Hi, I just want to add my own implementation of java.awt.canvas into JMEDesktop, but got nothing.

I list some small codes below



Mycanvas.class



import java.awt.Canvas;

import java.awt.Color;

import java.awt.Graphics;



class Mycanvas extends Canvas {

  int x, y, r;



  int red, green, blue;



  Mycanvas() {

      setSize(100, 100);

      setBackground(Color.cyan);

  }



  public void paint(Graphics g) {

      g.setColor(Color.orange);

      g.fillOval(12, 12, 45, 45);

  }

}



HelloJMEDesktop .class



import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;



import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.SwingUtilities;



import jmetest.awt.swingui.TestJMEDesktop;

import com.jme.app.SimpleGame;

import com.jme.input.MouseInput;

import com.jme.renderer.Renderer;

import com.jme.scene.Node;

import com.jme.scene.SceneElement;

import com.jme.scene.state.LightState;

import com.jmex.awt.swingui.JMEDesktop;



/**

  • Very short example for JMEDesktop - see {@link TestJMEDesktop} for more features.

    */

    public class HelloJMEDesktop extends SimpleGame {



        private Node guiNode;

        JTextField textfield;



        protected void simpleInitGame() {

            // create a node for ortho gui stuff

            guiNode = new Node( "gui" );

            guiNode.setRenderQueueMode( Renderer.QUEUE_ORTHO );



            // create the desktop Quad

            final JMEDesktop desktop = new JMEDesktop( "desktop", 800, 600, input );

            // and attach it to the gui node

            guiNode.attachChild( desktop );

            // center it on screen

            desktop.getLocalTranslation().set( display.getWidth() / 2 , display.getHeight() / 2 , 0 );



            // perform all the swing stuff in the swing thread

            SwingUtilities.invokeLater( new Runnable() {

                public void run() {

                    // make it transparent blue

                    desktop.getJDesktop().setBackground( new Color( 0, 0, 1, 0.2f ) );

                 

                    JPanel test = content();

                    test.setLocation( 10, 10 );

                    test.setSize(new Dimension(400,400));

                 

                    desktop.getJDesktop().add( test  );

                }

            } );



            // don't cull the gui away

            guiNode.setCullMode( SceneElement.CULL_NEVER );

            // gui needs no lighting

            guiNode.setLightCombineMode( LightState.OFF );

            // update the render states (especially the texture state of the deskop!)

            guiNode.updateRenderState();

         

            guiNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);

            guiNode.updateRenderState();

         

            // update the world vectors (needed as we have altered local translation of the desktop and it's

            //  not called in the update loop)

            guiNode.updateGeometricState( 0, true );



            // finally show the system mouse cursor to allow the user to click our button

            MouseInput.get().setCursorVisible( true );

        }



        protected void simpleRender() {

            // draw the gui stuff after the scene

            display.getRenderer().draw( guiNode );

        }



        private JPanel content() {

          JPanel panel = new JPanel(new BorderLayout());

          panel.setBackground(Color.white);

              Mycanvas test = new Mycanvas();

          panel.add(test);

         

          return panel;

      }



        public static void main( String[] args ) {

            new HelloJMEDesktop().start();

        }

    }



    But if i put Mycanvas in ordinary JFrame's panel, it works fine,TestFrame.class is as below



    import java.awt.Color;

    import javax.swing.JFrame;

    import javax.swing.JPanel;



    public class TestFrame extends JFrame {



      /

        * Launch the application

        *

        * @param args

        */

      public static void main(String args[]) {

          try {

            TestFrame frame = new TestFrame();

            frame.setVisible(true);

          } catch (Exception e) {

            e.printStackTrace();

          }

      }



      /


        * Create the frame

        */

      public TestFrame() {

          super();

          getContentPane().setLayout(null);

          setBounds(100, 100, 500, 375);

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



          final JPanel panel = new JPanel();

          panel.setBackground(Color.white);

          panel.setBounds(55, 40, 273, 215);

          Mycanvas test = new Mycanvas();

          panel.add(test);

          getContentPane().add(panel);

         

          //

      }



    }



    any advise?  embarassed



    thanke in advace



    ====================================

    I posted this question in the "General" yesterday, but no one answered, maybe i must post here.

I guess, canvas is a component to use native rendering etc. and it does not use the graphics (context) of the outer components. Try switching to a simple JComponent instead. Canvas most likely won't work.

Thank you irrisor! But if i do want to use canvas to be drawed in JMEDestop, How can i achieve that?

Probably you can't.

Thank you :slight_smile:

i think i'll dig it deeper