Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 绘制箭头时出现问题。如何正确设置坐标?_Java_Swing_Graph_Graphics_Graphics2d - Fatal编程技术网

Java 绘制箭头时出现问题。如何正确设置坐标?

Java 绘制箭头时出现问题。如何正确设置坐标?,java,swing,graph,graphics,graphics2d,Java,Swing,Graph,Graphics,Graphics2d,屏幕: 编辑 因此,我在程序员的建议下更改了代码,但箭头的移动令人不快。屏幕-> }因此,旋转将发生在左上角(0x0或“原点”)。对于一个组件,这已经被翻译到左上角供您参考 您还需要考虑到转换是复合的,因此您需要确保在完成转换时撤消它们-特别是因为Swing中的图形上下文是共享的。。。那真的会把事情搞砸 所以。基本思想是: 将内容转换到要绘制形状的位置 围绕形状中心旋转上下文,“通常”会产生所需的结果 重置转换 下面的示例演示了基本思想,并使用计时器旋转形状 public ArrowHead

屏幕:

编辑 因此,我在程序员的建议下更改了代码,但箭头的移动令人不快。屏幕->


}因此,旋转将发生在左上角(
0x0
或“原点”)。对于一个组件,这已经被翻译到左上角供您参考

您还需要考虑到转换是复合的,因此您需要确保在完成转换时撤消它们-特别是因为Swing中的
图形
上下文是共享的。。。那真的会把事情搞砸

所以。基本思想是:

  • 将内容转换到要绘制形状的位置
  • 围绕形状中心旋转上下文,“通常”会产生所需的结果
  • 重置转换
下面的示例演示了基本思想,并使用
计时器
旋转形状

public ArrowHead() {
    double size = Config.ARROW_HEAD_SIZE;
    moveTo(0, size);
    lineTo(size / 2, 0);
    lineTo(size, size);
}

您可能还想看一看,这是这个概念的一个更复杂的示例,但它似乎更符合您试图实现的目标

,因此,旋转将发生在锚点的左上角(
0x0
)。我想做的是,将该区域平移到我想要对象定位的位置,使定位点成为
0x0
围绕对象中心的旋转,记住在完成时要消除混乱,看看我提供的,你有两个完整的示例,可以运行并根据自己的内容进行修改,您可以看到代码是如何工作的,以及更改是如何影响它的。现在,看看你给我提供了什么——一些断章取义的片段,其中有很多关于状态和上下文的未回答的问题。你需要把你的问题提炼成一个简单的问题,在你告诉我这“太难”之前——我已经做了两次了
private void drawEdgeLine(Graphics2D graphics, double x1, double y1, double x2, double y2) {

    graphics.setColor(Color.BLACK);
    graphics.draw(new Line2D.Double(x1, y1, x2, y2));

    ArrowHead arrowHead = new ArrowHead();
    double length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    double t1 = Config.VERTEX_RADIUS / length;
    double t2 = (length - Config.VERTEX_RADIUS) / length;
    double arrowX, arrowY;
    if (t1 > t2) {
        arrowX = x1 + t1 * (x2 - x1);
        arrowY = y1 + t1 * (y2 - y1);
    } else {
        arrowX = x1 + t2 * (x2 - x1);
        arrowY = y1 + t2 * (y2 - y1);
    }

    double angle = Math.atan2(y2 - y1, x2 - x1);
    /*double angleDegrees = Math.toDegrees(angle + Math.PI);
    System.out.println(angleDegrees);
    if (angleDegrees > 90 && angleDegrees < 270) {
        arrowY += Config.ARROW_HEAD_SIZE / 2;
    } else {
        arrowX -= Config.ARROW_HEAD_SIZE / 2;
    }*/

    AffineTransform transform = AffineTransform.getTranslateInstance(arrowX, arrowY);
    transform.rotate(angle + Math.PI / 2);
    arrowHead.transform(transform);
    graphics.draw(arrowHead);
}
public ArrowHead() {
    double size = Config.ARROW_HEAD_SIZE;
    moveTo(0, size);
    lineTo(size / 2, 0);
    lineTo(size, size);
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ArrowHead extends Path2D.Double {

        public ArrowHead() {
            int size = 10;
            moveTo(0, size);
            lineTo(size / 2, 0);
            lineTo(size, size);
            closePath();
        }

    }

    public class TestPane extends JPanel {

        private ArrowHead arrow = new ArrowHead();
        private double angleOfAttack = 0;

        public TestPane() {
            Timer timer = new Timer(5, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    angleOfAttack += 0.5;
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Snap shot the current context
            Graphics2D g2d = (Graphics2D) g.create();

            // Translate to somewhere you want the arrow painted ...
            g2d.translate(50, 50);
            // The bounding box of the arrow...
            Rectangle bounds = arrow.getBounds();

            // A guide, showing where the arrow is been painted without the rotation
            g2d.setColor(Color.RED);
            g2d.draw(bounds);

            // Rotate abount the middle of the arrow...
            g2d.rotate(Math.toRadians(angleOfAttack), bounds.width / 2, bounds.height / 2);
            g2d.setColor(Color.BLACK);
            // Draw the arrow
            g2d.fill(arrow);

            // Discard the transformations so we don't effect anyone else
            g2d.dispose();
        }

    }

}