I have black bars surrounding my video player, as shown in the figure below. How do I get rid of them? I tried fixing the sizes in the AVPlayer class but while that stopped the random resizing of the video player, it didn't get rid of the black bars.
My second question: how do make it so that the video player sits where it is now, but moves up a bit so that the top of the player is on the same row as the combobox? Right now it is on the same row as the JLabel displaying "English" but I want it to extend on both rows.
Below you will find my SSCCE. It is still a bit long but I got rid of all the dependencies. You should get the image above if you compile it. I put the AVPlayer class as an inner class to simplify the code, it actually has its own class. You will need to add a video source for the video player to show up.
public class SSCCE extends JPanel {
private final JLabel kanjiLabel;
private final JLabel englishLabel;
private final JPanel wordPanel, cbPanel;
private final JComboBox chapters;
private final DefaultComboBoxModel model;
private final JLabel chapLabel = new JLabel("Chapter");
private AVPlayer player;
private final GridBagConstraints constraints = new GridBagConstraints();
public SSCCE() {
setLayout(new GridBagLayout());
setConstraints();
//wordPanel
wordPanel = new JPanel();
wordPanel.setLayout(new BoxLayout(wordPanel, BoxLayout.Y_AXIS));
wordPanel.setPreferredSize(new Dimension(500, 150));
kanjiLabel = new JLabel();
englishLabel = new JLabel();
kanjiLabel.setFont(new java.awt.Font("Dialog", 0, 50));
englishLabel.setFont(new java.awt.Font("Dialog", 0, 40));
wordPanel.add(englishLabel);
wordPanel.add(Box.createRigidArea(new Dimension(10, 10)));
wordPanel.add(kanjiLabel);
//chapter combox
chapters = new JComboBox();
model = new DefaultComboBoxModel();
chapters.setModel(model);
cbPanel = new JPanel();
addCB();
constraints.gridy += 1;
add(wordPanel, constraints);
//add the audio video panel
player = new AVPlayer();
constraints.gridx += 1;
add(player, constraints);
}
private void setConstraints() {
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1.0;
constraints.weighty = 0.0;
}
private void addCB() {
setConstraints();
cbPanel.removeAll();
cbPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));
cbPanel.add(chapLabel);
cbPanel.add(chapters);
add(cbPanel, constraints);
}
public void displayNewWord() {
kanjiLabel.setText("Kanji");
englishLabel.setText("English");
player.playVideo("video.mp4"); //replace this with a video on your computer
revalidate();
repaint();
}
public class AVPlayer extends JPanel {
private static final String VLCPATH = "C:\\VideoLAN\\VLC";
private final MediaPlayerFactory mpf;
private final EmbeddedMediaPlayer emp;
public AVPlayer() {
//this panels sizes
setPreferredSize(new Dimension(150, 150));
setMinimumSize(new Dimension(150, 150));
setMaximumSize(new Dimension(150, 150));
//canvas
setLayout(new BorderLayout());
Canvas canvas = new Canvas();
canvas.setPreferredSize(new Dimension(150, 150));
add(canvas, BorderLayout.CENTER);
//load the native vlc library
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), VLCPATH);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
mpf = new MediaPlayerFactory();
emp = mpf.newEmbeddedMediaPlayer();
emp.setVideoSurface(mpf.newVideoSurface(canvas));
}
public void playVideo(String path) {
emp.prepareMedia(path);
emp.play();
emp.setRepeat(true);
}
}
}
And the main method
JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SSCCE sscce = new SSCCE();
frame.add(sscce, BorderLayout.NORTH);
frame.setVisible(true);
sscce.displayNewWord();
frame.revalidate();
The black bars are part of the video output from the embedded VLC media player.
You need to set appropriate aspect ratio and/or crop geometry to get rid of them.
For example:
mediaPlayer.setCropGeometry("4:3"); // W:H
mediaPlayer.setCropGeometry("719x575+0+0"); // WxH+L+T
mediaPlayer.setCropGeometry("6+10+6+10"); // L+T+R+B
mediaPlayer.setAspectRatio("185:100");
Only you know what the correct values should be.
Alternatively, if the black bars are not in the native video output, then this is caused by the your Canvas
size not having the same aspect ratio as the video.
You can work out which of the two issues you're having by setting your Canvas
background to some other colour.