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 );
}}
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.