Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.util.Random;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.Timer;
- public class QS extends JFrame {
- private static final long serialVersionUID = 1L;
- public static void main(String[] args) {
- new QS();
- }
- private final JButton start;
- private final JButton action;
- private final Timer[] timer = new Timer[2];
- private final Random r;
- private final int millisec;
- private final QS self;
- public QS() {
- setTitle("QS");
- setSize(new Dimension(300, 300));
- setLayout(new FlowLayout());
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- r = new Random();
- self = this;
- millisec = 2000 + r.nextInt(10000);
- timer[0] = new Timer(millisec, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- timer[1].start();
- timer[0].stop();
- timer[0].setDelay(2000 + r.nextInt(10000));
- paint();
- action.setEnabled(true);
- }
- });
- timer[1] = new Timer(1500, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- timer[1].stop();
- JOptionPane.showMessageDialog(self, "FAIL");
- clear();
- start.setEnabled(true);
- action.setEnabled(false);
- }
- });
- start = new JButton("Start");
- start.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- start.setEnabled(false);
- timer[0].start();
- }
- });
- add(start);
- action = new JButton("Action");
- action.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- timer[0].stop();
- timer[1].stop();
- JOptionPane.showMessageDialog(self, "SUCCESS");
- clear();
- start.setEnabled(true);
- action.setEnabled(false);
- }
- });
- action.setEnabled(false);
- add(action);
- setVisible(true);
- }
- private void clear() {
- Graphics g = getGraphics();
- g.setColor(getBackground());
- g.fillRect(100, 100, 100, 100);
- }
- private void paint() {
- Graphics g = getGraphics();
- g.setColor(Color.RED);
- g.fillOval(100, 100, 100, 100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement