Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.util.Timer;
- import java.util.*;
- class React {
- private static Date startTime;
- public static void start() {
- startTime = new Date();
- }
- public static Long end() {
- return (new Date()).getTime() - startTime.getTime();
- }
- }
- class MyJFrame extends JFrame {
- private static Timer timer = new Timer();
- public MyJFrame(String title) {
- super(title);
- setSize(new Dimension(300, 150));
- setVisible(true);
- setLocationRelativeTo(null);
- setResizable(false);
- setLayout(GridBagLayout());
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- getContentPane().setBackground(Color.WHITE);
- JButton reactBtn = new JButton("Start");
- reactBtn.setFocusable(false);
- reactBtn.addActionListener(event => {
- if (reactBtn.getText().equalsIgnoreCase("start")) {
- reactBtn.setText("React");
- reactBtn.setEnabled(false);
- Random rand = new Random();
- scheduleTask(() -> {
- getContentPane().setBackground(Color.BLUE);
- React.start();
- reactBtn.setEnabled(true);
- }, rand.nextInt(11500));
- } else {
- JOptionPane.showMessageDialog(null, "WINNER: " + React.end() + " milliseconds", "RESULT", JOptionPane.INFORMATION_MESSAGE);
- getContentPane().setBackground(Color.WHITE);
- reactBtn.setText("Start");
- }
- };
- add(reactBtn, new GridBagConstraints());
- }
- private void scheduleTask(final Runnable runnable, long delay) {
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- runnable.run();
- }
- }, delay);
- }
- }
- public class Debug {
- public static void main(String[] args) {
- new MyJFrame("React Play");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement