fixed some functionallity added back some which was removed cause of refactoring ...

some new refactoring to make the UI code cleaner

Signed-off-by: rattatwinko <seppmutterman@gmail.com>
This commit is contained in:
2026-01-19 13:04:40 +01:00
parent f6ee3e915e
commit 565a4f3cf3
10 changed files with 393 additions and 373 deletions

View File

@@ -1,29 +1,15 @@
package io.swtc;
import com.github.sarxos.webcam.Webcam;
import io.swtc.proccessing.WebcamCaptureLoop;
import io.swtc.proccessing.CameraPanel;
import javax.imageio.ImageIO;
import io.swtc.proccessing.ui.iframe.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.geom.CubicCurve2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
public class SwingIFrame {
private final JFrame mainFrame;
private final BlenderDesktopPane desktopPane;
private boolean fullscreen = false;
private Rectangle windowedBounds;
private boolean blackbg = false;
private final Color defDesktopBg;
private final Color bgcolor;
private final DesktopPane desktopPane;
private final Map<JInternalFrame, EffectsPanelFrame> cameraToEffects = new HashMap<>();
public SwingIFrame() {
@@ -31,175 +17,48 @@ public class SwingIFrame {
mainFrame.setSize(1280, 720);
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
bgcolor = Color.decode("#1e1e1e");
desktopPane = new BlenderDesktopPane(cameraToEffects);
desktopPane = new DesktopPane(cameraToEffects);
desktopPane.setBackground(Color.WHITE);
defDesktopBg = desktopPane.getBackground();
mainFrame.add(desktopPane, BorderLayout.CENTER);
setupFullscreenToggle();
setupBlackBg();
}
public void addCameraInternalFrame(Webcam webcam) {
JInternalFrame cameraFrame = new JInternalFrame(
webcam.getName(),
true, true, true, true
CameraInternalFrame cameraFrame = new CameraInternalFrame(webcam, this::handleEffectsRequest);
EffectsPanelFrame effectsFrame = new EffectsPanelFrame(
"Effects - " + webcam.getName(),
cameraFrame.getCameraPanel()
);
CameraPanel cameraPanel = new CameraPanel();
WebcamCaptureLoop captureLoop = new WebcamCaptureLoop(webcam, (BufferedImage img) ->
SwingUtilities.invokeLater(() -> cameraPanel.setImage(img))
);
cameraToEffects.put(cameraFrame, effectsFrame);
JPanel contentPanel = new JPanel(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("View", cameraPanel);
tabbedPane.addTab("Capture", createCapturePanel(cameraPanel, webcam));
tabbedPane.addTab("Effects", new JPanel());
contentPanel.add(tabbedPane, BorderLayout.CENTER);
tabbedPane.setPreferredSize(null);
// where we show the effectpanel (io.swtc.proccessing.FilterPanel.java)
tabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (tabbedPane.getSelectedIndex() == 2) {
tabbedPane.setSelectedIndex(0);
EffectsPanelFrame effectsFrame = cameraToEffects.get(cameraFrame);
if (effectsFrame != null) {
effectsFrame.setVisible(true);
try {
effectsFrame.setSelected(true);
} catch (java.beans.PropertyVetoException ex) {
ex.printStackTrace();
}
}
}
}
});
int offset = desktopPane.getAllFrames().length * 15;
cameraFrame.setLocation(50 + offset, 50 + offset);
effectsFrame.setLocation(700 + offset, 50 + offset);
effectsFrame.setVisible(false);
cameraFrame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() {
@Override
public void internalFrameClosing(javax.swing.event.InternalFrameEvent e) {
captureLoop.stop();
EffectsPanelFrame effectsFrame = cameraToEffects.get(cameraFrame);
if (effectsFrame != null) {
effectsFrame.dispose();
cameraToEffects.remove(cameraFrame);
}
EffectsPanelFrame ef = cameraToEffects.remove(cameraFrame);
if (ef != null) ef.dispose();
desktopPane.forgetFrame(cameraFrame);
}
});
cameraFrame.add(contentPanel);
cameraFrame.setSize(600, 500);
int offset = desktopPane.getAllFrames().length * 30;
cameraFrame.setLocation(50 + offset, 50 + offset);
// Pre-create the effects frame but keep it hidden
EffectsPanelFrame effectsFrame = new EffectsPanelFrame(
"Effects - " + webcam.getName(),
cameraPanel,
cameraFrame
);
effectsFrame.setSize(350, 600);
effectsFrame.setLocation(700 + offset, 50 + offset);
effectsFrame.setVisible(false); // Hidden by default
cameraToEffects.put(cameraFrame, effectsFrame);
addLinkageListeners(cameraFrame, effectsFrame);
desktopPane.add(cameraFrame);
desktopPane.add(effectsFrame);
cameraFrame.setVisible(true);
captureLoop.start();
}
private void addLinkageListeners(JInternalFrame cameraFrame, EffectsPanelFrame effectsFrame) {
java.awt.event.ComponentAdapter linker = new java.awt.event.ComponentAdapter() {
@Override
public void componentMoved(java.awt.event.ComponentEvent e) {
desktopPane.repaint();
}
@Override
public void componentResized(java.awt.event.ComponentEvent e) {
desktopPane.repaint();
}
};
cameraFrame.addComponentListener(linker);
effectsFrame.addComponentListener(linker);
}
// --- Standard UI Setup Methods ---
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(); }
});
}
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) {
desktopPane.setBackground(bgcolor);
} else {
desktopPane.setBackground(defDesktopBg);
}
blackbg = !blackbg;
}
private void toggleFullscreen() {
if (!fullscreen) {
windowedBounds = mainFrame.getBounds();
mainFrame.dispose();
mainFrame.setUndecorated(true);
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainFrame.setVisible(true);
} else {
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));
JButton takeScreenshot = new JButton("Take Screenshot");
takeScreenshot.addActionListener(e -> saveSnapshot(cameraPanel, webcam, System.getProperty("user.home"), panel));
panel.add(takeScreenshot, BorderLayout.NORTH);
return panel;
}
private void saveSnapshot(CameraPanel cameraPanel, Webcam webcam, String directory, Component parent) {
BufferedImage img = cameraPanel.getCurrentProcessedImage();
if (img != null) {
private void handleEffectsRequest(CameraInternalFrame source) {
EffectsPanelFrame effectsFrame = cameraToEffects.get(source);
if (effectsFrame != null) {
effectsFrame.setVisible(true);
try {
File file = new File(directory, "snap.png");
ImageIO.write(img, "PNG", file);
} catch (Exception ex) {
ex.printStackTrace();
}
effectsFrame.setSelected(true);
effectsFrame.toFront();
} catch (java.beans.PropertyVetoException ex) { JOptionPane.showMessageDialog(null,"Exception" + ex.getMessage() , "Exception", JOptionPane.ERROR_MESSAGE); }
}
}
@@ -207,100 +66,4 @@ public class SwingIFrame {
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
SwingIFrame dashboard = new SwingIFrame();
// Example usage: dashboard.addCameraInternalFrame(Webcam.getDefault());
dashboard.show();
});
}
// --- Inner Classes for Blender Styling ---
static class BlenderDesktopPane extends JDesktopPane {
private final Map<JInternalFrame, EffectsPanelFrame> connections;
public BlenderDesktopPane(Map<JInternalFrame, EffectsPanelFrame> connections) {
this.connections = connections;
}
@Override
protected void paintChildren(Graphics g) {
super.paintChildren(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (Map.Entry<JInternalFrame, EffectsPanelFrame> entry : connections.entrySet()) {
JInternalFrame camera = entry.getKey();
EffectsPanelFrame effects = entry.getValue();
if (camera.isVisible() && effects.isVisible() && !camera.isIcon() && !effects.isIcon()) {
drawBezierConnection(g2d, camera, effects);
}
}
g2d.dispose();
}
private void drawBezierConnection(Graphics2D g2d, JInternalFrame from, JInternalFrame to) {
Rectangle f = from.getBounds();
Rectangle t = to.getBounds();
int x1 = f.x + f.width;
int y1 = f.y + f.height / 2;
int x2 = t.x;
int y2 = t.y + t.height / 2;
int ctrlOffset = Math.min(Math.abs(x2 - x1) / 2, 150);
CubicCurve2D curve = new CubicCurve2D.Double(x1, y1, x1 + ctrlOffset, y1, x2 - ctrlOffset, y2, x2, y2);
g2d.setStroke(new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.setColor(new Color(100, 200, 255, 200));
g2d.draw(curve);
g2d.fillOval(x1 - 5, y1 - 5, 10, 10);
g2d.fillOval(x2 - 5, y2 - 5, 10, 10);
}
}
static class EffectsPanelFrame extends JInternalFrame {
public EffectsPanelFrame(String title, CameraPanel cameraPanel, JInternalFrame cameraFrame) {
super(title, true, true, true, true);
// Hide instead of dispose so it can be reopened
setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
// Add your FilterPanel
add(new io.swtc.proccessing.FilterPanel(cameraPanel));
// Repaint desktop pane when this frame closes so the Bézier disappears
addInternalFrameListener(new InternalFrameAdapter() {
@Override
public void internalFrameClosing(InternalFrameEvent e) {
if (getDesktopPane() != null) {
getDesktopPane().repaint();
}
}
});
// Repaint curves when moving or resizing
addComponentListener(new java.awt.event.ComponentAdapter() {
@Override
public void componentMoved(java.awt.event.ComponentEvent e) {
if (getDesktopPane() != null)
getDesktopPane().repaint();
}
@Override
public void componentResized(java.awt.event.ComponentEvent e) {
if (getDesktopPane() != null)
getDesktopPane().repaint();
}
});
}
}
}