動画と2D表示(図形)を重ねる方法
車両側面視の動画と車両モデル計算結果とのオーバーレイを
実現する方法。なんだかJMFでやれそう。
「動画(ビジュアルコンポーネント)の上に他のコンポーネントをかぶせる場合、ビジュアルコンポーネントがライトウェイトである必要があるらしい。また、上に被せようとしているコンポーネントもライトウェイトで無ければならない。これはヘビーウェイトコンポーネントは透明化できないという理由による。」
参考になったサイト↓
http://feather.cocolog-nifty.com/weblog/2007/11/java_cf70.html
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Label;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.protocol.DataSource;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
public class MyPlayer {
public static void main(final String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
final String string = new String("文字列オーバレイテスト");
URL file;
try {
file = new URL("http://feather.cocolog-nifty.com/weblog/WindowsLiveWriter/sample.avi");
} catch (MalformedURLException e1) {
e1.printStackTrace();
return;
}
JFrame app = new JFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
* (1) ヘビーウェイトコンポーネント(Label)かライトウェイトコンポーネント(JLabel)か。
* ヘビーウェイトコンポーネントは透明化できない。
*/
// Label label = new Label(string, Label.CENTER);
JLabel label = new JLabel(string, JLabel.CENTER);
label.setBorder(BorderFactory.createLineBorder(Color.BLUE));
label.setBounds(40, 80, 180, 60);
label.setForeground(Color.BLUE);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(320, 240));
layeredPane.setBorder(BorderFactory.createTitledBorder("階層化区画"));
try {
MediaLocator locator = new MediaLocator(file);
/*
* (2) ビジュアルコンポーネントをヘビーウェイトとするかライトウェイトとするか。
* ヘビーウェイトの場合、動画は必ず最前面に来る。
*/
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));
Manager.setHint(Manager.PLUGIN_PLAYER, new Boolean(true));
DataSource source = Manager.createDataSource(locator);
Player player = Manager.createRealizedPlayer(source);
if (player.getVisualComponent() != null) {
player.getVisualComponent().setBounds(50, 50, 160, 120);
layeredPane.add(label);
layeredPane.add(player.getVisualComponent());
layeredPane.moveToFront(label);
app.add(player.getControlPanelComponent(), BorderLayout.NORTH);
app.add(layeredPane, BorderLayout.CENTER);
app.pack();
app.setVisible(true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}