Resize JPanel to fit new JLabel icon - java

I'm trying to create an animation (spinning text) by repeatedly changing the icon on a JLabel. The issue is the images are not of the same size, and when they are bigger than the size of the first image, they are clipped.
One way around this is to setPreferredSize for the JLabel so that all images fit - but I imagine there must be a way dinamically resize the JPanel containing the JLabel?
In the code bellow I've also tried removing the JLabel alltogether, creating a new one and then adding the new one, but to the same effect.
public class AnimationPanelv2 extends JPanel{
private JButton start = new JButton("Start Animation");
private JLabel img = new JLabel();
private JTextField animSpeed = new JTextField(10);
private JTextField filePrefix = new JTextField(10);
private JTextField noOfImg = new JTextField(10);
private JTextField audioFile = new JTextField(10);
private Timer timer;
private AudioClip clip;
private ArrayList<ImageIcon> icon = new ArrayList<>();
private int step=0;
public AnimationPanelv2() {
//button is for starting the animation
start.addActionListener(new Animatie());
this.setLayout(new BorderLayout());
add(start, BorderLayout.NORTH);
//showing the label with the first frame
Class metaObj = this.getClass();
URL url = metaObj.getResource("/image/L1.gif");
img.setIcon(new ImageIcon(url));
// img.setPreferredSize(new Dimension(500,550));
add(img, BorderLayout.CENTER);
//control panel
JPanel controls = new JPanel(new GridLayout(4,2));
controls.setBorder(new TitledBorder("Enter information for animation"));
controls.add(new JLabel("Animation speed in ms"));
controls.add(animSpeed);
controls.add(new JLabel("Image file prefix"));
controls.add(filePrefix);
controls.add(new JLabel("Number of images"));
controls.add(noOfImg);
controls.add(new JLabel("Audio file"));
controls.add(audioFile);
//
add(controls, BorderLayout.SOUTH);
}
private class TimerAnimation implements ActionListener {
public void actionPerformed(ActionEvent e) {
remove(img);
img = new JLabel(icon.get(step++));
img.setVisible(true);
add(img, BorderLayout.CENTER);
// img.revalidate();
// img.repaint();
validate();
repaint();
updateUI();
if (step==Integer.parseInt(noOfImg.getText())) step=0;
}
}
private class Animatie implements ActionListener {
public void actionPerformed(ActionEvent e) {
//getting data from the text fields
int ms = Integer.parseInt(animSpeed.getText());
String s = filePrefix.getText();
int nr = Integer.parseInt(noOfImg.getText());
String audioFilePath = audioFile.getText();
// clip
Class metaObj = this.getClass();
URL url = metaObj.getResource("/audio/"+audioFilePath);
clip = Applet.newAudioClip(url);
//image loading
for (int i=1; i<=nr; i++){
url = metaObj.getResource("/image/"+s+i+".gif");
System.out.println("/image/"+s+i+".gif");
icon.add(new ImageIcon(url));
}
//timer
timer = new Timer(ms, new TimerAnimation());
timer.start();
clip.loop();
}
}
public static void main(String[] args) {
JFrame jf = new JFrame("This class test");
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new AnimationPanelv2());
jf.pack();
jf.setVisible(true);
}
}
This whole panel will be used in an applet.
This is a screenshot: http://screencast.com/t/UmqQFZHJVy
The images that are supposed to be the frames, should be located in an /images/ sub-directory and if the user enters n for the number of frames and F for the image
prefix, then the files are F1, F2, and so on, to Fn (GIFs). The sound file should be in an /audio/ sub-directory, and the entire file name is given by the user.

You can try to create list of JLabels for each image, add them to a panel with CardLayout and swap cards.

Okay well a JLabel should size automatically to its given content, so to solve JPanel issue, simply override getPreferredSize() of JPanel containing the JLabel and return Dimensions according to the JLabel size.
public class MyPanel extends JPanel {
JLabel label=...;
#Override
public Dimension getPreferredSize() {
return new Dimension(label.getWidth(),label.getHeight());
}
}
also dont forget when you change Icon of JLabel call revalidate() and repaint() on JPanel instance in order for size changes to refelect.

