我正在构建一个交通模拟器,通过制作应该与交通灯模拟器交互的视频播放器,我需要将交通信号灯的帧插入到从机器播放的视频上。我刚刚使用JPanel将交通信号灯吸引到框架上,并确保在初始化播放器后创建框架。但问题是,在执行各种setAlwaysOnTop()或toFront()方法后,它们仍然不会叠加在一起。我希望框架在一段时间后通过视频播放器然后消失。但是一旦加载了帧,它就会使视频最小化。请帮帮我。
以下是代码。
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 < 5000)
{
}
Time blah = player.getMediaTime();
boss = blah;
player.stop();
long restart = System.currentTimeMillis();
while (System.currentTimeMillis()-restart < 5000)
{
}
boss = initial;
player.setMediaTime (blah);
player.start();
start = System.currentTimeMillis();
while (System.currentTimeMillis()-start < 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");
}
}