在java中翻转2d组件
我正在尝试翻转任何对象的二维对象。在Java中可以这样做吗?如果他们在不同的象限,我可以改变他们的象限吗?这与我们在画图中翻转图像的操作类似。我试图在 Java 中执行相同的实用程序。我听说过仿射变换,其中它使用了一个名为 TYPE_FLIP 的位,但我不确定如何使用它。任何小例子都会有很大帮助。注意:我不想翻转图像,而是翻转实际的 2D 对象。 这样就可以用仿射变换了。
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
public class TestRotate extends JFrame
{
public void paint( Graphics g )
{
super.paintComponents(g);
AffineTransform saveTransform;
int[] HouseX = {100,150,200,150,100,50};
int[] HouseY = {100,100,(int)(100+(40*(Math.sqrt(3))/2)),(int)(100+(40*(Math.sqrt(3)))),(int)(100+(40*(Math.sqrt(3)))),(int)(100+(40*(Math.sqrt(3))/2))};
Graphics2D g2 = ( Graphics2D ) g;
g.setColor( Color.BLACK );
g.drawPolygon(HouseX, HouseY, 6);
saveTransform = g2.getTransform();
AffineTransform transform = new AffineTransform();
transform.scale( 1.0, -1.0 );
g2.setTransform( transform );
g2.setColor( Color.BLUE );
g.drawPolygon(HouseX, HouseY, 6);
transform.rotate( Math.toRadians( 45 ) );
g2.setTransform( transform );
g2.setColor( Color.GREEN );
g.drawPolygon(HouseX, HouseY, 6);
}
public static void main(String args[])
{
TestRotate frame = new TestRotate();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 600, 500 );
frame.setVisible( true );
}}
i am trying to flip a 2d object any object. Is it possible to do so in Java? if they r in separate quadrant can I change their quadrant? It is similar to what we do to flip images in Paint. The same utility I am trying to perform in Java. I have heard about Affine transform wherein it makes use of a bit called TYPE_FLIP, but I am not sure how to use it. Any small example would be of great help. Note: I do not want to flip images, but actual 2D objects.
In this way with Affine Transform.
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
public class TestRotate extends JFrame
{
public void paint( Graphics g )
{
super.paintComponents(g);
AffineTransform saveTransform;
int[] HouseX = {100,150,200,150,100,50};
int[] HouseY = {100,100,(int)(100+(40*(Math.sqrt(3))/2)),(int)(100+(40*(Math.sqrt(3)))),(int)(100+(40*(Math.sqrt(3)))),(int)(100+(40*(Math.sqrt(3))/2))};
Graphics2D g2 = ( Graphics2D ) g;
g.setColor( Color.BLACK );
g.drawPolygon(HouseX, HouseY, 6);
saveTransform = g2.getTransform();
AffineTransform transform = new AffineTransform();
transform.scale( 1.0, -1.0 );
g2.setTransform( transform );
g2.setColor( Color.BLUE );
g.drawPolygon(HouseX, HouseY, 6);
transform.rotate( Math.toRadians( 45 ) );
g2.setTransform( transform );
g2.setColor( Color.GREEN );
g.drawPolygon(HouseX, HouseY, 6);
}
public static void main(String args[])
{
TestRotate frame = new TestRotate();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 600, 500 );
frame.setVisible( true );
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码并未提供完整的答案,但旨在让您思考。您应该考虑的是“菱形去哪里了?”。尝试调整变换缩放中使用的数字,看看是否可以解决A。
A) 好的.. 基本问题是,对于您正在执行的变换类型,通常需要结合比例 和翻译。
原因是这样的。
使用比例完成“翻转”翻译后,concatenate() 与 翻译() 变换将形状移回到可视区域。
如果您知道如何操作,这些组合变换就会很容易。我不知道怎么办。
This code does not provide a complete answer, but is designed to get you thinking. What you should be thinking about, is "Where does the diamond shape go to?". Try adjusting the numbers used in the scaling of the transform, and see if you can figure it outA.
A) OK.. The basic problem is that for the type of transform that you are doing, it is usually necessary to combine both a scale & a translate.
The reason is that.
After the 'flip' translate is done using scale, concatenate() that with a translate() transform to move the shape back into the viewable area.
These combined transforms are easy if you know how. I don't know how.