WebcamPanel extends JPanel implements WebcamListener, PropertyChangeListener { public void stop() { if (!started.compareAndSet(true, false)) { return; } webcam.removeWebcamListener(this); LOG.debug("Stopping panel rendering and closing attached webcam"); try { updater.stop(); } catch (InterruptedException e) { throw new RuntimeException(e); } image = null; final SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { @Override protected Void doInBackground() throws Exception { try { if (webcam.isOpen()) { errored = !webcam.close(); } } catch (WebcamException e) { errored = true; throw e; } finally { repaintPanel(); } return null; } }; worker.execute(); } WebcamPanel(Webcam webcam);  WebcamPanel(Webcam webcam, boolean start);  WebcamPanel(Webcam webcam, Dimension size, boolean start);  WebcamPanel(Webcam webcam, Dimension size, boolean start, ImageSupplier supplier); void setPainter(Painter painter); Painter getPainter(); void start(); void stop(); void pause(); void resume(); boolean isFPSLimited(); void setFPSLimited(boolean frequencyLimit); double getFPSLimit(); void setFPSLimit(double fps); boolean isDisplayDebugInfo(); void setDisplayDebugInfo(boolean displayDebugInfo); boolean isFPSDisplayed(); void setFPSDisplayed(boolean displayed); boolean isImageSizeDisplayed(); void setImageSizeDisplayed(boolean imageSizeDisplayed); void setAntialiasingEnabled(boolean antialiasing); boolean isAntialiasingEnabled(); boolean isStarting(); boolean isStarted(); DrawMode getDrawMode(); void setDrawMode(DrawMode drawMode); boolean isErrored(); @Deprecated Map<RenderingHints.Key, Object> getImageRenderingHints(); @Deprecated boolean isFitArea(); @Deprecated void setFitArea(boolean fitArea); @Deprecated void setFillArea(boolean fillArea); @Deprecated boolean isFillArea(); Painter getDefaultPainter(); @Override void propertyChange(PropertyChangeEvent evt); @Override void webcamOpen(WebcamEvent we); @Override void webcamClosed(WebcamEvent we); @Override void webcamDisposed(WebcamEvent we); @Override void webcamImageObtained(WebcamEvent we); boolean isMirrored(); void setMirrored(boolean mirrored); Webcam getWebcam(); BufferedImage getImage(); static final double MIN_FREQUENCY; static final Map<RenderingHints.Key, Object> DEFAULT_IMAGE_RENDERING_HINTS; }
@Test public void test_sizeSpecified() throws InterruptedException { Webcam.setDriver(new DummyDriver()); final Webcam w = Webcam.getDefault(); final WebcamPanel p = new WebcamPanel(w, new Dimension(256, 345), false); w.open(); p.repaint(); final Dimension d = p.getPreferredSize(); Assertions .assertThat(d.getWidth()) .isEqualTo(256); Assertions .assertThat(d.getHeight()) .isEqualTo(345); p.stop(); w.close(); }