/** Video Panel
*
* @author pquiring
*/
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javaforce.*;
public class VideoPanel extends javax.swing.JPanel {
/**
* Creates new form VideoPanel
*/
public VideoPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
controls = new javax.swing.JPanel();
time = new javax.swing.JSlider();
play = new javax.swing.JButton();
stop = new javax.swing.JButton();
fullScreen = new javax.swing.JButton();
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
formMouseMoved(evt);
}
});
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
formComponentResized(evt);
}
});
controls.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
controlsMouseMoved(evt);
}
});
time.setValue(0);
time.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
timeStateChanged(evt);
}
});
play.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pause.png"))); // NOI18N
play.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
playActionPerformed(evt);
}
});
stop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/stop.png"))); // NOI18N
stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
stopActionPerformed(evt);
}
});
fullScreen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fullscreen.png"))); // NOI18N
fullScreen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fullScreenActionPerformed(evt);
}
});
javax.swing.GroupLayout controlsLayout = new javax.swing.GroupLayout(controls);
controls.setLayout(controlsLayout);
controlsLayout.setHorizontalGroup(
controlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(controlsLayout.createSequentialGroup()
.addContainerGap()
.addComponent(time, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(controlsLayout.createSequentialGroup()
.addGap(128, 128, 128)
.addComponent(stop)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(play)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(fullScreen)
.addContainerGap(129, Short.MAX_VALUE))
);
controlsLayout.setVerticalGroup(
controlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(controlsLayout.createSequentialGroup()
.addContainerGap()
.addComponent(time, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(controlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(play)
.addComponent(stop)
.addComponent(fullScreen))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(controls, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 226, Short.MAX_VALUE)
.addComponent(controls, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
}// </editor-fold>//GEN-END:initComponents
private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
wake();
}//GEN-LAST:event_formMouseMoved
private void stopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopActionPerformed
MediaApp.panel.stop(false);
}//GEN-LAST:event_stopActionPerformed
private void playActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playActionPerformed
MediaApp.panel.playOrPause();
}//GEN-LAST:event_playActionPerformed
private void controlsMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_controlsMouseMoved
wake();
}//GEN-LAST:event_controlsMouseMoved
private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
MediaApp.panel.setVideoSize(getWidth(), getHeight());
}//GEN-LAST:event_formComponentResized
private void timeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_timeStateChanged
MediaApp.panel.seek();
}//GEN-LAST:event_timeStateChanged
private void fullScreenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fullScreenActionPerformed
MediaApp.frame.toggleFullScreen();
}//GEN-LAST:event_fullScreenActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel controls;
private javax.swing.JButton fullScreen;
private javax.swing.JButton play;
private javax.swing.JButton stop;
private javax.swing.JSlider time;
// End of variables declaration//GEN-END:variables
private JFImage img, new_img;
private boolean needPainting = false;
private boolean showControls = true;
private java.util.Timer timer;
private int cnt = 0;
private void wake() {
if (showControls) return;
cnt = 0;
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
showControls = true;
controls.setVisible(true);
repaint();
}
});
}
public void start() {
timer = new java.util.Timer();
timer.schedule(new TimerTask() {
public void run() {
if (!showControls) return;
cnt++;
if (cnt == 5) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
showControls = false;
controls.setVisible(false);
repaint();
}
});
}
}
}, 1000, 1000);
}
public void stop() {
timer.cancel();
timer = null;
if (MediaApp.frame.isFullScreen()) {
MediaApp.frame.toggleFullScreen();
}
}
public void setImage(JFImage src) {
new_img = src;
if (needPainting) {
JFLog.log("Video updating too slow");
return;
}
needPainting = true;
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
img = new_img;
repaint();
}
});
}
public void paintComponent(Graphics g) {
if (!needPainting) {
JFLog.log("VideoPanel:not painting");
return;
}
int w = getWidth();
int h = getHeight();
//paint controls
if (img == null) {
g.fillRect(0, 0, w, h);
} else {
int iw = img.getWidth();
int ih = img.getHeight();
if (((iw != w) || (ih != h))) {
JFLog.log("VideoPanel:image scaled");
JFImage scaled = new JFImage();
scaled.setImageSize(w, h);
scaled.getGraphics().drawImage(img.getImage(), 0,0, w,h, 0,0, iw,ih, null);
img = scaled;
}
g.drawImage(img.getImage(), 0, 0, null);
}
needPainting = false;
}
public JSlider time() {
return time;
}
public JButton play() {
return play;
}
}