D
djbitchpimp
I am making a simple media player for the Java Media Framework.
Basically I want it to be able to open a video from a url and play it
in a swing component.
The problem I am having is that when I start my player, the JPanel that
I want to redraw with the player window will not refresh itself to show
the video screen. Right now it is displaying the control panel
component but not the video component.
I am not very familiar with swing, so could someone please point out
any obvious errors in my code where this might be happening? Thanks!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;
public class MediaPlayerGui implements ActionListener{
protected JFrame clientWin ; // window frame
protected Component playerWin ; // the visual component of the player
protected Component controls ; // player controls
final JFileChooser open = new JFileChooser () ;
final JButton stopButton = new JButton ("Stop"); // stop button
final JButton openButton = new JButton ("Open File"); // open button
final JButton openURLButton = new JButton ("Open URL"); // openURL
button
final JTextField URLlocation = new JTextField (22) ; // URL text area
final JLabel URLLabel = new JLabel ("URL:") ;
final JLabel loopStartLab = new JLabel ("Loop Start:") ;
final JLabel loopEndLab = new JLabel ("Loop End:") ;
final JTextField loopStart = new JTextField (7);
final JTextField loopEnd = new JTextField (7);
final JPanel pane = new JPanel(new BorderLayout ());
final JCheckBox loopCheck = new JCheckBox ("Auto Restart") ;
final JCheckBox autoCheck = new JCheckBox ("Loop") ;
final JPanel windowPanel = new JPanel () ;
Player player ;
String dataLocation = new String () ; // a string to hold the data
location
MediaLocator dataSource ; // the datasource to be played
GridBagConstraints c = new GridBagConstraints();
//DataSource mediaSource = new DataSource () ;
public MediaPlayerGui () {
Manager man ;
dataSource = new MediaLocator (dataLocation) ;
// get the preferred size of the media source
player = null ;
windowPanel.setLayout(new GridLayout (0,1)) ;
windowPanel.setBorder(BorderFactory.createEmptyBorder(
0, //top
0, //left
240, //bottom
320) //right
);
windowPanel.setBackground(Color.black) ;
}
public Component createComponents() {
stopButton.addActionListener(this);
openButton.addActionListener(this);
openURLButton.addActionListener(this);
pane.setLayout (new GridBagLayout ());
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.LINE_START ;
pane.add(openButton, c);
c.gridx = 1;
pane.add(stopButton, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 4 ;
pane.add(windowPanel, c);
c.gridx = 0;
c.gridy = 2;
pane.add(URLLabel, c);
c.gridx = 1;
c.gridy = 2;
pane.add(URLlocation, c);
c.gridx = 0;
c.gridy = 3;
pane.add(openURLButton, c);
c.gridx = 0;
c.gridy = 4;
pane.add(loopCheck, c);
c.gridx = 0;
c.gridy = 5;
pane.add(autoCheck, c);
c.gridx = 0;
c.gridy = 6;
pane.add(loopStartLab, c);
c.gridx = 1;
c.gridy = 6;
pane.add(loopStart, c);
c.gridx = 0;
c.gridy = 7;
pane.add(loopEndLab, c);
c.gridx = 1;
c.gridy = 7;
pane.add(loopEnd, c);
return pane;
}
public static void main(String[] args) {
JFrame clientWin = new JFrame ("Media Player") ;
MediaPlayerGui window = new MediaPlayerGui () ;
Component contents = window.createComponents() ;
clientWin.getContentPane().add(contents);
clientWin.pack();
clientWin.setVisible(true);
clientWin.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed (ActionEvent e) {
if (e.getSource() == openButton) {
int returnVal = open.showOpenDialog(pane);
}
else if (e.getSource() == stopButton) {
player.stop () ;
}
else if (e.getSource() == openURLButton) {
// get the text from the textbox
String URL = URLlocation.getText() ;
// make a medialocator with that URL
dataSource = new MediaLocator (URL) ;
//System.out.println(URL) ;
try {
player = Manager.createRealizedPlayer(dataSource) ;
} catch (Exception ex) {
ex.printStackTrace() ;
}
playerWin = player.getVisualComponent() ;
controls = player.getControlPanelComponent() ;
windowPanel.add (playerWin) ;
windowPanel.add (controls) ;
player.start () ;
windowPanel.repaint();
}
}
}
Basically I want it to be able to open a video from a url and play it
in a swing component.
The problem I am having is that when I start my player, the JPanel that
I want to redraw with the player window will not refresh itself to show
the video screen. Right now it is displaying the control panel
component but not the video component.
I am not very familiar with swing, so could someone please point out
any obvious errors in my code where this might be happening? Thanks!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;
public class MediaPlayerGui implements ActionListener{
protected JFrame clientWin ; // window frame
protected Component playerWin ; // the visual component of the player
protected Component controls ; // player controls
final JFileChooser open = new JFileChooser () ;
final JButton stopButton = new JButton ("Stop"); // stop button
final JButton openButton = new JButton ("Open File"); // open button
final JButton openURLButton = new JButton ("Open URL"); // openURL
button
final JTextField URLlocation = new JTextField (22) ; // URL text area
final JLabel URLLabel = new JLabel ("URL:") ;
final JLabel loopStartLab = new JLabel ("Loop Start:") ;
final JLabel loopEndLab = new JLabel ("Loop End:") ;
final JTextField loopStart = new JTextField (7);
final JTextField loopEnd = new JTextField (7);
final JPanel pane = new JPanel(new BorderLayout ());
final JCheckBox loopCheck = new JCheckBox ("Auto Restart") ;
final JCheckBox autoCheck = new JCheckBox ("Loop") ;
final JPanel windowPanel = new JPanel () ;
Player player ;
String dataLocation = new String () ; // a string to hold the data
location
MediaLocator dataSource ; // the datasource to be played
GridBagConstraints c = new GridBagConstraints();
//DataSource mediaSource = new DataSource () ;
public MediaPlayerGui () {
Manager man ;
dataSource = new MediaLocator (dataLocation) ;
// get the preferred size of the media source
player = null ;
windowPanel.setLayout(new GridLayout (0,1)) ;
windowPanel.setBorder(BorderFactory.createEmptyBorder(
0, //top
0, //left
240, //bottom
320) //right
);
windowPanel.setBackground(Color.black) ;
}
public Component createComponents() {
stopButton.addActionListener(this);
openButton.addActionListener(this);
openURLButton.addActionListener(this);
pane.setLayout (new GridBagLayout ());
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.LINE_START ;
pane.add(openButton, c);
c.gridx = 1;
pane.add(stopButton, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 4 ;
pane.add(windowPanel, c);
c.gridx = 0;
c.gridy = 2;
pane.add(URLLabel, c);
c.gridx = 1;
c.gridy = 2;
pane.add(URLlocation, c);
c.gridx = 0;
c.gridy = 3;
pane.add(openURLButton, c);
c.gridx = 0;
c.gridy = 4;
pane.add(loopCheck, c);
c.gridx = 0;
c.gridy = 5;
pane.add(autoCheck, c);
c.gridx = 0;
c.gridy = 6;
pane.add(loopStartLab, c);
c.gridx = 1;
c.gridy = 6;
pane.add(loopStart, c);
c.gridx = 0;
c.gridy = 7;
pane.add(loopEndLab, c);
c.gridx = 1;
c.gridy = 7;
pane.add(loopEnd, c);
return pane;
}
public static void main(String[] args) {
JFrame clientWin = new JFrame ("Media Player") ;
MediaPlayerGui window = new MediaPlayerGui () ;
Component contents = window.createComponents() ;
clientWin.getContentPane().add(contents);
clientWin.pack();
clientWin.setVisible(true);
clientWin.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed (ActionEvent e) {
if (e.getSource() == openButton) {
int returnVal = open.showOpenDialog(pane);
}
else if (e.getSource() == stopButton) {
player.stop () ;
}
else if (e.getSource() == openURLButton) {
// get the text from the textbox
String URL = URLlocation.getText() ;
// make a medialocator with that URL
dataSource = new MediaLocator (URL) ;
//System.out.println(URL) ;
try {
player = Manager.createRealizedPlayer(dataSource) ;
} catch (Exception ex) {
ex.printStackTrace() ;
}
playerWin = player.getVisualComponent() ;
controls = player.getControlPanelComponent() ;
windowPanel.add (playerWin) ;
windowPanel.add (controls) ;
player.start () ;
windowPanel.repaint();
}
}
}