Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package CalcDimensions;
- /* To run this file after compiling with net beans use this syntax java -jar "CalulateEightByFour.jar"
- /* Written by MD Harrington London Kent DA6 8NP
- * Program is designed to assist builders ground worker in working out sheets of 8 by 4 foot wood ,
- * and plastic coverings for maintenance
- * Please see other links
- * https://www.facebook.com/mark.harrington.14289
- * https://www.instagram.com/markukh2021
- * https://pastebin.com/u/Mark2020H
- * https://codeshare.io/codes
- * https://github.com/markh2016?tab=repositories
- */
- import javax.swing.*;
- import java.io.FileWriter;
- import java.io.IOException;
- import javax.swing.filechooser.FileNameExtensionFilter;
- public class Calc8by4 {
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
- } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
- }
- // Create the main frame
- JFrame frame = new JFrame("Wall Measurement Calculator");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(600, 700);
- frame.setLocationRelativeTo(null);
- // Create components
- JLabel instructionsLabel = new JLabel("Enter dimensions (e.g., 161cm * 93cm):");
- JTextArea inputTextArea = new JTextArea(10, 40);
- JScrollPane inputScrollPane = new JScrollPane(inputTextArea);
- JButton convertButton = new JButton("Convert to Inches");
- JButton clearButton = new JButton("Clear");
- JTextArea outputTextArea = new JTextArea(10, 40);
- outputTextArea.setEditable(false);
- JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
- JLabel totalSquareInchesLabel = new JLabel("Total Square Area (sq in):");
- JTextField totalSquareInchesField = new JTextField(15);
- totalSquareInchesField.setEditable(false);
- JLabel totalSquareFeetLabel = new JLabel("Total Square Area (sq ft):");
- JTextField totalSquareFeetField = new JTextField(15);
- totalSquareFeetField.setEditable(false);
- JLabel sheetsRequiredLabel = new JLabel("Number of Sheets Required:");
- JTextField sheetsRequiredField = new JTextField(15);
- sheetsRequiredField.setEditable(false);
- // Menu Bar
- JMenuBar menuBar = new JMenuBar();
- // File Menu
- JMenu fileMenu = new JMenu("File");
- JMenuItem saveResultsItem = new JMenuItem("Save Results to File");
- JMenuItem exitItem = new JMenuItem("Exit");
- fileMenu.add(saveResultsItem);
- fileMenu.add(exitItem);
- // About Menu
- JMenu aboutMenu = new JMenu("About");
- JMenuItem aboutItem = new JMenuItem("About");
- aboutMenu.add(aboutItem);
- // Look and Feel Menu
- JMenu lookAndFeelMenu = new JMenu("Set Look and Feel");
- UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
- for (UIManager.LookAndFeelInfo look : looks) {
- JMenuItem lookItem = new JMenuItem(look.getName());
- lookAndFeelMenu.add(lookItem);
- lookItem.addActionListener(e -> {
- try {
- UIManager.setLookAndFeel(look.getClassName());
- SwingUtilities.updateComponentTreeUI(frame);
- } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException ex) {
- JOptionPane.showMessageDialog(frame, "Failed to apply look and feel.");
- }
- });
- }
- // Add menus to menu bar
- menuBar.add(fileMenu);
- menuBar.add(aboutMenu);
- menuBar.add(lookAndFeelMenu);
- frame.setJMenuBar(menuBar);
- // Layout the components
- JPanel panel = new JPanel();
- panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
- panel.add(instructionsLabel);
- panel.add(inputScrollPane);
- panel.add(convertButton);
- panel.add(clearButton);
- panel.add(new JLabel("Converted Dimensions in Inches:"));
- panel.add(outputScrollPane);
- panel.add(totalSquareInchesLabel);
- panel.add(totalSquareInchesField);
- panel.add(totalSquareFeetLabel);
- panel.add(totalSquareFeetField);
- panel.add(sheetsRequiredLabel);
- panel.add(sheetsRequiredField);
- frame.add(panel);
- frame.setVisible(true);
- // Button action listeners
- convertButton.addActionListener(e -> {
- String input = inputTextArea.getText();
- String[] lines = input.split("\n");
- double totalSquareInches = 0;
- StringBuilder output = new StringBuilder();
- for (String line : lines) {
- line = line.trim();
- if (line.matches("\\d+cm\\s*\\*\\s*\\d+cm")) {
- String[] parts = line.split("\\*");
- double widthCm = Double.parseDouble(parts[0].replace("cm", "").trim());
- double heightCm = Double.parseDouble(parts[1].replace("cm", "").trim());
- double widthIn = widthCm * 0.393701;
- double heightIn = heightCm * 0.393701;
- double areaIn = widthIn * heightIn;
- totalSquareInches += areaIn;
- output.append(String.format("%s = %.2f in * %.2f in = %.2f sq in \n", line, widthIn, heightIn, areaIn));
- } else {
- output.append(String.format("Invalid input: %s\n", line));
- }
- }
- double totalSquareFeet = totalSquareInches / 144;
- int sheetsRequired = (int) Math.ceil(totalSquareFeet / 32); // Each sheet is 32 sq ft (8x4 ft)
- // Display results
- outputTextArea.setText(output.toString());
- totalSquareInchesField.setText(String.format("%.2f", totalSquareInches));
- totalSquareFeetField.setText(String.format("%.2f", totalSquareFeet));
- sheetsRequiredField.setText(String.valueOf(sheetsRequired));
- });
- clearButton.addActionListener(e -> {
- inputTextArea.setText("");
- outputTextArea.setText("");
- totalSquareInchesField.setText("");
- totalSquareFeetField.setText("");
- sheetsRequiredField.setText("");
- });
- saveResultsItem.addActionListener(e -> {
- // Open file chooser
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setDialogTitle("Save Results");
- fileChooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
- fileChooser.setAcceptAllFileFilterUsed(false);
- int userSelection = fileChooser.showSaveDialog(frame);
- if (userSelection == JFileChooser.APPROVE_OPTION) {
- // Get the selected file path
- String filePath = fileChooser.getSelectedFile().getAbsolutePath();
- if (!filePath.endsWith(".txt")) {
- filePath += ".txt"; // Ensure the file ends with .txt extension
- }
- try (FileWriter writer = new FileWriter(filePath)) {
- writer.write("Input Dimensions:\n");
- writer.write(inputTextArea.getText() + "\n\n");
- writer.write("Converted Dimensions:\n");
- writer.write(outputTextArea.getText() + "\n");
- writer.write("Total Square Inches: " + totalSquareInchesField.getText() + "\n");
- writer.write("Total Square Feet: " + totalSquareFeetField.getText() + "\n");
- writer.write("Sheets Required: " + sheetsRequiredField.getText() + "\n");
- JOptionPane.showMessageDialog(frame, "Results saved to " + filePath);
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(frame, "Error saving results: " + ex.getMessage());
- }
- }
- });
- exitItem.addActionListener(e -> System.exit(0));
- aboutItem.addActionListener(e -> {
- JOptionPane.showMessageDialog(frame, "Coded by Mark Harrington\nLondon, Kent\nDA6 8NP", "About", JOptionPane.INFORMATION_MESSAGE);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement