Last active
February 5, 2021 20:28
-
-
Save lppedd/1f23af07ba8eb5184512ad0ff5b577e8 to your computer and use it in GitHub Desktop.
JPanel layouts. See comment below for a preview.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.lang.reflect.InvocationTargetException; | |
import java.net.URL; | |
@SuppressWarnings("ALL") | |
public class HelloApp { | |
private final JFrame frame; | |
private final JLabel videoLabel; | |
public static void main(final String[] args) { | |
EventQueue.invokeLater(() -> { | |
final HelloApp window = new HelloApp(); | |
}); | |
} | |
public HelloApp() { | |
frame = new JFrame("MULTIPLE-TARGET TRACKING"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
final int width = 800; | |
final int height = 500; | |
frame.setBounds(0, 0, width, height); | |
frame.setLayout(new BorderLayout()); | |
// Show the frame at the center of the screen | |
frame.setLocation( | |
(int) (0.5 * Toolkit.getDefaultToolkit().getScreenSize().width - width / 2), | |
(int) (0.5 * Toolkit.getDefaultToolkit().getScreenSize().height - height / 2) | |
); | |
frame.setVisible(true); | |
// Main panel, direct child of the JFrame | |
final JPanel panel = new JPanel(new BorderLayout()); | |
frame.getContentPane().add(panel, BorderLayout.CENTER); | |
// Upper section of the UI | |
final JButton startButton = new JButton("Start / Replay"); | |
startButton.addActionListener(event -> { | |
// Frame processing should run in a separate (non-EDT) thread to avoid freezing the UI. | |
// To spawn a thread you can use a bare Thread, or look at ExecutorService. | |
// | |
// Also remember not to press the button more than once, or multiple threads | |
// will be updating the shown image | |
final Thread thread = new Thread(() -> { | |
try { | |
playVideo(); | |
} catch (final InterruptedException | InvocationTargetException ex) { | |
ex.printStackTrace(); | |
} | |
}); | |
thread.start(); | |
} | |
); | |
final Box upperLeftBox = Box.createHorizontalBox(); | |
upperLeftBox.add(startButton); | |
upperLeftBox.add(Box.createHorizontalStrut(10)); | |
upperLeftBox.add(new JButton("Close")); | |
final Box upperRightBox = Box.createHorizontalBox(); | |
upperRightBox.add(new JButton("Capture camera")); | |
final JPanel upperPanel = new JPanel(new BorderLayout()); | |
upperPanel.add(upperLeftBox, BorderLayout.LINE_START); | |
upperPanel.add(upperRightBox, BorderLayout.LINE_END); | |
upperPanel.setBorder( | |
BorderFactory.createCompoundBorder( | |
BorderFactory.createEmptyBorder(10, 10, 10, 10), | |
BorderFactory.createLineBorder(Color.BLACK, 2) | |
) | |
); | |
// Middle section of the UI | |
videoLabel = new JLabel(); | |
videoLabel.setBorder(BorderFactory.createLineBorder(Color.RED)); | |
final JPanel middlePanel = new JPanel(new BorderLayout()); | |
middlePanel.add(videoLabel, BorderLayout.CENTER); | |
final JPanel settingsPanel = new JPanel(new GridBagLayout()); | |
settingsPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN)); | |
final GridBagConstraints gc = new GridBagConstraints(); | |
gc.fill = GridBagConstraints.HORIZONTAL; | |
gc.anchor = GridBagConstraints.NORTH; | |
gc.insets = new Insets(3, 5, 3, 5); | |
settingsPanel.add(new JLabel("Learning rate"), gc); | |
gc.gridx = 1; | |
gc.weightx = 1.0; | |
settingsPanel.add(new JTextField(10), gc); | |
gc.weightx = 0.0; | |
gc.gridx = 0; | |
gc.gridy = 1; | |
settingsPanel.add(new JLabel("Min blob"), gc); | |
gc.weightx = 1.0; | |
gc.gridx = 1; | |
settingsPanel.add(new JTextField(10), gc); | |
gc.weighty = 1.0; | |
gc.gridx = 0; | |
gc.gridy = 2; | |
gc.gridwidth = 2; | |
settingsPanel.add(new JButton("Submit"), gc); | |
final Box middleRightBox = Box.createVerticalBox(); | |
middleRightBox.add(settingsPanel); | |
middlePanel.add(middleRightBox, BorderLayout.LINE_END); | |
middlePanel.setBorder( | |
BorderFactory.createCompoundBorder( | |
BorderFactory.createEmptyBorder(0, 10, 0, 10), | |
BorderFactory.createLineBorder(Color.BLACK, 2) | |
) | |
); | |
// Bottom section of the UI | |
final JPanel bottomPanel = new JPanel(new BorderLayout()); | |
bottomPanel.add(new JButton("Open file..."), BorderLayout.LINE_START); | |
bottomPanel.setBorder( | |
BorderFactory.createCompoundBorder( | |
BorderFactory.createEmptyBorder(10, 10, 10, 10), | |
BorderFactory.createLineBorder(Color.BLACK, 2) | |
) | |
); | |
panel.add(upperPanel, BorderLayout.PAGE_START); | |
panel.add(middlePanel, BorderLayout.CENTER); | |
panel.add(bottomPanel, BorderLayout.PAGE_END); | |
} | |
private void playVideo() throws InterruptedException, InvocationTargetException { | |
// Loop to showcase how the images change inside the JLabel | |
frame.setResizable(false); | |
while (true) { | |
for (int i = 1; i < 6; i++) { | |
// Here we process the frame. | |
// Replace this with whatever you need to do | |
final URL resource = getClass().getResource("/img" + i + ".jpg"); | |
// Update the shown image using the EDT thread, otherwise an exception will be thrown | |
EventQueue.invokeAndWait(() -> { | |
final Icon image = new ImageIcon(resource); | |
videoLabel.setIcon(image); | |
frame.pack(); | |
}); | |
// Simulate 20 FPS | |
Thread.sleep(50); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: