如何在不传递Graphics2d对象引用的情况下旋转Arc2d对象

时间:2019-04-09 06:29:41

标签: java graphics2d

我目前正在用一个基本的游戏循环来动画Java游戏。我有一个Arc2d和Line2d对象,看起来像弓。我没有Graphics2d引用来利用rotation()方法,但是我需要一起旋转Arc2d对象和Line2d对象,以使它们在跟随鼠标指针的同时继续保持相同的形状和相互关系。

我尝试修改Arc2d对象的开始和范围参数,但是它无法保持其形状,并且显然相对于Line2d对象移动了。我还试图找出一种利用Graphics2d.rotate()方法的方法,但是我没有看到一种传递Graphics2d对象旋转的方法。

这是我要旋转对象的类。

import controller.Main;

import java.awt.*;
import java.awt.geom.Arc2D;
import java.awt.geom.Line2D;

public class Shooter extends GameFigure {

    public final int BOW_STRING_SIZE = 40;
    public final int ARROW_LEN = 30;
    public final int UNIT_MOVE = 10;
    public Arc2D.Float bow;
    public Line2D.Float arrow;
    public Color color = Color.YELLOW;
    public double rad;

    public Shooter(int x, int y) {
        super(x, y);
        bow = new Arc2D.Float(x - BOW_STRING_SIZE / 2, y - 15, BOW_STRING_SIZE, BOW_STRING_SIZE, 0, 180, Arc2D.CHORD);
        arrow = new Line2D.Float(x, y, x, y - ARROW_LEN);
    }

    @Override
    public void render(Graphics2D g2) {
        g2.setColor(color);
        g2.setStroke(new BasicStroke(7));
        g2.draw(arrow);
        g2.draw(bow);
    }

    @Override
    public void update() {
        MousePointer mousePointer = (MousePointer) Main.gameData.fixedObjects.get(Main.INDEX_MOUSE_POINTER);
        float tx = mousePointer.location.x;
        float ty = mousePointer.location.y;
        rad = Math.atan2(ty - super.location.y, tx - super.location.x);
        /*float barrel_y = (float) (ARROW_LEN * Math.sin(rad));
        float barrel_x = (float) (ARROW_LEN * Math.cos(rad));

        arrow.x1 = super.location.x;
        arrow.y1 = super.location.y;
        arrow.x2 = super.location.x + barrel_x;
        arrow.y2 = super.location.y + barrel_y;*/
    }

当前,弓上的箭头随鼠标指针旋转,但弓本身不旋转。

0 个答案:

没有答案