【问题标题】:Shape modified by AffineTransform is rendering reverse from expectationsAffineTransform 修改的形状与预期相反
【发布时间】:2013-03-06 04:30:13
【问题描述】:

我有一些几何形状,它们位于不同的坐标中,我想将它们带到面板的中心。我编写了一个名为“Set Scale”的函数,将其置于中心位置,但形状颠倒了。

我的代码:

private void SetScale1(Graphics2D gr, int gr_width , int gr_height, double left_x , double right_x , double top_y , double bottom_y ){


    Rectangle2D drawing_rect = new Rectangle2D.Double(left_x, top_y, right_x - left_x, bottom_y - top_y);
 double drawing_cx=( left_x+ right_x) / 2;
 double drawing_cy =(top_y + bottom_y) / 2;

AffineTransform at = AffineTransform.getTranslateInstance(-1 * drawing_cx, -1 * drawing_cy);
//gr.translate(-1 * drawing_cx, -1 * drawing_cy);
//gr.TranslateTransform(0, 0)
double scale_x=gr_width / drawing_rect.getWidth();
double scale_y=gr_height / Math.abs(drawing_rect.getHeight());

scale_x = Math.min(scale_x, scale_y);
scale_y = scale_x;
scale_x = Math.abs(scale_x);
// at = AffineTransform.getScaleInstance(scale_x, -1 * scale_y);
//gr.transform(tt);
gr.transform(at);

//' Translate to center over the drawing area.
double graphics_cx =gr_width / 2;
double graphics_cy = gr_height / 2;
gr.translate(graphics_cx, graphics_cy);
}

【问题讨论】:

    标签: java matrix transformation coordinate-systems affinetransform


    【解决方案1】:

    根据给出的描述,我建议你看看你的转换矩阵:

    AffineTransform at = AffineTransform.getTranslateInstance(-1 * drawing_cx, -1 * drawing_cy);
    

    您确定您的 tx 和 ty 参数正确吗?这是getTranslationInstance返回的矩阵:

    [   1    0    tx  ]
    [   0    1    ty  ]
    [   0    0    1   ]
    

    您在getTranslationInstance 的参数中设置的-1 值对我来说似乎很可疑......除非我错过了什么,否则我看不出它们应该为负数的任何理由。负值应该mirror the result,所以这很可能就是你看到反转的原因。尝试更改这些(它们将分别更改上面矩阵中的 tx 和 ty 字段)并告诉我们是否有帮助。

    【讨论】:

      【解决方案2】:

      我试过这个......它有效:-)

      感谢您的帮助:-)

      Rectangle2D drawing_rect = new Rectangle2D.Double(left_x, top_y, right_x - left_x, bottom_y - top_y); 绘图_cx=(left_x+ right_x) / 2; 绘图_cy =(top_y + bottom_y) / 2;

       at= AffineTransform.getTranslateInstance( -1* drawing_cx,  -1*drawing_cy);
      //gr.translate(-1 * drawing_cx, -1 * drawing_cy);
      //gr.TranslateTransform(0, 0)
      double scale_x=gr_width / drawing_rect.getWidth();
      double scale_y=gr_height / Math.abs(drawing_rect.getHeight());
      
      scale_x = Math.min(scale_x, scale_y);
      scale_y = scale_x;
      scale_x = Math.abs(scale_x);
      // at = AffineTransform.getScaleInstance(scale_x, -1 * scale_y);
      
      gr.scale(Math.round(scale_x), Math.round( scale_y));
      //gr.scale(1.2, 1.2);
      //gr.transform(tt);
      gr.transform(at);
      
      //' Translate to center over the drawing area.
       graphics_cx =gr_width / 2;
       graphics_cy = gr_height / 2;
      gr.translate(graphics_cx, graphics_cy);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 2022-10-08
        • 2021-12-12
        • 1970-01-01
        • 1970-01-01
        • 2013-07-08
        相关资源
        最近更新 更多