Related

How to add a JPanel graphic to a JFrame without covering the JFrame

I'm trying to add a small tornado graphic (upside down pyramid) to my Frame. I can get the tornado by adding it to the frame in the main method but when I do that all I see is the tornado graphic and not the GUI underneath it.
So, I'm now trying to add the Tornado graphic to the frame when its created in the createComponents method but it now doesn't appear at all. Instead all I can see it the GUI in the frame.
I' probably missing something easy but I can't seem to figure it out. I'm not sure what I need to to in order to get the GUI and the tornado graphic both to appear.
public class EFScaleViewer {
public static void main(String[] args) {
// TODO Auto-generated method stub
TornadoFrame frame = new TornadoFrame();
frame.setTitle("EF Scale");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Here is where I create the frame and am trying to add the tornado:
public class TornadoFrame extends JFrame{
private JButton submit;
private JLabel label;
static JLabel errorLabel;
static JTextField textBox;
JPanel tornado = new TornadoComponent();
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 300;
//Constructor for the frame
public TornadoFrame() {
super();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
createComponents();
}
private void createComponents()
{
textBox = new JTextField(" ");
submit = new JButton("Submit");
label = new JLabel("Please enter a windspeed:");
errorLabel = new JLabel("Error Message " );
JPanel panel = new JPanel();
panel.add(label);
panel.add(textBox);
panel.add(submit);
panel.add(errorLabel);
panel.add(tornado);
add(panel);
}
}
I know this is working but I may be missing something so here is where I create the tornado:
public class TornadoComponent extends JPanel {
public void paintComponent(Graphics g) {
int[] xPoints = {100,200,0};
int[] yPoints = {0,200,200};
int nPoints = 3;
g.drawPolygon(xPoints, yPoints, nPoints);
}
}
You have to set the JPanels size for it to be able to display Graphics.
static class TornadoComponent extends JPanel {
public TornadoComponent() {
setPreferredSize(new Dimension(500, 500));
}
#Override
public void paintComponent(Graphics g) {
//Whatever
}
}
And in order to trigger paintComponent(Graphics g) you have to add tornado.repaint(); at the end of your createComponents() function.
private void createComponents() {
//All your components
panel.add(tornado);
add(panel);
tornado.repaint();
}
Now the Polygon is shown but not at the right place (slightly off the image)
Therefore we have to arrange your JPanels a bit:
private void createComponents() {
textBox = new JTextField(" ");
submit = new JButton("Submit");
label = new JLabel("Please enter a windspeed:");
errorLabel = new JLabel("Error Message " );
JPanel upper = new JPanel();
upper.setLayout(new BoxLayout(upper,BoxLayout.X_AXIS));
upper.add(label);
upper.add(textBox);
upper.add(submit);
upper.add(errorLabel);
JPanel lower = new JPanel();
lower.setLayout(new BoxLayout(lower,BoxLayout.X_AXIS));
lower.add(tornado);
JPanel over = new JPanel();
over.setLayout(new BoxLayout(over,BoxLayout.Y_AXIS));
over.add(upper);
over.add(lower);
add(over);
tornado.repaint();
}
Basically I make some boxes...
Over
Upper
... your stuff with text
Lower
Our tornado
Now our tornado is the wrong way round...
int[] xPoints = {100,200,150};
int[] yPoints = {0,0,150};
And voilĂ :
We just created a very basic tornado that is not aiming at anything :)
If you want to change the tornados position later you just have to recall tornado.repaint(); and you are all set.

JPanel appears as a small white box

I am in the early stages of trying to create a Java 2d graphics paint program. I'm using a flow layout, and I'm using three panels. The first two are rows of buttons, combo boxes, etc. and the third is meant to be a large, blank, white panel that will be used to paint on. The first two panels show up beautifully, but the paint panel appears as a small white box next to the second button panel. Any help would be appreciated.
public class DrawingApp extends JFrame
{
private final topButtonPanel topPanel = new topButtonPanel();
private final bottomButtonPanel bottomPanel = new bottomButtonPanel();
private final PaintPanel paintPanel = new PaintPanel();
public DrawingApp()
{
super("Java 2D Drawings");
setLayout(new FlowLayout());
add(topPanel);
add(bottomPanel);
add(paintPanel);
}
public static void main(String[] args)
{
DrawingApp frame = new DrawingApp();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(750,500);
frame.setVisible(true);
}
}
public class topButtonPanel extends JPanel
{
private final String[] names = {"Line", "Oval", "Rectangle"};
private final JButton undo = new JButton("Undo");
private final JButton clear = new JButton("Clear");
private final JLabel shape = new JLabel("Shape:");
private final JComboBox<String> shapesComboBox = new JComboBox(names);
private final JCheckBox filled = new JCheckBox("Filled");
public topButtonPanel()
{
super();
setLayout(new FlowLayout());
add(undo);
add(clear);
add(shape);
shapesComboBox.setMaximumRowCount(3);
add(shapesComboBox);
add(filled);
}
}
public class bottomButtonPanel extends JPanel
{
private final JCheckBox useGradient = new JCheckBox("Use Gradient");
private final JButton firstColor = new JButton("1st Color");
private final JButton secondColor = new JButton("2nd Color");
private final JLabel lineWidthLabel = new JLabel("Line Width:");
private final JLabel dashLengthLabel = new JLabel("Dash Length:");
private final JTextField lineWidthField = new JTextField(2);
private final JTextField dashLengthField = new JTextField(2);
private final JCheckBox filled = new JCheckBox("Dashed");
public bottomButtonPanel()
{
super();
setLayout(new FlowLayout());
add(useGradient);
add(firstColor);
add(secondColor);
add(lineWidthLabel);
add(lineWidthField);
add(dashLengthLabel);
add(dashLengthField);
add(filled);
}
}
public class PaintPanel extends JPanel
{
public PaintPanel()
{
super();
setBackground(Color.WHITE);
setSize(700,400);
}
}
Basically, it's a misunderstanding of how the Swing API works.
Swing relies (heavily) on the layout management API which is used to make decisions about how large components should be (and where they should be placed)
Using setSize is pointless, as the layout manager will make it's own decisions about what it thinks the size of your component should be and will adjust it accordingly.
You can make suggestions to the layout manager about how large you'd like the component to be using getPreferred/Minimum/MaximumSize, for example
public class PaintPanel extends JPanel
{
public PaintPanel()
{
super();
setBackground(Color.WHITE);
}
public Dimension getPreferredSize() {
return new Dimension(700, 400);
}
}
Just remember, layout managers are well within their right to ignore these values, so you need to have a better understanding of how these managers work
See Laying Out Components Within a Container for more details

One of the JLayeredPane layers is not showing up

There are a number of similar posts, notably:
Java Swing JLayeredPane not showing up
and
Top layer in JLayeredPane not displaying
But none of them seem to solve my specific problem.
I have the following code:
public class PlayScreen{
private JLayeredPane playScreen;
private Player player;
private JPanel towerButtons;
private JPanel detailPanel;
private JLabel map;
public PlayScreen(Player player){
this.player = player;
playScreen = new JLayeredPane();
playScreen.setLayout(new BorderLayout());
setTowerButtons();
playScreen.add(towerButtons, BorderLayout.EAST);
playScreen.setLayer(towerButtons, 0);
setDetailLabels();
playScreen.add(detailPanel, BorderLayout.WEST);
playScreen.setLayer(detailPanel, 0);
setMap(player.getNextMap());
playScreen.add(map, BorderLayout.CENTER);
playScreen.setLayer(map, 0);
JButton playButton = new JButton("play");
playScreen.add(playButton, BorderLayout.NORTH);
playScreen.setLayer(playButton, 0);
playButton.addActionListener(new playListener());
playScreen.setVisible(true);
}
where setTowerButtons(), setDetailLabels() and setMap() methods called in the constructor basically initialize the towerButtons, detailPanel and map fields.
When the "play" button is pressed, the following code is called:
public class playListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Image image = null;
try{
image = ImageIO.read(new File("mob1.bmp"));
}
catch (Exception e1){
System.out.println("image creation failed");
}
ImageIcon img = new ImageIcon(image);
JLabel mob1Button = new JLabel(img);
System.out.println("hi");
playScreen.add(mob1Button, BorderLayout.CENTER);
playScreen.setLayer(mob1Button, 1);
mob1Button.setVisible(true);
}
}
I know that this is definitely called, because "hi" appears on the output stream.
(The playScreen is a JLayeredPane that is constructed from this:)
public class View {
private JFrame frame;
private Container contentPane;
private Player player;
private LoadScreen loadScreen;
private PlayScreen playScreen;
public View(Player player){
frame = new JFrame("Display");
contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
this.player = player;
frame.setLocationRelativeTo(null); //puts frame in middle of screen
frame.pack();
frame.setVisible(true);
}
public void makeLoadScreen(){
loadScreen = new LoadScreen();
contentPane.add(loadScreen.getLoadScreen(), BorderLayout.CENTER);
frame.pack();
}
public void makePlayScreen(Player player){
contentPane.remove(loadScreen.getLoadScreen());;
playScreen = new PlayScreen(player);
contentPane.add(playScreen.getPlayScreen());
frame.pack();
}
public class LoadScreen{
private JPanel loadScreen;
public LoadScreen(){
loadScreen = new JPanel();
JButton play = new JButton("Play");
JButton load = new JButton("Load");
loadScreen.setLayout(new GridLayout(2,1,0,0));
loadScreen.add(play);
loadScreen.add(load);
play.addActionListener(new OpenActionListener());
load.addActionListener(new LoadActionListener());
}
public JPanel getLoadScreen(){
return loadScreen;
}
public class OpenActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
makePlayScreen(player);//might not be receiving correct player variable?
}
}
public class LoadActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}
}
}
public GameManager(){
player = new Player();
player.setNextMap("map.bmp");
//Tower tower = new Tower1();
//player.addUnlockedTower(tower);
view = new View(player);
view.makeLoadScreen();
}
A GameManager Class is created. This creates the loadScreen, which has two buttons, "play" and "load." Clicking on "play" creates the playScreen, which is governed by a separate class.
Sorry for all the code, but I have no idea anymore where it is going wrong, I thought it was to do with not setting the sizes of the playScreen, towerButtons etc, as in the similar post, but that didn't seem to work when I added preferred /min / max sizes.
Currently an image of the map comes up, but the image of the "mob1" does not appear at all, and I would expect it to overlay the map image.

