Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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_User Interface_Graphics_Awt_Computational Geometry - Fatal编程技术网

Java 如何基于给定的度数旋转直线

Java 如何基于给定的度数旋转直线,java,user-interface,graphics,awt,computational-geometry,Java,User Interface,Graphics,Awt,Computational Geometry,我用图形对象画了一条线。我想根据鼠标的拖动程度将这条线旋转一定的角度。我可以得到旋转它所需要的度数,但是我如何基于这个旋转线呢 谢谢大家! 我想你说的是Java AWT图形类。图形可以看作是画布。这是一个像素值数组,“画一条线”只是一个实用函数,可以改变其中一些像素的值——从它的角度来看,没有什么“线对象”可以说。通常情况下,你应该抹去整件事,然后用你想要的角度画一条新的线。不过,您可能希望了解Graphics2D(),尤其是setTransform和AffineTransform类。我想您谈论

我用图形对象画了一条线。我想根据鼠标的拖动程度将这条线旋转一定的角度。我可以得到旋转它所需要的度数,但是我如何基于这个旋转线呢


谢谢大家!

我想你说的是Java AWT图形类。图形可以看作是画布。这是一个像素值数组,“画一条线”只是一个实用函数,可以改变其中一些像素的值——从它的角度来看,没有什么“线对象”可以说。通常情况下,你应该抹去整件事,然后用你想要的角度画一条新的线。不过,您可能希望了解Graphics2D(),尤其是setTransform和AffineTransform类。

我想您谈论的是Java AWT图形类。图形可以看作是画布。这是一个像素值数组,“画一条线”只是一个实用函数,可以改变其中一些像素的值——从它的角度来看,没有什么“线对象”可以说。通常情况下,你应该抹去整件事,然后用你想要的角度画一条新的线。不过,您可能需要查看Graphics2D(),尤其是setTransform和AffineTransform类。

您可以为原始线条创建一个对象。然后,您可以使用获取一个仿射变换,该变换围绕某个点围绕某个角度进行旋转。使用此
AffineTransform
,可以创建旋转的
Line2D
对象进行绘制。因此,您的绘制代码大致如下所示:

protected void paintComponent(Graphics gr) {
    super.paintComponent(gr);
    Graphics2D g = (Graphics2D)gr; 

    // Create the original line, starting at the origin,
    // and extending along the x-axis
    Line2D line = new Line2D.Double(0,0,100,0);

    // Obtain an AffineTransform that describes a rotation
    // about a certain angle (given in radians!), around
    // the start point of the line. (Here, this is the
    // origin, so this could be simplified. But in this
    // form, it's more generic)
    AffineTransform at = 
        AffineTransform.getRotateInstance(
            Math.toRadians(angleInDegrees), line.getX1(), line.getY1());

    // Draw the rotated line
    g.draw(at.createTransformedShape(line));
}
可以为原始直线创建对象。然后,您可以使用获取一个仿射变换,该变换围绕某个点围绕某个角度进行旋转。使用此
AffineTransform
,可以创建旋转的
Line2D
对象进行绘制。因此,您的绘制代码大致如下所示:

protected void paintComponent(Graphics gr) {
    super.paintComponent(gr);
    Graphics2D g = (Graphics2D)gr; 

    // Create the original line, starting at the origin,
    // and extending along the x-axis
    Line2D line = new Line2D.Double(0,0,100,0);

    // Obtain an AffineTransform that describes a rotation
    // about a certain angle (given in radians!), around
    // the start point of the line. (Here, this is the
    // origin, so this could be simplified. But in this
    // form, it's more generic)
    AffineTransform at = 
        AffineTransform.getRotateInstance(
            Math.toRadians(angleInDegrees), line.getX1(), line.getY1());

    // Draw the rotated line
    g.draw(at.createTransformedShape(line));
}

好的,你需要计算线的长度,假设线的末端是(x0,y0)和(x1,y1),并且(x,y)是鼠标坐标,你想要的是(x0,y0)和(x,y)之间的线上的点(x2,y2),(x0,y0)和(x2,y2)之间的距离必须与(x0,y0)和(x1,y1)之间的距离相同

(x0,y0)和(x1,y1)之间的距离为:

(x0,y0)和(x,y)之间的距离为:

和(x2,y2)是:


好的,你需要计算线的长度,假设线的末端是(x0,y0)和(x1,y1),并且(x,y)是鼠标坐标,你想要的是(x0,y0)和(x,y)之间的线上的点(x2,y2),(x0,y0)和(x2,y2)之间的距离必须与(x0,y0)和(x1,y1)之间的距离相同

(x0,y0)和(x1,y1)之间的距离为:

(x0,y0)和(x,y)之间的距离为:

和(x2,y2)是:


是否要围绕其一端旋转直线?或围绕任意点旋转直线?直线是直的(平行于x轴)我想让第二个点根据角度移动一定量,我的意思是围绕它的一端是的,你想围绕它的一端旋转这条线吗?还是围绕某个任意点旋转?这条线是直的(平行于x轴)我想让第二个点,根据角度,移动一定的量,我的意思是,绕着它的一个端点移动。“删除”行通常不是一个好主意。AWT/Swing中的绘制机制表明,线条应该已经在旋转状态下绘制。是的,这就是我想要说的。我的意思是擦除整个画布并重新绘制。“删除”行通常不是一个好主意。AWT/Swing中的绘制机制表明,线条应该已经在旋转状态下绘制。是的,这就是我想要说的。我的意思是擦除整个画布并重新绘制。
double dx1 = x-x0;
double dy1 = y-y0;
double mouseDist = Math.sqrt(dx1*dx1, dy1*dy1);
int x2 = x0 + (int)(dx1*length/mouseDist);
int y2 = y0 + (int)(dy1*length/mouseDist);
static Point rotateLineClockWise(Point center, Point edge, int angle) {
    double xRot = (int) center.x + Math.cos(Math.toRadians(angle)) * (edge.x - center.x) - Math.sin(Math.toRadians(angle)) * (edge.y - center.y);
    double yRot = (int) center.y + Math.sin(Math.toRadians(angle)) * (edge.x - center.x) + Math.cos(Math.toRadians(angle)) * (edge.y - center.y);
    return new Point((int) xRot, (int) yRot);
}