Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.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 简单点变换(Graphics2D)_Java_Graph_Graphics - Fatal编程技术网

Java 简单点变换(Graphics2D)

Java 简单点变换(Graphics2D),java,graph,graphics,Java,Graph,Graphics,我有一个简单的类来表示一个图,目前它只画线。我目前在绘制之前截取线定义,并更改它们的点,但现在我想添加更复杂的形状,而不仅仅是点定义。我知道仿射变换,但我不确定如何使用它。我想要一个等价于以下内容的转换: private Point transform(PlasmaPoint2D plasmaPoint2D) { double x = plasmaPoint2D.getX(); double y = plasmaPoint2D.getY();

我有一个简单的类来表示一个图,目前它只画线。我目前在绘制之前截取线定义,并更改它们的点,但现在我想添加更复杂的形状,而不仅仅是点定义。我知道仿射变换,但我不确定如何使用它。我想要一个等价于以下内容的转换:

    private Point transform(PlasmaPoint2D plasmaPoint2D) {
        double x = plasmaPoint2D.getX();
        double y = plasmaPoint2D.getY();
        Point p = new Point();
        p.setLocation(x * this.gridScale / this.scale + this.gridScale, y * this.gridScale / this.scale + this.gridScale);
        return p;
    }
(其中PlasmaPoint2D是我自己的、不可变的point2d版本)。gridScale是一个实例变量,指定每条网格线之间的像素数,scale是由单个网格框表示的单位数


我不知道如何在AffineTransform中实现这一点,因此非常感谢您的帮助。

当从“变换”菜单中选择变换时,该变换将连接到AffineTransform上:

public void setTrans(int transIndex) {
    // Sets the AffineTransform.
    switch ( transIndex ) {
    case 0 :
        at.setToIdentity();
        at.translate(w/2, h/2);
        break;
    case 1 :
        at.rotate(Math.toRadians(45));
        break;
    case 2 :
        at.scale(0.5, 0.5);
        break;
    case 3 :
        at.shear(0.5, 0.0);
        break;
    }
}
在显示与菜单选项对应的形状之前,应用程序首先从Graphics2D对象检索当前变换:

AffineTransform saveXform = g2.getTransform();
检索当前变换后,将创建另一个仿射变换,即ToCenter,以使形状在面板的中心进行渲染。at仿射变换被连接到ToCenter上:

AffineTransform toCenterAt = new AffineTransform();
toCenterAt.concatenate(at);
toCenterAt.translate(-(r.width/2), -(r.height/2));
ToCenter变换通过变换方法连接到Graphics2D变换:

g2.transform(toCenterAt);
g2.setTransform(saveXform);
渲染完成后,使用setTransform方法恢复原始变换:

g2.transform(toCenterAt);
g2.setTransform(saveXform);

假设我有一条四元曲线:

QuadCurve2D.Double c2=new QuadCurve2D.Double(p[j][0], p[j][1], 
  p[j+1][0], p[j+1][1], p[j+2][0], p[j+2][1]);
int translatebyx=x * this.gridScale / this.scale + this.gridScale-c2.getX1(),
    translatebyy=y * this.gridScale / this.scale + this.gridScale-c2.getY1(), 
  AffineTransform at=AffineTransform.getTranslateInstance(translatebyx, translatebyy);
  Shape s=at.createTransformedShape(c2);
  g2.draw(s);
在这里,我要转换到您指定的给定位置

x * this.gridScale / this.scale + this.gridScale,
y * this.gridScale / this.scale + this.gridScale

如果要执行旋转,请应用相同的过程(使用getRotateInstance())等。

affinetransform实际上并不移动点本身,它会告诉图形对象如何通过旋转绘制点。是否有方法设置移动点行为?直线和圆弧。我很想知道如何转换它们,但我更希望有一个通用的解决方案。这听起来像是其他类的描述,我不太明白。