Adding picture to JFrame from another class java

I am trying to make a program that reads a file chosen by the user, and after reading the file - the suffix "txt" is changed to "gif" and the file is saved as a picture (which is in the same catalogue as the file). The thing is, this picture variable gets its value in the "actionPerformed-method" and after that I want to add it to a frame in another class- but it doesn't show. Here's the code in my OptionsPane-class:
public class OptionsPane extends JComponent implements ActionListener{
private JButton buttonOne = new JButton("Alt.1");
private JButton buttonTwo = new JButton("Alt.2");
private JButton buttonThree = new JButton("Alt.3");
private int option;
private JButton buttonChoose = new JButton("Choose file");
private FileHandler filehandler;
private String picture;
private JLabel picLabel;
public OptionsPane(){
JLabel label = new JLabel("Choose optimization method", SwingConstants.CENTER);
JPanel subPanel = new JPanel();
label.setForeground(Color.CYAN);
label.setFont(new Font("Tahoma", Font.BOLD, 15));
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(label);
buttonOne.addActionListener(this);
buttonTwo.addActionListener(this);
buttonThree.addActionListener(this);
buttonChoose.addActionListener(this);
subPanel.setBackground(Color.DARK_GRAY);
subPanel.add(buttonOne);
subPanel.add(buttonTwo);
subPanel.add(buttonThree);
subPanel.add(buttonChoose);
this.add(subPanel);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttonOne){
option = 1;
System.out.println("You clicked button 1!");
}else if(e.getSource() == buttonTwo){
option = 2;
System.out.println("You clicked button 2!");
}else if(e.getSource() == buttonThree){
option = 3;
System.out.println("You clicked button 3!");
}else if(e.getSource() == buttonChoose){
System.out.println("hello");
option = 4;
filehandler = new FileHandler();
filehandler.read();
picture = filehandler.getFilePath().replaceFirst("txt", "gif");
picLabel = new JLabel(new ImageIcon(picture));
this.add(picLabel);
}
}
}
The frame is in the "MainFrame"-class, which looks like this at the moment:
public class MainFrame extends JFrame{
private JFrame frame = new JFrame();
private String picture;
private JLabel picLabel;
public MainFrame(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1300, 800));
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
OptionsPane optionspane = new OptionsPane();
frame.add(optionspane);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
}
}
Why isn't the picture visible in the mainframe?
EDIT
It works now!
https://stackoverflow.com/a/22380387/3271504
Thank you for your help #arooaroo . I tried to write down some of what you wrote, but it still didn't work when I wanted to add an image based on what file the user had chosen (for example if the user chose file text1.txt i wanted the corresponding picture "text1.gif" to show up). With your help, the picture showed up when I typed a specific pathway with "/"-slashes, but when I chose a file and tried to load the picture from the file pathway, it didn't show and that is because it had backslashes in the pathways. This is how it should be (such an irritating problem):
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttonOne){
option = 1;
System.out.println("You clicked button 1!");
}else if(e.getSource() == buttonTwo){
option = 2;
System.out.println("You clicked button 2!");
}else if(e.getSource() == buttonThree){
option = 3;
System.out.println("You clicked button 3!");
}else if(e.getSource() == buttonChoose){
filehandler = new FileHandler();
filehandler.read();
filepath = filehandler.getFilePath();
picture = filepath.replaceFirst("txt", "gif");
picture = picture.replaceAll("\\\\", "/");
ImageIcon icon = new ImageIcon(picture);
mainFrame.setPicture(icon);
}
Thank you for your help!
Once you separate your GUI code in to separate classes - which is a Good Thing - you will find the eternal challenge for GUI programming is allowing for clean communication between them where there are inter-dependencies.
In this instance perhaps the simplest approach is to pass in a reference of MainFrame into OptionsPane.
Let's assume you create an additional method in MainFrame for setting the picture:
public class MainFrame extends JFrame{
// all instance vars as before
public MainFrame() {
// same as before except for this line...
OptionsPane optionspane = new OptionsPane(this);
}
public void setPicture(JLabel pictureLabel) {
// add code here for adding the picture...
// That's an exercise for yourself, or another question ;)
}
}
Then in your OptionsPane class:
....
private MainFrame mainFrame; // add a new instance var
public OptionsPane(MainFrame mainFrame) {
this.mainFrame = mainFrame;
// ... rest of the code same as before
}
#Override
public void actionPerformed(ActionEvent e) {
//...
picture = filehandler.getFilePath().replaceFirst("txt", "gif");
picLabel = new JLabel(new ImageIcon(picture));
mainFrame.setPicture(picLabel); // <-- This is where you communicate with the mainFrame instance
//...
}
EDIT
Although my original answer provided a valid and correct solution, it's clear that the OP requires a fully working example, including the code to load display the resulting image. Here's a sample program.
public class OptionsPane extends JComponent implements ActionListener {
private JButton buttonOne = new JButton("Alt.1");
private JButton buttonTwo = new JButton("Alt.2");
private JButton buttonThree = new JButton("Alt.3");
private int option;
private JButton buttonChoose = new JButton("Choose file");
private String picture;
private JLabel picLabel;
private MainFrame mainFrame;
public OptionsPane(MainFrame mainFrame) {
this.mainFrame = mainFrame;
JLabel label = new JLabel("Choose optimization method", SwingConstants.CENTER);
JPanel subPanel = new JPanel();
label.setForeground(Color.CYAN);
label.setFont(new Font("Tahoma", Font.BOLD, 15));
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(label);
buttonOne.addActionListener(this);
buttonTwo.addActionListener(this);
buttonThree.addActionListener(this);
buttonChoose.addActionListener(this);
subPanel.setBackground(Color.DARK_GRAY);
subPanel.add(buttonOne);
subPanel.add(buttonTwo);
subPanel.add(buttonThree);
subPanel.add(buttonChoose);
this.add(subPanel);
}
#Override
public void actionPerformed(ActionEvent e) {
// For sake of simplicity I'm ignoring the original button logic here
// and focussing on just getting an icon loaded in the parent frame...
ImageIcon icon = new ImageIcon("/path/to/test/image.png");
// Just pass the icon itself rather than a new label.
mainFrame.setPicture(icon);
}
}
public class MainFrame {
// No need to extend JFrame if you're using a JFrame instance variable
private JFrame frame = new JFrame();
private JLabel picLabel;
private JPanel mainPanel;
public MainFrame() {
mainPanel = new JPanel(new BorderLayout());
mainPanel.setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1300, 800));
OptionsPane optionspane = new OptionsPane(this);
mainPanel.add(optionspane, BorderLayout.NORTH);
picLabel = new JLabel();
picLabel.setHorizontalAlignment(JLabel.CENTER);
mainPanel.add(picLabel, BorderLayout.CENTER);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
}
public void setPicture(ImageIcon icon) {
picLabel.setIcon(icon);
}
public static void main(String[] args) {
new MainFrame();
}
}
Note I've done a couple of things differently. Personally I always create a JPanel and set that up as the primary layer rather and add that directly to the frame rather than messing with the rootPane. And I used the BorderLayout in this example as it's much simpler.
The other thing is to add the JLabel which is to hold the picture to the GUI in the initial set up. Then you'll see I'm only changing its icon in the setPicture() method rather than adding a new JLabel on each instance.

