title: Overlaying a JFrame with a drawn image on a video player built on JMF
body: <p>I am building a traffic simulator by making video player which is supposed to interact with a traffic light simulator and i need to insert a frame of the traffic light onto the video being played from the machine. I have just drawn the traffic light onto the frame using JPanel and have made sure the frame is created after the player is initialized. But the problem is that they still don't overlay on top of each other after doing the various setAlwaysOnTop() or toFront() methods. I want the frame to come over the video player after some time and then disappear. But as soon as the frame is loaded, it minimizes the video. Please help me out.</p>
  
  <p>Below is the code.</p>
  
  <pre><code>class MediaPlayer extends Frame implements ActionListener,ControllerListener,ItemListener
  {
  
      Player player;
      Component vc, cc;
      Graphics2D traffic;
      int x,y,w,h;
      Time boss = new Time(0);
      //  boolean first = false, loop = false;
  
      MediaPlayer (String title) throws IOException
      {
          super (title);
          toFront();
          GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
          GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
          if (devices[0].isFullScreenSupported()) {
              setUndecorated(true);
              setVisible (true);
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              x = (5*screenSize.width)/8;
              y = screenSize.height/2;
              w = 35;
              h = 50;
              setSize(screenSize);
              devices[0].setFullScreenWindow(this);
              validate();
              if (player != null)
                  player.close ();
  
              try
              {
                  player = Manager.createPlayer (new MediaLocator ("file:C:\\Documents and Settings\\User\\My Documents\\Downloads\\Fig21_06_07\\Fig21_06_07\\bailey.mpg"));
                  if (player == null)
                  {
                      System.out.println ("Trouble creating a player.");
                      return;
                  }
                  player.addControllerListener (this);
                  System.out.println("asking for prefetch");
                  player.prefetch ();
                  setSize(screenSize);
                  toFront();
                  boolean flag = true;
                  Time initial = player.getMediaTime(); 
                  do {//  
                      JFrame frame = new JFrame();
                      setAlwaysOnTop(true);
                      frame.setAlwaysOnTop(true);
                      frame.toBack();
  
                      int he = 50, wi = 35;
                      //int x = 400, y = 400;
                      frame.setUndecorated(true);
                      frame.setVisible(true);
                      frame.setAlwaysOnTop(true);
                      frame.setLocation(x,y); 
                      frame.setSize(wi,he);
                      frame.setResizable(false);
                      ShapesJPanel tr = new ShapesJPanel();
                      tr.setSize(100,100);
                      tr.setBounds(0, 0,100,100);
                      tr.setBackground(Color.MAGENTA);
                      tr.x = 0;
                      tr.y = 0;
                      tr.w = 100;                     
                                          tr.h1 = 100;
                      frame.add(tr);
                      validate();
                      pack();
                      frame.setVisible(true);
                      setVisible(true);
                      toFront();
                      x = (5*screenSize.width)/8;
                      y = screenSize.height/2;
                      w = 15;
                      h = 20;
                      setSize(screenSize);
                      boss = initial;
                      long start = System.currentTimeMillis();
                      int i = 0;
                      while (System.currentTimeMillis()-start &lt; 5000)
                      {
                      }
                      Time blah = player.getMediaTime();
                      boss = blah;
                      player.stop();
                      long restart = System.currentTimeMillis();
                      while (System.currentTimeMillis()-restart &lt; 5000)
                      {
                      }
                      boss = initial;
                      player.setMediaTime (blah);
                      player.start();
                      start = System.currentTimeMillis();
                      while (System.currentTimeMillis()-start &lt; 5000)
                      {
                      }
                      player.setMediaTime (initial);
                      player.prefetch();
                      setSize(screenSize);
                      flag = false;
                  }while(true);
              }
              catch (java.io.IOException e2)
              {
                  System.out.println (e2);
                  return;
              }
              catch (NoPlayerException e2)
              {
                  System.out.println ("Could not find a player.");
                  return;
              }
          }
          else {
              pack();
              setVisible(true);
          }
      }
  
      public void actionPerformed (ActionEvent e)
      {
      }
  
      public void controllerUpdate (ControllerEvent e)
      {
          // A ControllerClosedEvent is posted when player.close is
          // called.  If there is a visual component, this component must
          // be removed.  Otherwise, this visual component appears
          // blanked out on the screen. (To be consistent, we do the same
          // thing for the control panel component.)
          //
          // Note: This problem occurs when run under JMF 2.1 Windows and
          //       SDK 1.3 on a Windows 98 SE platform.
  
          if (e instanceof ControllerClosedEvent)
          { 
              if (vc != null)
              {
                  remove (vc);
                  vc = null;
              }
  
              if (cc != null)
              {
                  remove (cc);
                  cc = null;
              }
  
              return;
          }
  
          if (e instanceof EndOfMediaEvent)
          {
              player.setMediaTime(new Time(0));
              player.start();
              return;
          }
  
          if (e instanceof PrefetchCompleteEvent)
          {
              System.out.println("Prefetch occured..");
              player.start ();
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              player.getVisualComponent().setSize(screenSize.width, screenSize.height);
              player.getVisualComponent().setMinimumSize(screenSize);
              player.getVisualComponent().setBounds(0, 0, screenSize.width, screenSize.height);
              return;
          }
  
          if (e instanceof RealizeCompleteEvent)
          {
              System.out.println("player realized..");
              vc = player.getVisualComponent ();
  
              if (vc != null){
                  Dimension screenSize = Toolkit.getDefaultToolkit(). getScreenSize();
                  vc.setMinimumSize(screenSize);
                  vc.setBounds(0, 0, screenSize.width, screenSize.height);
                  add (vc); 
              }
              pack ();
  
          }
  
      }
  
      public void itemStateChanged (ItemEvent e)
      {
  
      }
  
      public void paint (Graphics g)
      {
          super.paint (g);
          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
          int wi = screenSize.width;
          int he = screenSize.height;
          g.setColor (Color.black);
          g.fillRect (0, 0, wi, he);
          // Call overridden Frame superclass paint method.  That method
          // will call each contained container and component (including
          // the control panel component) paint method.
      }
  
      // Eliminate control panel component flicker by preventing frame
      // background from being cleared.
  
      public void update (Graphics g)
      {
          paint (g);
      }
  
      public static void main (String [] args) throws IOException
      {
          new MediaPlayer ("Road Simulator");
      }
  }
  </code></pre>