简体   繁体   English

java翻译矩形2D

[英]java translate rectangle 2D

I want to translate a rectangle 2D using a double x and double y. 我想用双x和双y翻译一个矩形2D。 It seems that rectangle2D does not support the translate method. 看来rectangle2D不支持translate方法。 What is the appropriate method to use? 适当的使用方法是什么?

If you need it to remain a Rectangle2D, you might have to write your own custom method that just adds to the x and y. 如果你需要它来保持Rectangle2D,你可能必须编写自己的自定义方法,只添加到x和y。

You can also perform arbitrary transformations with an AffineTransform, but this will turn it into a generic Shape (since that transformation might make it no longer rectangular). 您还可以使用AffineTransform执行任意转换,但这会将其转换为通用Shape(因为该转换可能使其不再是矩形)。

AffineTransform at = AffineTransform.getTranslateInstance(tx, ty);
Shape transformed = at.createTransformedShape(rectangle);

You have to cast the old Graphics variable to Graphics2D which has double methods. 您必须将旧的Graphics变量Graphics2D为具有双重方法的Graphics2D Mostly double parameters are used. 大多数情况下使用双参数。

@Override
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g.translate(3.0, 4.0);

Double rectangles are not available in Graphics2D (like in drawRect ). Graphics2D中没有双矩形(如drawRect )。 They are available however for own programming as java.awt.geom.Rectangle2D.Double . 但它们可用于自己编程为java.awt.geom.Rectangle2D.Double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM