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());
}
}
但它不起作用...有什么建议吗?
I'm trying to scale a rectangle from its center using AffineTransform. I'm sure the solution is obvious but I cannot make it work ! Here is what I've tested so far...
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());
}
}
But it doesn't work.... Any suggestion ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我明白你在处理矩形时的意思。 原因是因为平移的初始计算没有考虑容器对象的大小。
请使用它:
它的作用是在缩放之前将矩形平移到面板的中心。 在我的测试中它工作得很好。
I see what you mean when you're dealing with rectangles. The reason is because the initial calculation for the translation didn't take into account the size of the container object.
Use this instead:
What this is doing is translating the rectangle to the center of the panel before scaling it. In my tests it works just fine.
假设缩放固定了矩形左上角的位置(我认为这是正确的,但自从我用 Java 完成图形以来已经很长时间了),您需要沿与缩放相反的方向平移矩形。
因此,您将矩形向左和向上移动宽度和高度变化的一半。
Assuming scaling fixes the location of the top lefthand corner of the rectangle (which I think is right but it's been a long time since I've done graphics in Java), you need to translate the rectangle in the direction opposite to the scaling.
So you move the rectangle left and up half of the change in width and height.
我正在开发一个桌面应用程序来裁剪 Brittney Spear 的脸(白天)裁剪矩形必须围绕其中心点缩放:
一般来说,该算法的工作原理如下:
x + (width * (1 - ZoomFactor) / 2)
y + (height * (1 - ZoomFactor) / 2)
width * ZoomFactor
height * ZoomFactor
I was just working on a desktop application to crop Brittney Spear's face (D.A.Y.) The cropping rectangle had to scale around its center point:
In general, the algorithm works like this:
x + (width * (1 - zoomFactor) / 2)
y + (height * (1 - zoomFactor) / 2)
width * zoomFactor
height * zoomFactor
这涉及一个称为共轭变换的过程。
如果 S 是您想要执行的缩放,而 T 是将点 (0,0) 带到要成为缩放中心的点的变换,则执行此操作的变换是
T(S(inverse( )))
This involves a process called conjugating a transform.
If S is the scaling you want to do, and T is the transformation that takes point (0,0) to the point that is to be the center of your scaling, then the transform that does the job is
T(S(inverse(T)))
(稍后)这是一个无需事先了解面板尺寸即可工作的解决方案。
(Later) Here is a solution working without any prior knowledge of the Dimension of the panel.