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:
2026-01-14 20:55:45 +01:00
parent ccc3d264f7
commit e7a3d98dd0
2 changed files with 115 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
@@ -24,14 +25,28 @@ import java.util.Date;
public class SwingIFrame {
private final JFrame mainFrame;
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() {
mainFrame = new JFrame("viewer");
mainFrame.setSize(1280,720);
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.setBackground(Color.WHITE);
defDesktopBg = desktopPane.getBackground();
mainFrame.add(desktopPane, BorderLayout.CENTER);
setupFullscreenToggle();
setupBlackBg();
}
public void addCameraInternalFrame(Webcam webcam) {
@@ -73,6 +88,79 @@ public class SwingIFrame {
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) {
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.setBorder(new EmptyBorder(15, 15, 15, 15));