在 Java 中调整表单对象的大小
我有一个 Java 小程序,它是一种可以在其中绘制形状的表单(矩形、椭圆形、直线)。 每个形状由 2 个点表示,并且可以将自身绘制到形状中。 当 JApplet 表单调整大小时,我还需要调整形状大小,同时保持纵横比。
我没有找到解决此问题的高质量解决方案。
尝试编写一个解决方案,但在测试时发现它很糟糕,有人可以发布一个示例代码来做到这一点吗?
I have a Java applet, which is a form that draw shapes into it (Rect, Oval, Line).
Each shape is represented by 2 points and can draw itself to the form.
When the JApplet form resizes, I need to resize the shapes also while keeping the aspect ratio.
I didn't find an high quality solution for doing this that solves this problem.
Tried to write a solution from this, but it came up as lousy when tested, Can someone publish an example code for doing that please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提供任何细节,但我猜测当您缩放时,对象会移动并缩放。您需要了解一些有关图形转换的知识。主要有两种方法:
通过缩放坐标来调整对象大小
对对象应用变换矩阵。这可能是更好的方法。
如果您使用的是 Graphics2D (http:// java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics2D.html)您将可以访问转换。
当您调整窗口大小时,您将需要计算出哪个尺寸(宽度或高度)对比例影响最大,并采用其他尺寸 - 这将阻止对象过度膨胀。计算单个比例并将其平均应用于 X 和 Y。
您可能不会在原点上看到对象。在这种情况下,您需要执行三个操作:
- 将对象平移到原点
- 重新调整它的大小
- 将其转换回原来的位置
这些操作可以组合成一个矩阵。
我不能更明确,因为我不知道你的问题的细节,但这应该给你一些指示。
尝试以下示例:
http://www.java2s.com/Code/Java/ 2D-图形-GUI/Linetransformationrotationshearscale.htm
这应该会向您展示一些效果。
You give no details but I am guessing that when you scale the object moves as well as scales. You will need to know something about graphics transformations. There are two main approaches:
resize the object by scaling its coordinates
apply a transformation matrix to the object. This is probably the better way.
If you are using Graphics2D (http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics2D.html) you will have access to transformations.
When you resize the window you will want to work out which dimension (width or height) has most effect on the scale and take the others one - this will stop the object expanding too much. Calculate a single scale and apply it equally to X and Y.
You will probably not have the object on the origin. In this case you need to carry out three operations:
- translate the object to the origin
- rescale it
- translate it back to where it was
These operations can be combined into a single matrix.
I cannot be more definite as I do not know the details of your problem but this should give you some pointers.
Try out the examples in:
http://www.java2s.com/Code/Java/2D-Graphics-GUI/Linetransformationrotationshearscale.htm
which should show you some of the effects.