Index: [Article Count Order] [Thread]

Date:  Mon, 19 Feb 2001 21:20:17 +0900
From:  ichiyama ryoichi <ichiyama@....jp>
Subject:  [XP-jp:01624] Re: GUI and XP
To:  extremeprogramming-jp@....jp
Message-Id:  <m3ae7iampq.wl@....localdomain>
In-Reply-To:  <200102161457.XAA03300@....jp>
References:  <un1bmpxtb.wl@....jp>	<200102161457.XAA03300@....jp>
X-Mail-Count: 01624

いちやまです。

At Fri, 16 Feb 2001 23:57:17 +0900,
Akira SASAKI - 佐々木聡 wrote:
>   私は、直接Robotクラスを利用するのではなくて、簡単な制御構文なしマクロ
> もどき?を解釈してくれる仕組みを用意して
> 
>   マウスポインタを (x1,y1) に移動
>     ↓
>   テキストボックスに文字入力
>     ↓
>   マウスポインタを (x2,y2) に移動
>     ↓
>   クリックイベント発生
> 
> みたいなことをマクロもどき?でjava.util.Setかなんかに順に放り込むことで
> テストコードが(しかもJavaで)書けるといいなぁ、なんて考えてました。

RobotをSmalltalkのPenみたく使うMouseクラスを作ってあるので紹介します。
こんなかんじです。

// 五角形をなぞる。
Mouse mouse = new Mouse(300, 300);
mouse.press();
mouse.move(100);
mouse.turn(144);
mouse.move(100);
mouse.turn(144);
mouse.move(100);
mouse.turn(144);
mouse.move(100);
mouse.turn(144);
mouse.move(100);
mouse.release();

----------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;

public class Mouse {
    private int x;
    private int y;
    private double angle;
    private Robot robot;
    
    public Mouse(int aX, int aY) throws AWTException {
        robot = new Robot();
        goTo(aX, aY);
    }
    
    public void press() {
        robot.mousePress(InputEvent.BUTTON1_MASK);
    }
    
    public void release() {
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
    }
    
    public void turn(double degree) {
        angle += degree;
    }
    
    public void move(int length) {
        int newX = x + (int) (length * Math.cos(Math.toRadians(angle)));
        int newY = y + (int) (length * Math.sin(Math.toRadians(angle)));
        moveTo(newX, newY);
    }
    
    public void moveTo(int newX, int newY) {
        int oldX = x;
        int oldY = y;
        if (Math.abs(oldX - newX) > Math.abs(oldY - newY)) {
            moveAlongX(oldX, newX, oldY, newY);
        } else {
            moveAlongY(oldX, newX, oldY, newY);
        }
    }
    
    private void moveAlongX(int oldX, int newX, int oldY, int newY) {
        int directionX = (oldX > newX) ? -1 : 1;
        int directionY = (oldY > newY) ? -1 : 1;
        int aX = oldX; 
        int aY = oldY;
        int substituteXY = Math.abs(oldX - newX) - Math.abs(oldY - newY);
        int diffY = Math.abs(oldY - newY);
        int termX = substituteXY;
        while (aX != newX) {
            if (termX <= 0) {
                aY += directionY;
                termX += substituteXY;
            } else {
                termX -= diffY;
            }
            goTo(aX, aY);
            aX += directionX;
        }
        goTo(aX, aY);
    }
    
    private void moveAlongY(int oldX, int newX, int oldY, int newY) {
        int directionX = (oldX > newX) ? -1 : 1;
        int directionY = (oldY > newY) ? -1 : 1;
        int aX = oldX;
        int aY = oldY;
        int substituteXY = Math.abs(oldY - newY) - Math.abs(oldX - newX);
        int diffX = Math.abs(oldX - newX);
        int termY = substituteXY;
        while (aY != newY) {
            if (termY <= 0) {
                aX += directionX;
                termY += substituteXY;
            } else {
                termY -= diffX;
            }
            goTo(aX, aY);
            aY += directionY;
        }
        goTo(aX, aY);
    }

    private void goTo(int aX, int aY) {
        x = aX;
        y = aY;
        try {
            Thread.sleep(10);
        } catch (InterruptedException ex) {
        }
        robot.mouseMove(x, y);
    }
    
    public static void main(String[] args) throws AWTException {
        Mouse mouse = new Mouse(300, 300);
        mouse.move(200);
        assertEquals("first corner x", 500, mouse.x, 1);
        assertEquals("first corner y", 300, mouse.y, 1);
        mouse.turn(-120);
        mouse.move(200);
        assertEquals("last corner x", 400, mouse.x, 1);
        int top = 300 - (int) (Math.sin(Math.toRadians(60)) * 200);
        assertEquals("last corner y", top, mouse.y, 1);
        mouse.turn(-120);
        mouse.move(200);
        assertEquals("last corner x", 300, mouse.x, 1);
        assertEquals("last corner y", 300, mouse.y, 1);
        System.exit(0);
    }
    
    protected static void assertEquals(String message, double expect, 
                                       double result, double delta) {
        if (!(expect - delta <= result && result <= expect + delta)) {
            System.out.println(message + " extect: " + expect + 
                               " result: " + result);
        }
    }
}
--------------------------------------------------------------------------

ただあんまり使う機会がないんですよね。

-- 
ichiyama ryoichi
mailto:ichiyama@....jp