vlcj video not showing, but video sound is heard

when the application is run, the sound is heard and other button controls are visible on the frame, but the panel containing the video does not show any black screen neither is it displayed on the frame.
public class MediaPlayer extends JPanel {
private JFrame ourframe = new JFrame();
//Declares our media player component
private EmbeddedMediaPlayerComponent mediaplayer;
//This string holds the media URL path
private String mediapath = "";
//This string holds the vlc URL path
private final String vlcpath = "C:\\Program Files (x86)\\VideoLAN\\VLC";
private JPanel panel, panel2;
private JButton play_btn, stop_btn, foward_btn, rewind_btn, enlarge_btn;
private JSlider timeline;
public MediaPlayer(String mediapath) {
this.mediapath = mediapath;
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcpath);
mediaplayer = new EmbeddedMediaPlayerComponent();
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(mediaplayer, BorderLayout.CENTER); //panel containing the video
play_btn = new JButton("play");
stop_btn = new JButton("stop");
foward_btn = new JButton("ff");
rewind_btn = new JButton("rew");
enlarge_btn = new JButton("enlarge");
timeline = new JSlider(0, 100, 0);
timeline.setMajorTickSpacing(10);
timeline.setMajorTickSpacing(5);
timeline.setPaintTicks(true);
panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
panel2.add(play_btn);
panel2.add(stop_btn);
panel2.add(foward_btn);
panel2.add(rewind_btn);
panel2.add(enlarge_btn);
panel2.add(timeline);
panel.add(panel2, BorderLayout.SOUTH);
add(panel);
}
public void play() {
mediaplayer.getMediaPlayer().playMedia(mediapath);
}
public static void main(String[] args) {
//Declare and initialize local variables
String mediaPath = "";
mediaPath = "C:\\Users\\goldAnthony\\Desktop\\Videos\\Whistle.mp4";
//creates instances of the VlcPlayer object, pass the mediaPath and invokes the method "run"
MediaPlayer mediaplayer = new MediaPlayer(mediaPath);
JFrame ourframe = new JFrame();
ourframe.setContentPane(mediaplayer);
ourframe.setSize(720, 560);
ourframe.setVisible(true);
mediaplayer.play();
ourframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
This is because you are adding the video surface to a JPanel without choosing an appropriate layout manager.
By default JPanel has a FlowLayout and this will layout components according to their preferred size.
The video surface panel has no preferred size set.
The solution is to set a layout manager like BorderLayout on the main panel.
So at the end of your constructor, instead of this...
add(panel);
...do this...
setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
Cross-posted at [1].
[1] https://groups.google.com/forum/#!topic/vlcj-users/m0zG-fx4qm8

Categories