Quality of Life changes to the viewer
changed: / SwingIFrame ; Some new methodes for setting bg color, and fullscreen, keybinds are "F11" and "B" / WebcamCaptureLoop.java ; Safety is key! --- rattatwinko
This commit is contained in:
@@ -10,6 +10,7 @@ import javax.swing.*;
|
|||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
import javax.swing.border.TitledBorder;
|
import javax.swing.border.TitledBorder;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
@@ -24,14 +25,28 @@ import java.util.Date;
|
|||||||
public class SwingIFrame {
|
public class SwingIFrame {
|
||||||
private final JFrame mainFrame;
|
private final JFrame mainFrame;
|
||||||
private final JDesktopPane desktopPane;
|
private final JDesktopPane desktopPane;
|
||||||
|
private boolean fullscreen = false;
|
||||||
|
private Rectangle windowedBounds;
|
||||||
|
private boolean blackbg = false;
|
||||||
|
private final Color defDesktopBg;
|
||||||
|
private final Color bgcolor;
|
||||||
|
|
||||||
public SwingIFrame() {
|
public SwingIFrame() {
|
||||||
mainFrame = new JFrame("viewer");
|
mainFrame = new JFrame("viewer");
|
||||||
mainFrame.setSize(1280,720);
|
mainFrame.setSize(1280,720);
|
||||||
mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
|
mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
|
||||||
|
|
||||||
|
// this is good on the eyes for long periods of times,
|
||||||
|
// i would have used #4B9EA0, which is also easy on the eyes
|
||||||
|
bgcolor = Color.decode("#1e1e1e");
|
||||||
|
|
||||||
desktopPane = new JDesktopPane();
|
desktopPane = new JDesktopPane();
|
||||||
desktopPane.setBackground(Color.WHITE);
|
desktopPane.setBackground(Color.WHITE);
|
||||||
|
defDesktopBg = desktopPane.getBackground();
|
||||||
mainFrame.add(desktopPane, BorderLayout.CENTER);
|
mainFrame.add(desktopPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
setupFullscreenToggle();
|
||||||
|
setupBlackBg();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCameraInternalFrame(Webcam webcam) {
|
public void addCameraInternalFrame(Webcam webcam) {
|
||||||
@@ -73,6 +88,79 @@ public class SwingIFrame {
|
|||||||
captureLoop.start();
|
captureLoop.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupBlackBg() {
|
||||||
|
JRootPane root = mainFrame.getRootPane();
|
||||||
|
|
||||||
|
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||||
|
.put(KeyStroke.getKeyStroke("B"), "toggleBlackBg");
|
||||||
|
|
||||||
|
root.getActionMap().put("toggleBlackBg", new AbstractAction() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
setbgblack();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/* Setup F11 for Fullscreen */
|
||||||
|
private void setupFullscreenToggle() {
|
||||||
|
JRootPane root = mainFrame.getRootPane();
|
||||||
|
|
||||||
|
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
||||||
|
.put(KeyStroke.getKeyStroke("F11"), "toggleFullscreen");
|
||||||
|
|
||||||
|
root.getActionMap().put("toggleFullscreen", new AbstractAction() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
toggleFullscreen();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setbgblack() {
|
||||||
|
if (!blackbg) {
|
||||||
|
// easy stuff here, just setting the bg to the private color
|
||||||
|
desktopPane.setBackground(bgcolor);
|
||||||
|
updateInternalFrameBg(bgcolor);
|
||||||
|
} else {
|
||||||
|
desktopPane.setBackground(defDesktopBg);
|
||||||
|
updateInternalFrameBg(null);
|
||||||
|
}
|
||||||
|
blackbg = !blackbg;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateInternalFrameBg(Color bg) {
|
||||||
|
for (JInternalFrame frame : desktopPane.getAllFrames()) {
|
||||||
|
Container content = frame.getContentPane();
|
||||||
|
if (bg != null) {
|
||||||
|
content.setBackground(bg);
|
||||||
|
} else content.setBackground(null); // restore default
|
||||||
|
content.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void toggleFullscreen() {
|
||||||
|
if (!fullscreen) {
|
||||||
|
// We set the window to borderless windowed mode, so it doesnt
|
||||||
|
// lag like shit when we drag windows around, which is annoying
|
||||||
|
windowedBounds = mainFrame.getBounds();
|
||||||
|
mainFrame.dispose();
|
||||||
|
mainFrame.setUndecorated(true);
|
||||||
|
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
|
||||||
|
mainFrame.setVisible(true);
|
||||||
|
} else {
|
||||||
|
// do the opposite
|
||||||
|
mainFrame.dispose();
|
||||||
|
mainFrame.setUndecorated(false);
|
||||||
|
mainFrame.setExtendedState(JFrame.NORMAL);
|
||||||
|
mainFrame.setBounds(windowedBounds);
|
||||||
|
mainFrame.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
fullscreen = !fullscreen;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private JPanel createCapturePanel(CameraPanel cameraPanel, Webcam webcam) {
|
private JPanel createCapturePanel(CameraPanel cameraPanel, Webcam webcam) {
|
||||||
JPanel panel = new JPanel(new BorderLayout(10, 10));
|
JPanel panel = new JPanel(new BorderLayout(10, 10));
|
||||||
panel.setBorder(new EmptyBorder(15, 15, 15, 15));
|
panel.setBorder(new EmptyBorder(15, 15, 15, 15));
|
||||||
|
|||||||
@@ -5,13 +5,15 @@ import com.github.sarxos.webcam.WebcamException;
|
|||||||
|
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.*;
|
||||||
|
|
||||||
public class WebcamCaptureLoop {
|
public class WebcamCaptureLoop {
|
||||||
private final Webcam webcam;
|
private final Webcam webcam;
|
||||||
private final Consumer<BufferedImage> onFrameCaptured;
|
private final Consumer<BufferedImage> onFrameCaptured;
|
||||||
private volatile boolean running = false;
|
private volatile boolean running = false;
|
||||||
|
private final AtomicBoolean cleanedUp = new AtomicBoolean(false);
|
||||||
|
|
||||||
public WebcamCaptureLoop(Webcam webcam, Consumer<BufferedImage> onFrameCaptured) {
|
public WebcamCaptureLoop(Webcam webcam, Consumer<BufferedImage> onFrameCaptured) {
|
||||||
this.webcam = webcam;
|
this.webcam = webcam;
|
||||||
@@ -64,22 +66,40 @@ public class WebcamCaptureLoop {
|
|||||||
captureThread.start();
|
captureThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
// method for cleaning up
|
/**
|
||||||
|
* Safely release webcam
|
||||||
|
*/
|
||||||
private synchronized void cleanup() {
|
private synchronized void cleanup() {
|
||||||
|
if (!cleanedUp.compareAndSet(false, true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean success = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// if the camera is open try to cloes it!
|
if (webcam.isOpen())
|
||||||
webcam.close();
|
webcam.close();
|
||||||
|
|
||||||
|
success = true;
|
||||||
|
|
||||||
} catch (WebcamException exception) {
|
} catch (WebcamException exception) {
|
||||||
|
SwingUtilities.invokeLater(() ->
|
||||||
JOptionPane.showMessageDialog(
|
JOptionPane.showMessageDialog(
|
||||||
null,
|
null,
|
||||||
"Cleanup failed \nWebCamException@"+exception.getMessage(),
|
"Cleanup failed \nWebCamException@"+exception.getMessage(),
|
||||||
"WebCamException",
|
"WebCamException",
|
||||||
JOptionPane.ERROR_MESSAGE
|
JOptionPane.WARNING_MESSAGE // changed to warning, its better tbh
|
||||||
);
|
));
|
||||||
}
|
} finally {
|
||||||
|
if (!success)
|
||||||
|
cleanedUp.set(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Arguments: null (or just none)
|
||||||
|
* What does this method do? : Sets CameraThreads running flag to false and calls cleanup();
|
||||||
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
running = false;
|
running = false;
|
||||||
cleanup();
|
cleanup();
|
||||||
|
|||||||
Reference in New Issue
Block a user