|
| 1 | +import java.awt.Dimension; |
| 2 | +import java.awt.Rectangle; |
| 3 | +import java.awt.event.ActionEvent; |
| 4 | + |
| 5 | +import javax.swing.AbstractAction; |
| 6 | +import javax.swing.JButton; |
| 7 | +import javax.swing.JFrame; |
| 8 | + |
| 9 | +import com.github.sarxos.webcam.Webcam; |
| 10 | +import com.github.sarxos.webcam.WebcamPanel; |
| 11 | +import com.github.sarxos.webcam.WebcamResolution; |
| 12 | + |
| 13 | + |
| 14 | +@SuppressWarnings("serial") |
| 15 | +public class WebcamPanelWithSubcomponentsExample { |
| 16 | + |
| 17 | + public static void main(String[] args) throws InterruptedException { |
| 18 | + |
| 19 | + final Dimension size = WebcamResolution.VGA.getSize(); |
| 20 | + |
| 21 | + final Webcam webcam = Webcam.getDefault(); |
| 22 | + webcam.setViewSize(size); |
| 23 | + |
| 24 | + final WebcamPanel panel = new WebcamPanel(webcam, false); |
| 25 | + panel.setFPSDisplayed(true); |
| 26 | + panel.setDisplayDebugInfo(true); |
| 27 | + panel.setImageSizeDisplayed(true); |
| 28 | + panel.setMirrored(true); |
| 29 | + |
| 30 | + final String play = "PLAY"; |
| 31 | + final String stop = "STOP"; |
| 32 | + |
| 33 | + final JButton button = new JButton(); |
| 34 | + button.setAction(new AbstractAction(play) { |
| 35 | + |
| 36 | + @Override |
| 37 | + public void actionPerformed(ActionEvent e) { |
| 38 | + if (panel.isStarted()) { |
| 39 | + panel.stop(); |
| 40 | + button.setText(play); |
| 41 | + } else { |
| 42 | + panel.start(); |
| 43 | + button.setText(stop); |
| 44 | + } |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + button.setBounds(getButtonBounds(size)); |
| 49 | + |
| 50 | + panel.setLayout(null); |
| 51 | + panel.add(button); |
| 52 | + |
| 53 | + final JFrame window = new JFrame("Test webcam panel"); |
| 54 | + window.add(panel); |
| 55 | + window.setResizable(true); |
| 56 | + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 57 | + window.pack(); |
| 58 | + window.setVisible(true); |
| 59 | + } |
| 60 | + |
| 61 | + private static Rectangle getButtonBounds(Dimension size) { |
| 62 | + final int x = (int) (size.width * 0.1); |
| 63 | + final int y = (int) (size.height * 0.8); |
| 64 | + final int w = (int) (size.width * 0.8); |
| 65 | + final int h = (int) (size.height * 0.1); |
| 66 | + return new Rectangle(x, y, w, h); |
| 67 | + } |
| 68 | +} |
0 commit comments