0

私は2Dプログラムを書いています。私paintComponentは円弧を作成しました。

public class Board extends Panel{

    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D graphics2d = (Graphics2D)g;
        int x = MouseInfo.getPointerInfo().getLocation().x;//set mouses current position
        int y = MouseInfo.getPointerInfo().getLocation().y;

        graphics2d.setStroke(wideStroke);
        graphics2d.draw(new Arc2D.Double(200, 200, 100, 100, ?, 180, Arc2D.OPEN));

    }
}

私のメインではThread、グラフを更新するために a を使用しています。の位置が?開始角度です。これを変更するたびに、円弧は車の車輪の半分のように円を描くように移動します。円弧の動きをマウスに追従させることは可能ですか? 例えば? = 270

ここに画像の説明を入力

どうすればいいですか?(塗装下手ですみません!)

4

1 に答える 1

1

したがって、 Java 2d 回転方向のマウス ポイントからの情報に基づいて

2 つのことが必要です。アンカーポイント (円弧の中心点) とターゲットポイント (マウスポイント) が必要です。

を使用するMouseMotionListenerと、コンポーネント内のマウスの動きを監視できます

// Reference to the last known position of the mouse...
private Point mousePoint;
//....
addMouseMotionListener(new MouseAdapter() {
    @Override
    public void mouseMoved(MouseEvent e) {
        mousePoint = e.getPoint();                    
        repaint();                    
    }                
});

次に、これらの 2 点間の角度を計算する必要があります...

if (mousePoint != null) {
    // This represents the anchor point, in this case, 
    // the centre of the component...
    int x = width / 2;
    int y = height / 2;

    // This is the difference between the anchor point
    // and the mouse.  Its important that this is done
    // within the local coordinate space of the component,
    // this means either the MouseMotionListener needs to
    // be registered to the component itself (preferably)
    // or the mouse coordinates need to be converted into
    // local coordinate space
    int deltaX = mousePoint.x - x;
    int deltaY = mousePoint.y - y;

    // Calculate the angle...
    // This is our "0" or start angle..
    rotation = -Math.atan2(deltaX, deltaY);
    rotation = Math.toDegrees(rotation) + 180;
}

ここから 90 度を引く必要があります。これにより、円弧の開始角度が得られ、180 度の範囲が使用されます。

于 2014-04-30T22:39:05.357 に答える