Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Toolkit;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Random;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class Main extends JFrame implements MouseListener {
- public int[] position = new int[] { 400, 300 };
- public int[] mousePos = new int[] { 0, 0 };
- public Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
- public Random rnd = new Random();
- public int lvl = 0;
- JLabel text;
- public void mouseClicked(MouseEvent e) {}
- public void mousePressed(MouseEvent e) {
- this.mousePos[0] = e.getX();
- this.mousePos[1] = e.getY();
- boolean inRect = false;
- int offsetX = this.mousePos[0] - this.position[0] + 15;
- int offsetY = this.mousePos[1] - this.position[1] + 15;
- if (offsetX >= -15 && offsetX <= 15 && offsetY >= -15 && offsetY <= 15)
- inRect = true;
- if (inRect) {
- this.lvl++;
- this.text.setForeground(new Color(21, 199, 5));
- this.text.setText("<html><center>NICE</center><br><center>clicks = " + this.lvl + "</center></html>");
- } else {
- this.text.setForeground(Color.RED);
- this.text.setText("WRONG");
- }
- WriteFile(offsetX + "x" + offsetY);
- this.position[0] = this.rnd.nextInt(this.size.width - 60) + 30;
- this.position[1] = this.rnd.nextInt(this.size.height - 60) + 30;
- repaint();
- }
- public void mouseReleased(MouseEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- public static void main(String[] args) {
- (new Main()).Quadro();
- }
- public void Quadro() {
- this.text = new JLabel("CLICK ON RED AREA");
- this.text.setFont(new Font("Verdana", 1, 18));
- setLayout(new FlowLayout(1));
- add(this.text);
- setSize(this.size);
- setUndecorated(true);
- addMouseListener(this);
- setDefaultCloseOperation(3);
- setLocationRelativeTo((Component)null);
- setVisible(true);
- }
- public void paint(Graphics g) {
- super.paint(g);
- Sleep(100);
- g.setColor(Color.RED);
- g.fillRect(this.position[0], this.position[1], 30, 30);
- g.setColor(Color.BLACK);
- g.drawRect(this.position[0] - 1, this.position[1] - 1, 31, 31);
- g.drawRect(this.position[0] - 2, this.position[1] - 2, 33, 33);
- }
- public void WriteFile(String dat) {
- try(FileWriter fw = new FileWriter("MouseAccuracy.dat", true);
- BufferedWriter bw = new BufferedWriter(fw);
- PrintWriter out = new PrintWriter(bw)) {
- out.println(dat);
- } catch (IOException iOException) {}
- }
- public void Sleep(int ms) {
- try {
- Thread.sleep(ms);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement