Advertisement
cd62131

Red Ball

Feb 14th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.50 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.Graphics;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9. import java.util.Random;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.Timer;
  14.  
  15. public class QS extends JFrame {
  16.   private static final long serialVersionUID = 1L;
  17.  
  18.   public static void main(String[] args) {
  19.     new QS();
  20.   }
  21.   private final JButton start;
  22.   private final JButton action;
  23.   private final Timer[] timer = new Timer[2];
  24.   private final Random r;
  25.   private final int millisec;
  26.   private final QS self;
  27.  
  28.   public QS() {
  29.     setTitle("QS");
  30.     setSize(new Dimension(300, 300));
  31.     setLayout(new FlowLayout());
  32.     setDefaultCloseOperation(EXIT_ON_CLOSE);
  33.     r = new Random();
  34.     self = this;
  35.     millisec = 2000 + r.nextInt(10000);
  36.     timer[0] = new Timer(millisec, new ActionListener() {
  37.       @Override
  38.       public void actionPerformed(ActionEvent e) {
  39.         timer[1].start();
  40.         timer[0].stop();
  41.         timer[0].setDelay(2000 + r.nextInt(10000));
  42.         paint();
  43.         action.setEnabled(true);
  44.       }
  45.     });
  46.     timer[1] = new Timer(1500, new ActionListener() {
  47.       @Override
  48.       public void actionPerformed(ActionEvent e) {
  49.         timer[1].stop();
  50.         JOptionPane.showMessageDialog(self, "FAIL");
  51.         clear();
  52.         start.setEnabled(true);
  53.         action.setEnabled(false);
  54.       }
  55.     });
  56.     start = new JButton("Start");
  57.     start.addMouseListener(new MouseAdapter() {
  58.       @Override
  59.       public void mouseClicked(MouseEvent e) {
  60.         start.setEnabled(false);
  61.         timer[0].start();
  62.       }
  63.     });
  64.     add(start);
  65.     action = new JButton("Action");
  66.     action.addMouseListener(new MouseAdapter() {
  67.       @Override
  68.       public void mouseClicked(MouseEvent e) {
  69.         timer[0].stop();
  70.         timer[1].stop();
  71.         JOptionPane.showMessageDialog(self, "SUCCESS");
  72.         clear();
  73.         start.setEnabled(true);
  74.         action.setEnabled(false);
  75.       }
  76.     });
  77.     action.setEnabled(false);
  78.     add(action);
  79.     setVisible(true);
  80.   }
  81.  
  82.   private void clear() {
  83.     Graphics g = getGraphics();
  84.     g.setColor(getBackground());
  85.     g.fillRect(100, 100, 100, 100);
  86.   }
  87.  
  88.   private void paint() {
  89.     Graphics g = getGraphics();
  90.     g.setColor(Color.RED);
  91.     g.fillOval(100, 100, 100, 100);
  92.   }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement