Reputation: 101
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 );
}}
Upvotes: 1
Views: 1515
Reputation: 168795
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.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
public class TestRotate
{
public static void main(String args[])
{
JFrame frame = new JFrame("Test Rotate");
JPanel gui = new JPanel(new BorderLayout());
final HousePanel housePanel = new HousePanel();
gui.add(housePanel, BorderLayout.CENTER);
final JCheckBox transform = new JCheckBox("Transform");
transform.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
housePanel.setTransform(transform.isSelected());
housePanel.repaint();
}
});
gui.add(transform, BorderLayout.NORTH);
frame.add( gui );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
gui.setPreferredSize( new Dimension(300, 200) );
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible( true );
}
}
class HousePanel extends JPanel {
public int[] HouseX = {
100,105,110,105,100,95
};
public int[] HouseY = {
100,
100,
(int)(100+(5*(Math.sqrt(3))/2)),
(int)(100+(5*(Math.sqrt(3)))),
(int)(100+(5*(Math.sqrt(3)))),
(int)(100+(5*(Math.sqrt(3))/2))
};
boolean transform = false;
public void setTransform(boolean transform) {
this.transform = transform;
}
public void paintComponent( Graphics g )
{
final Graphics2D g2 = (Graphics2D) g;
g2.clearRect( 0, 0, this.getWidth(), this.getHeight() );
g2.setColor( Color.BLACK );
if (transform) {
AffineTransform transform = new AffineTransform();
transform.scale( -1.0, 1.0 );
g2.setTransform( transform );
}
g2.drawPolygon(HouseX, HouseY, 6);
}
}
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.
Upvotes: 3