3

パネルの中央に配置したいさまざまな座標の幾何学的形状がいくつかあります。中央に持ってくる「SetScale」という関数を書いたのですが、形が逆になっています。

私のコード:

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);
}
4

2 に答える 2

0

与えられた説明から、変換行列を確認することをお勧めします。

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

txおよびtyパラメーターが正しいことを確認しますか?getTranslationInstanceによって返されるマトリックスは次のとおりです。

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

パラメータに設定した-1の値は、getTranslationInstance私には疑わしいようです...何かを見逃していない限り、それらが負になる理由はわかりません。負の値は結果を反映している必要があるため、逆転が見られるのはそのためかもしれません。それらを変更してみて(上記のマトリックスのtxフィールドとtyフィールドをそれぞれ変更します)、それが役立つかどうかをお知らせください。

于 2013-03-06T04:35:50.283 に答える
0

私はこれを試しました...それは動作します:-)

助けてくれてありがとう:-)

Rectangle2D drawing_rect = new Rectangle2D.Double(left_x, top_y, right_x - left_x, bottom_y - top_y); drawing_cx=(left_x+right_x) / 2; drawing_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);
于 2013-03-14T05:53:18.027 に答える