LoginSignup
0
1

More than 1 year has passed since last update.

【JAVA】ただマウスポインターを動かすだけのプログラム

Posted at

きっかけ

マウスポインター動かすプログラムを定期実行しておけば人がいるように見せらえるのではないか?
ではプログラムの勉強で作ってしまおうと思いました。

必要な環境

EclipseのJAVAが使える環境なら問題ないと思います。
またWindows前提となります。定期実行はタスクスケジューラで行います。

詳細な手順などは省いているため、プログラムしたことがある方を前提で記載します。

作業順序

大まかな作業順序の流れは以下になります。

  • マウスポインターを動かすプログラム作成(JAVA)
  • 実行可能jarを生成
  • jarを動作させるbat作成
  • タスクスケジューラにbat登録

マウスポインターを動かすプログラム作成(JAVA)

以下がソースコードになります。
作成したらデバック実行して動作するか確認してみてください。
引数は「3」(秒)がちょうどよいかなと思います。

マウスポインターソースコード
import java.awt.AWTException;
import java.awt.GraphicsEnvironment;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Rectangle;
import java.awt.Robot;
import java.util.Random;

public class MoveMousePointer {

    private static Integer SPLIT = 50;
    private static Integer INTERVAL = 10;

    public void call(Integer moveSecond) {
        this.moveMousePointer(moveSecond);
    }

    private void moveMousePointer(Integer moveSecond) {
        Rectangle desktopBounds = this.getDesktopSize();
        Integer moveHeight = this.getRandomValue((int) desktopBounds.getHeight());
        Integer moveWidth = this.getRandomValue((int) desktopBounds.getWidth());

        Point point = this.getCurrentPointerLocation();
        Integer currentHeight = (int) point.getY();
        Integer currentWidth = (int) point.getX();

        Integer cnt = (moveSecond * MoveMousePointer.SPLIT);
        Integer movePointOneHeight = (moveHeight - currentHeight) / cnt;
        Integer movePointOneWidth = (moveWidth - currentWidth) / cnt;

        for (int i = 1; i <= cnt; i++) {
            Integer locationHeight = currentHeight + (movePointOneHeight * i);
            Integer locationWidth = currentWidth + (movePointOneWidth * i);
            this.moveLocationMousePointer(locationWidth, locationHeight);
            try {
                Thread.sleep(MoveMousePointer.INTERVAL);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private Rectangle getDesktopSize() {
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        return env.getMaximumWindowBounds();
    }

    private Point getCurrentPointerLocation() {
        PointerInfo pointerInfo = MouseInfo.getPointerInfo();
        return pointerInfo.getLocation();
    }

    private Integer getRandomValue(Integer range) {
        Random random = new Random();
        return random.nextInt(range);
    }

    private void moveLocationMousePointer(Integer width, Integer height) {
        Robot r = null;
        try {
            r = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        r.mouseMove(width, height);
    }
}
メインソースコード
public class App
{
    public static void main( String[] args )
    {
        if(args.length != 1) {
            System.out.println("引数を1つ指定してください");
            System.exit(1);
        }
        Integer moveSecond = Integer.parseInt(args[0]);
        if ( !(1 <= moveSecond && moveSecond <= 5) ) {
            System.out.println("引数1を1から5(秒)以内に設定してください");
            System.exit(1);
        }

        MoveMousePointer moveMousePointer = new MoveMousePointer();
        moveMousePointer.call(moveSecond);

        System.exit(0);
    }
}

実行可能jarを生成

Eclipseのエクスポートから実行可能jarで出力すれば問題ないと思います。

jarを動作させるbat作成

batファイルとjarを同じフォルダに配置している前提となります。
「move-mouse-pointer.jar」の部分は生成したjar名に修正してください。

batソースコード
@echo off

set folder=%~dp0
start %folder%move-mouse-pointer.jar 3

タスクスケジューラにbat登録

Windowsのタスクスケジューラから作成したbatを定期実行登録するだけです。
詳しい手順は参考サイトを見てください。

参考サイト

Javaでデスクトップのサイズを取得する方法
J2SE 5.0 Tiger 虎の穴 マウスの位置
乱数を生成!JavaでRandomクラスを使う方法【初心者向け】現役エンジニアが解説
【Windows/タスクスケジューラ】バッチを実行する際の3つの注意点

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1