JMF 비디오 크기 조정
JMF 사용하여 Java 응용 프로그램에서 비디오를 재생하려고 할 때, 비디오가 재생 비디오 크게 표시하려면
아래의 코드와 같이 다른 jpanel gridbag 레이아웃이 안에 배치합니다.
채우기 제약 조건 추가 할 때 가로 세로 비율에 따라 비디오가 늘어납니다.
public class VideoPanel extends JPanel
{
private Player mediaPlayer;
private File file;
public VideoPanel(String videoFile, String path)
{
setOpaque(false);
file = new File(path+"/video/"+videoFile);
URL mediaURL = null;
try {
mediaURL = file.toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
setLayout( new BorderLayout() );
Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
try{
mediaPlayer = Manager.createRealizedPlayer( mediaURL );
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();
double scale = getScale(this.getWidth(),this.getHeight(),video.getWidth(),video.getHeight());
video.setPreferredSize(new Dimension((int)scale*video.getWidth(),(int)scale*video.getHeight()));
if(video != null)
add(video,BorderLayout.CENTER );
if(controls != null)
add(controls,BorderLayout.SOUTH );
}
catch ( NoPlayerException noPlayerException ){
System.err.println( "No media player found" );
}
catch ( CannotRealizeException cannotRealizeException ){
System.err.println( "Could not realize media player" );
}
catch ( IOException iOException ){
System.err.println( "Error reading from the source" );
}
}
private double getScale(int panelWidth, int panelHeight, int imageWidth, int imageHeight)
{
double scale = 1;
double xScale;
double yScale;
xScale = (double) panelWidth / imageWidth;
yScale = (double) panelHeight / imageHeight;
scale = Math.min(xScale, yScale);
return scale;
}
public void stopPlay()
{
mediaPlayer.stop();
}
}