Advertisement
Guest User

Untitled

a guest
Feb 6th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Component;
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Toolkit;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseListener;
  10. import java.io.BufferedWriter;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.io.PrintWriter;
  14. import java.util.Random;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17.  
  18. public class Main extends JFrame implements MouseListener {
  19.   public int[] position = new int[] { 400, 300 };
  20.  
  21.   public int[] mousePos = new int[] { 0, 0 };
  22.  
  23.   public Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
  24.  
  25.   public Random rnd = new Random();
  26.  
  27.   public int lvl = 0;
  28.  
  29.   JLabel text;
  30.  
  31.   public void mouseClicked(MouseEvent e) {}
  32.  
  33.   public void mousePressed(MouseEvent e) {
  34.     this.mousePos[0] = e.getX();
  35.     this.mousePos[1] = e.getY();
  36.     boolean inRect = false;
  37.     int offsetX = this.mousePos[0] - this.position[0] + 15;
  38.     int offsetY = this.mousePos[1] - this.position[1] + 15;
  39.     if (offsetX >= -15 && offsetX <= 15 && offsetY >= -15 && offsetY <= 15)
  40.       inRect = true;
  41.     if (inRect) {
  42.       this.lvl++;
  43.       this.text.setForeground(new Color(21, 199, 5));
  44.       this.text.setText("<html><center>NICE</center><br><center>clicks = " + this.lvl + "</center></html>");
  45.     } else {
  46.       this.text.setForeground(Color.RED);
  47.       this.text.setText("WRONG");
  48.     }
  49.     WriteFile(offsetX + "x" + offsetY);
  50.     this.position[0] = this.rnd.nextInt(this.size.width - 60) + 30;
  51.     this.position[1] = this.rnd.nextInt(this.size.height - 60) + 30;
  52.     repaint();
  53.   }
  54.  
  55.   public void mouseReleased(MouseEvent e) {}
  56.  
  57.   public void mouseEntered(MouseEvent e) {}
  58.  
  59.   public void mouseExited(MouseEvent e) {}
  60.  
  61.   public static void main(String[] args) {
  62.     (new Main()).Quadro();
  63.   }
  64.  
  65.   public void Quadro() {
  66.     this.text = new JLabel("CLICK ON RED AREA");
  67.     this.text.setFont(new Font("Verdana", 1, 18));
  68.     setLayout(new FlowLayout(1));
  69.     add(this.text);
  70.     setSize(this.size);
  71.     setUndecorated(true);
  72.     addMouseListener(this);
  73.     setDefaultCloseOperation(3);
  74.     setLocationRelativeTo((Component)null);
  75.     setVisible(true);
  76.   }
  77.  
  78.   public void paint(Graphics g) {
  79.     super.paint(g);
  80.     Sleep(100);
  81.     g.setColor(Color.RED);
  82.     g.fillRect(this.position[0], this.position[1], 30, 30);
  83.     g.setColor(Color.BLACK);
  84.     g.drawRect(this.position[0] - 1, this.position[1] - 1, 31, 31);
  85.     g.drawRect(this.position[0] - 2, this.position[1] - 2, 33, 33);
  86.   }
  87.  
  88.   public void WriteFile(String dat) {
  89.     try(FileWriter fw = new FileWriter("MouseAccuracy.dat", true);
  90.         BufferedWriter bw = new BufferedWriter(fw);
  91.         PrintWriter out = new PrintWriter(bw)) {
  92.       out.println(dat);
  93.     } catch (IOException iOException) {}
  94.   }
  95.  
  96.   public void Sleep(int ms) {
  97.     try {
  98.       Thread.sleep(ms);
  99.     } catch (InterruptedException e) {
  100.       e.printStackTrace();
  101.     }
  102.   }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement