Guest User

Number Counter (1.0-1.5)

a guest
Nov 22nd, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.86 KB | Source Code | 0 0
  1. Download Link for 1.0: https://www.mediafire.com/file/5sfuy34lgw4q3jf/Number_Counter_1.0.jar/file
  2. Download Link for 1.1: https://www.mediafire.com/file/00ov7dpew6ywtjs/Number_Counter_1.1.jar/file
  3. Download Link for 1.2: https://www.mediafire.com/file/ikvugi9qbzh6bpj/Number_Counter_1.2.jar/file
  4. Download Link for 1.3: https://www.mediafire.com/file/679fsofhqu7xyb4/Number_Counter_1.3.jar/file
  5. Download Link for 1.4: https://www.mediafire.com/file/lp8eck7kubo7ioe/Number_Counter_1.4.jar/file
  6. Download Link for 1.5: https://www.mediafire.com/file/a4vif8r31cg6x2v/Number_Counter_1.5.jar/file
  7.  
  8. Source code for 1.5:
  9.  
  10. import java.awt.*;
  11. import java.text.NumberFormat;
  12. import java.util.concurrent.atomic.AtomicLong;
  13. import javax.swing.*;
  14. import javax.swing.text.*;
  15.  
  16. @SuppressWarnings("serial")
  17. public class Main extends JFrame {
  18.     private JLabel label, msLabel;
  19.     private JButton startButton, stopButton, pauseResumeButton;
  20.     private JTextField maxCountField;
  21.     private AtomicLong count = new AtomicLong();
  22.     private long maxCount, lastTime, millis;
  23.     private boolean running, paused, msRunning;
  24.     private Timer timer;
  25.     private JComboBox<String> fontSelector;
  26.  
  27.     public Main() {
  28.         setTitle("Number Counter 1.5");
  29.         setSize(600, 280);
  30.         setDefaultCloseOperation(3);
  31.         setResizable(false);
  32.         setLayout(null);
  33.         label = new JLabel("Count: 0");
  34.         label.setFont(new Font("Arial", 1, 40));
  35.         add(label);
  36.         msLabel = new JLabel("Time: 0ms");
  37.         msLabel.setFont(new Font("Arial", 1, 30));
  38.         add(msLabel);
  39.         maxCountField = new JTextField("", 15);
  40.         maxCountField.setDocument(numberDocument());
  41.         add(maxCountField);
  42.         startButton = new JButton("Start");
  43.         startButton.addActionListener(e -> startCount());
  44.         add(startButton);
  45.         stopButton = new JButton("Stop");
  46.         stopButton.addActionListener(e -> stopCount());
  47.         stopButton.setEnabled(false);
  48.         add(stopButton);
  49.         pauseResumeButton = new JButton("Pause");
  50.         pauseResumeButton.addActionListener(e -> pauseResumeCount());
  51.         pauseResumeButton.setEnabled(false);
  52.         add(pauseResumeButton);
  53.         String[] fonts = {"Arial", "Courier New", "Comic Sans MS", "Verdana", "Bell MT"};
  54.         fontSelector = new JComboBox<>(fonts);
  55.         fontSelector.setSelectedItem("Arial");
  56.         fontSelector.addActionListener(e -> changeFont());
  57.         add(fontSelector);
  58.         componentPositions();
  59.         setLocationRelativeTo(null);
  60.         setVisible(true);
  61.     }
  62.  
  63.     private void changeFont() {
  64.         String selectedFont = (String) fontSelector.getSelectedItem();
  65.         label.setFont(new Font(selectedFont, 1, 40));
  66.         msLabel.setFont(new Font(selectedFont, 1, 30));
  67.         componentPositions();
  68.     }
  69.  
  70.     private PlainDocument numberDocument() {
  71.         return new PlainDocument() {
  72.             public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
  73.                 if (str == null || !str.matches("[0-9]*")) return;
  74.                 StringBuilder sb = new StringBuilder(getText(0, getLength()));
  75.                 sb.insert(offs, str);
  76.                 if (sb.length() == 0 || sb.toString().matches("[0-9]+")) super.insertString(offs, str, a);
  77.             }
  78.         };
  79.     }
  80.  
  81.     private void componentPositions() {
  82.         int labelWidth = label.getPreferredSize().width;
  83.         int labelX = (getWidth() - labelWidth) / 2;
  84.         label.setBounds(labelX, 10, labelWidth, 50);
  85.         int secondsLabelWidth = msLabel.getPreferredSize().width;
  86.         int secondsLabelX = (getWidth() - secondsLabelWidth) / 2;
  87.         msLabel.setBounds(secondsLabelX, 155, secondsLabelWidth, 30);
  88.         int textFieldWidth = maxCountField.getPreferredSize().width;
  89.         int textFieldX = (getWidth() - textFieldWidth) / 2;
  90.         maxCountField.setBounds(textFieldX, 115, textFieldWidth, 30);
  91.         int buttonWidth = 80;
  92.         int spacing = 20;
  93.         int totalButtonWidth = buttonWidth * 3 + spacing * 2;
  94.         int buttonY = 70;
  95.         int startButtonX = (getWidth() - totalButtonWidth) / 2;
  96.         startButton.setBounds(startButtonX, buttonY, buttonWidth, 30);
  97.         stopButton.setBounds(startButtonX + buttonWidth + spacing, buttonY, buttonWidth, 30);
  98.         pauseResumeButton.setBounds(startButtonX + 2 * (buttonWidth + spacing), buttonY, buttonWidth, 30);
  99.         fontSelector.setBounds((getWidth() - 150) / 2, 200, 150, 30);
  100.     }
  101.  
  102.     public void startCount() {
  103.         if (running) return;
  104.         try {
  105.             maxCount = Long.parseLong(maxCountField.getText());
  106.         } catch (NumberFormatException e) { return; }
  107.         running = true;
  108.         count.set(0);
  109.         millis = 0;
  110.         paused = false;
  111.         stopButton.setEnabled(true);
  112.         pauseResumeButton.setEnabled(true);
  113.         lastTime = System.currentTimeMillis();
  114.         msRunning = true;
  115.         new Thread(() -> {
  116.             while (running) {
  117.                 if (!paused) {
  118.                     long currentCount = count.getAndIncrement();
  119.                     if (currentCount >= maxCount) {
  120.                         count.set(maxCount);
  121.                         running = false;
  122.                         msRunning = false;
  123.                     }
  124.                     continue;
  125.                 }
  126.             }
  127.             msRunning = false;
  128.         }).start();
  129.         timer = new Timer(1, e -> {
  130.             updateLabel();
  131.             if (msRunning) {
  132.                 long now = System.currentTimeMillis();
  133.                 millis += (now - lastTime);
  134.                 msLabel.setText(millis + "ms");
  135.                 lastTime = now;
  136.             }
  137.         });
  138.         timer.start();
  139.     }
  140.  
  141.     public void stopCount() {
  142.         running = false;
  143.         count.set(0);
  144.         label.setText("Count: 0");
  145.         millis = 0;
  146.         msLabel.setText("Time: 0ms");
  147.         stopButton.setEnabled(false);
  148.         pauseResumeButton.setEnabled(false);
  149.         if (timer != null) {
  150.             timer.stop();
  151.             msRunning = false;
  152.         }
  153.         componentPositions();
  154.     }
  155.  
  156.     public void pauseResumeCount() {
  157.         if (!running || count.get() >= maxCount) return;
  158.         paused = !paused;
  159.         if (paused) {
  160.             pauseResumeButton.setText("Resume");
  161.             msRunning = false;
  162.         } else {
  163.             pauseResumeButton.setText("Pause");
  164.             msRunning = true;
  165.             lastTime = System.currentTimeMillis();
  166.         }
  167.     }
  168.  
  169.     private void updateLabel() {
  170.         long currentCount = count.get();
  171.         StringBuilder sb = new StringBuilder();
  172.         sb.append(NumberFormat.getInstance().format(currentCount));
  173.         label.setText(sb.toString());
  174.         componentPositions();
  175.     }
  176.  
  177.     public static void main(String[] args) {
  178.         SwingUtilities.invokeLater(Main::new);
  179.     }
  180. }
Add Comment
Please, Sign In to add comment