Search code examples
javajpanelscalemp4

Add MP4-File to JPanel and scale it - Java


I have a JPanel in a JFrame:

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Demo{

    public static void main(String [] args) {
        JFrame frame = new JFrame() {
            private static final long serialVersionUID = 1L;
            {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setSize(200, 200);
                setLocationRelativeTo(null);

                add(new JPanel() {
                    private static final long serialVersionUID = 1L;
                    {
                        //Here I want to add the video!
                    }
                });
            }
        };

        frame.setVisible(true);
    }
}

Is it possible to...

  • add a video to the JPanel

  • scale the video (As you can do it with Images this way: getScaledInstance(width, height, Image.SCALE_SMOOTH))?

  • play the video


Solution

  • Yes... but I wouldn't recommend you use Java/Swing for this though. Java/Swing's support for video is poor. If you must use Java/Swing, then use JMF. I know Xuggler exists, but haven't used it myself. Research JMF first and know what you are giving up by using it (ex. loosing ability to run anywhere) and its limitations (its old and supported codecs).

    If you have the choice on what technologies to use, check out JavaFX's multimedia support.

    Links: JMF, Xuggler, JavaFX Question about Video