AffineTransform:从中心缩放形状

时间:2009-03-27 18:08:40

标签: java swing geometry shape affinetransform

我正在尝试使用AffineTransform从其中心缩放矩形。我确信解决方案很明显,但我不能让它发挥作用!这是我到目前为止测试过的......

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Test extends JPanel {
    Test()
        {
        super(null);
        setOpaque(true);
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(200,200));
        }
    @Override
    protected void paintComponent(Graphics g1) {
        super.paintComponent(g1);
        Rectangle r= new Rectangle(5,5,getWidth()-10,getHeight()-10);
        double cx= r.getCenterX();
        double cy= r.getCenterY();
        Graphics2D g=(Graphics2D)g1;
        g.setColor(Color.BLACK);
        AffineTransform old= g.getTransform();
        for(double zoom=0.9; zoom>=0.5; zoom-=0.1)
            {
            AffineTransform tr2= new AffineTransform(old);
            tr2.translate(-cx, -cy);
            tr2.scale(zoom, zoom);
            tr2.translate(cx/zoom,cy/zoom);
            g.setTransform(tr2);
            g.draw(r);
            g.setTransform(old);
            }
        }


    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, new Test());
        }
    }

但它不起作用......有什么建议吗?

5 个答案:

答案 0 :(得分:5)

假设缩放修正了矩形左上角的位置(我认为这是正确的,但是自从我用Java完成图形以来已经很长时间了),你需要将矩形转换为与缩放。

tr2.translate(
    r.getWidth()*(1-zoom)/2,
    r.getHeight()*(1-zoom)/2
);
tr2.scale(zoom,zoom);
g.setTransform(tr2);

所以你将矩形向左和向上移动一半的宽度和高度变化。

答案 1 :(得分:5)

当你处理矩形时,我明白了你的意思。原因是因为翻译的初始计算没有考虑容器对象的大小。

请改用:

tr2.translate(
    (this.getWidth()/2) - (r.getWidth()*(zoom))/2,
    (this.getHeight()/2) - (r.getHeight()*(zoom))/2
);
tr2.scale(zoom,zoom);
g.setTransform(tr2);

这样做是在缩放之前将矩形平移到面板的中心。在我的测试中,它运作得很好。

答案 2 :(得分:1)

我正在研究桌面应用程序来裁剪Brittney Spear的脸(D.A.Y.)裁剪矩形必须围绕其中心点进行缩放:

import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;

class ResizableRectangle extends Rectangle {

 ResizableRectangle(double x, double y, double width, double height, Group group) {

  super(x, y, width, height);

  // Set scroll listener for crop selection
  group.addEventHandler(ScrollEvent.SCROLL, event -> {
   double zoomFactor = 1.10;
   double deltaY = event.getDeltaY();
   if (deltaY > 0) {
    zoomFactor = 2.0 - zoomFactor;
   }

   super.setX(getX() + (super.getWidth() * (1 - zoomFactor) / 2)); // Set new X position
   super.setWidth(getWidth() * zoomFactor); // Set new Width

   super.setY(getY() + (super.getHeight() * (1 - zoomFactor) / 2)); // Set new Y position
   super.setHeight(getHeight() * zoomFactor); // Set new Height

   event.consume();
  });
 });
}

通常,算法的工作方式如下:

  • 使用:x + (width * (1 - zoomFactor) / 2)
  • 翻译矩形x值
  • 使用以下内容翻译y值:y + (height * (1 - zoomFactor) / 2)
  • 将新宽度设置为:width * zoomFactor
  • 将新高度设置为:height * zoomFactor

答案 3 :(得分:0)

这涉及一个称为共轭变换的过程。

如果S是您想要的缩放,并且T是将点(0,0)转换为缩放中心的点的变换,那么执行该作业的变换是

T(S(倒数(T)))

答案 4 :(得分:0)

(稍后)这是一个解决方案,在没有事先了解面板尺寸的情况下工作。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Test extends JPanel
    {
    private static final long serialVersionUID = 1L;
    private Test()
        {
        super(null);
        setOpaque(true);
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(600,600));
        }

    @Override
    protected void paintComponent(Graphics g1) {
        super.paintComponent(g1);
        Shape r= new Ellipse2D.Double(5,380,400,200);
        double cx= r.getBounds2D().getCenterX();
        double cy= r.getBounds2D().getCenterY();
        Graphics2D g=(Graphics2D)g1;
        g.setColor(Color.BLACK);
        AffineTransform old= g.getTransform();
        g.drawLine((int)cx-10, (int)cy, (int)cx+10, (int)cy);
        g.drawLine((int)cx, (int)cy-10, (int)cx, (int)cy+10);
        for(double zoom=1; zoom>=0.1; zoom-=0.1)
                {


                AffineTransform tr2 =AffineTransform.getTranslateInstance(-cx, -cy);
                AffineTransform  tr= AffineTransform.getScaleInstance(zoom,zoom);
                tr.concatenate(tr2); tr2=tr;
                tr =AffineTransform.getTranslateInstance(cx, cy);
                tr.concatenate(tr2); tr2=tr;

                tr= new AffineTransform(old);
                tr.concatenate(tr2);  tr2=tr;

                g.setTransform(tr2);



                g.draw(r);
                g.setTransform(old);
                }
        }


    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, new Test());
        }
    }