Advertisement
Mark2020H

Designed for ease of use for builders and trades people 8 by 4 foot calculator

Jan 15th, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.25 KB | Source Code | 0 0
  1. package CalcDimensions;
  2.  
  3.  
  4. /* To run this file after compiling  with net beans use this syntax java -jar "CalulateEightByFour.jar"
  5.  
  6. /* Written  by MD Harrington  London  Kent DA6 8NP
  7. * Program is designed to assist builders ground worker in working out sheets of 8 by 4 foot wood ,
  8. * and plastic coverings for maintenance
  9. * Please see other links
  10. * https://www.facebook.com/mark.harrington.14289
  11. * https://www.instagram.com/markukh2021
  12. * https://pastebin.com/u/Mark2020H
  13. * https://codeshare.io/codes
  14. * https://github.com/markh2016?tab=repositories
  15. */
  16.  
  17. import javax.swing.*;
  18. import java.io.FileWriter;
  19. import java.io.IOException;
  20. import javax.swing.filechooser.FileNameExtensionFilter;
  21.  
  22. public class Calc8by4 {
  23.  
  24.     public static void main(String[] args) {
  25.         try {
  26.             UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
  27.         } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
  28.         }
  29.        
  30.        
  31.         // Create the main frame
  32.         JFrame frame = new JFrame("Wall Measurement Calculator");
  33.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.         frame.setSize(600, 700);
  35.         frame.setLocationRelativeTo(null);
  36.  
  37.         // Create components
  38.         JLabel instructionsLabel = new JLabel("Enter dimensions (e.g., 161cm * 93cm):");
  39.         JTextArea inputTextArea = new JTextArea(10, 40);
  40.         JScrollPane inputScrollPane = new JScrollPane(inputTextArea);
  41.         JButton convertButton = new JButton("Convert to Inches");
  42.         JButton clearButton = new JButton("Clear");
  43.         JTextArea outputTextArea = new JTextArea(10, 40);
  44.         outputTextArea.setEditable(false);
  45.         JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
  46.         JLabel totalSquareInchesLabel = new JLabel("Total Square Area (sq in):");
  47.         JTextField totalSquareInchesField = new JTextField(15);
  48.         totalSquareInchesField.setEditable(false);
  49.         JLabel totalSquareFeetLabel = new JLabel("Total Square Area (sq ft):");
  50.         JTextField totalSquareFeetField = new JTextField(15);
  51.         totalSquareFeetField.setEditable(false);
  52.         JLabel sheetsRequiredLabel = new JLabel("Number of Sheets Required:");
  53.         JTextField sheetsRequiredField = new JTextField(15);
  54.         sheetsRequiredField.setEditable(false);
  55.  
  56.         // Menu Bar
  57.         JMenuBar menuBar = new JMenuBar();
  58.  
  59.         // File Menu
  60.         JMenu fileMenu = new JMenu("File");
  61.         JMenuItem saveResultsItem = new JMenuItem("Save Results to File");
  62.         JMenuItem exitItem = new JMenuItem("Exit");
  63.         fileMenu.add(saveResultsItem);
  64.         fileMenu.add(exitItem);
  65.  
  66.         // About Menu
  67.         JMenu aboutMenu = new JMenu("About");
  68.         JMenuItem aboutItem = new JMenuItem("About");
  69.         aboutMenu.add(aboutItem);
  70.  
  71.         // Look and Feel Menu
  72.         JMenu lookAndFeelMenu = new JMenu("Set Look and Feel");
  73.         UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
  74.         for (UIManager.LookAndFeelInfo look : looks) {
  75.             JMenuItem lookItem = new JMenuItem(look.getName());
  76.             lookAndFeelMenu.add(lookItem);
  77.             lookItem.addActionListener(e -> {
  78.                 try {
  79.                     UIManager.setLookAndFeel(look.getClassName());
  80.                     SwingUtilities.updateComponentTreeUI(frame);
  81.                 } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException ex) {
  82.                     JOptionPane.showMessageDialog(frame, "Failed to apply look and feel.");
  83.                 }
  84.             });
  85.         }
  86.  
  87.         // Add menus to menu bar
  88.         menuBar.add(fileMenu);
  89.         menuBar.add(aboutMenu);
  90.         menuBar.add(lookAndFeelMenu);
  91.         frame.setJMenuBar(menuBar);
  92.  
  93.         // Layout the components
  94.         JPanel panel = new JPanel();
  95.         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  96.         panel.add(instructionsLabel);
  97.         panel.add(inputScrollPane);
  98.         panel.add(convertButton);
  99.         panel.add(clearButton);
  100.         panel.add(new JLabel("Converted Dimensions in Inches:"));
  101.         panel.add(outputScrollPane);
  102.         panel.add(totalSquareInchesLabel);
  103.         panel.add(totalSquareInchesField);
  104.         panel.add(totalSquareFeetLabel);
  105.         panel.add(totalSquareFeetField);
  106.         panel.add(sheetsRequiredLabel);
  107.         panel.add(sheetsRequiredField);
  108.  
  109.         frame.add(panel);
  110.         frame.setVisible(true);
  111.  
  112.         // Button action listeners
  113.         convertButton.addActionListener(e -> {
  114.             String input = inputTextArea.getText();
  115.             String[] lines = input.split("\n");
  116.             double totalSquareInches = 0;
  117.             StringBuilder output = new StringBuilder();
  118.  
  119.             for (String line : lines) {
  120.                 line = line.trim();
  121.                 if (line.matches("\\d+cm\\s*\\*\\s*\\d+cm")) {
  122.                     String[] parts = line.split("\\*");
  123.                     double widthCm = Double.parseDouble(parts[0].replace("cm", "").trim());
  124.                     double heightCm = Double.parseDouble(parts[1].replace("cm", "").trim());
  125.                     double widthIn = widthCm * 0.393701;
  126.                     double heightIn = heightCm * 0.393701;
  127.                     double areaIn = widthIn * heightIn;
  128.                     totalSquareInches += areaIn;
  129.                     output.append(String.format("%s = %.2f in * %.2f in = %.2f sq in \n", line, widthIn, heightIn, areaIn));
  130.                 } else {
  131.                     output.append(String.format("Invalid input: %s\n", line));
  132.                 }
  133.             }
  134.  
  135.             double totalSquareFeet = totalSquareInches / 144;
  136.             int sheetsRequired = (int) Math.ceil(totalSquareFeet / 32); // Each sheet is 32 sq ft (8x4 ft)
  137.  
  138.             // Display results
  139.             outputTextArea.setText(output.toString());
  140.             totalSquareInchesField.setText(String.format("%.2f", totalSquareInches));
  141.             totalSquareFeetField.setText(String.format("%.2f", totalSquareFeet));
  142.             sheetsRequiredField.setText(String.valueOf(sheetsRequired));
  143.         });
  144.  
  145.         clearButton.addActionListener(e -> {
  146.             inputTextArea.setText("");
  147.             outputTextArea.setText("");
  148.             totalSquareInchesField.setText("");
  149.             totalSquareFeetField.setText("");
  150.             sheetsRequiredField.setText("");
  151.         });
  152.  
  153.         saveResultsItem.addActionListener(e -> {
  154.     // Open file chooser
  155.      JFileChooser fileChooser = new JFileChooser();
  156.         fileChooser.setDialogTitle("Save Results");
  157.          fileChooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
  158.              fileChooser.setAcceptAllFileFilterUsed(false);
  159.  
  160.         int userSelection = fileChooser.showSaveDialog(frame);
  161.         if (userSelection == JFileChooser.APPROVE_OPTION) {
  162.             // Get the selected file path
  163.             String filePath = fileChooser.getSelectedFile().getAbsolutePath();
  164.             if (!filePath.endsWith(".txt")) {
  165.                 filePath += ".txt"; // Ensure the file ends with .txt extension
  166.             }
  167.  
  168.             try (FileWriter writer = new FileWriter(filePath)) {
  169.                 writer.write("Input Dimensions:\n");
  170.                 writer.write(inputTextArea.getText() + "\n\n");
  171.                 writer.write("Converted Dimensions:\n");
  172.                 writer.write(outputTextArea.getText() + "\n");
  173.                 writer.write("Total Square Inches: " + totalSquareInchesField.getText() + "\n");
  174.                 writer.write("Total Square Feet: " + totalSquareFeetField.getText() + "\n");
  175.                 writer.write("Sheets Required: " + sheetsRequiredField.getText() + "\n");
  176.                 JOptionPane.showMessageDialog(frame, "Results saved to " + filePath);
  177.             } catch (IOException ex) {
  178.                 JOptionPane.showMessageDialog(frame, "Error saving results: " + ex.getMessage());
  179.             }
  180.         }
  181.     });
  182.  
  183.  
  184.         exitItem.addActionListener(e -> System.exit(0));
  185.  
  186.         aboutItem.addActionListener(e -> {
  187.             JOptionPane.showMessageDialog(frame, "Coded by Mark Harrington\nLondon, Kent\nDA6 8NP", "About", JOptionPane.INFORMATION_MESSAGE);
  188.         });
  189.     }
  190. }
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement