1

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 );
     }}
pascal
  • 101
  • 1
  • 11
  • How is the pentahex represented? – Ted Hopp Nov 20 '11 at 01:45
  • *"..I am not sure how to use it. Any small example would be of great help."* That is where 'any small attempt' (e.g. an [SSCCE](http://sscce.org/) of your best effort to flip any construct that will look different when flipped), greatly helps to motivate people to investigate your question. – Andrew Thompson Nov 20 '11 at 01:58
  • 2
    There are plenty of examples of AffineTransform available on the web. why not try to use it and if it doesn't work, post your attempt? – Hovercraft Full Of Eels Nov 20 '11 at 02:33
  • is it possible to upload an image as an example? – pascal Nov 20 '11 at 02:34
  • *"is it possible to upload an image..?"* Yes if you have enough rep., but don't do that. None of the people capable of helping need to see a screenshot in order to be able to assist. They are more likely to need a) code, & b) a specific question on the code. – Andrew Thompson Nov 20 '11 at 02:41
  • Good call on posting code. Unfortunately here the code does not compile, so it is not a good description of the problem (not an SSCCE of a run-time problem). Can you change that? Note also that we do not get informed of edits to a question. If you edit one, it is best to let people know with a comment starting @name .. – Andrew Thompson Nov 20 '11 at 05:40
  • ok. @Andrew:does the compiler throw a error? or is it some other issue? if yes,what is the error? i ran the code before posting.ok, i have changed the code.Please have a look at it.I am able to draw a single hexagon,now what i want to do is just apply simple transforms mostly rotate and flip,even if i get it done on a single hexagon it would be good. I am posting a new code.Thanks – pascal Nov 20 '11 at 05:57
  • I started playing with your old code before I saw your new code or comments. Try my example & see if that gives you any ideas. Part of the reason I say that is that I 'fixed' a number of other things beyond the compilation error, and could not be bothered fixing them again. ;) – Andrew Thompson Nov 20 '11 at 06:08

1 Answers1

3

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.

Diamond Here Diamond Gone

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.

  1. When a shape is flipped horizontally, it gets drawn to the left of the viewable (visible) area of the component.
  2. When a shape is flipped vertically, it gets drawn above the viewable area.

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    +1 See also [`AffineTest`](http://stackoverflow.com/questions/5593066/rotating-a-shape-vertically-around-the-x-axis/5594424#5594424). – trashgod Nov 20 '11 at 07:54
  • 1
    @trashgod *"Absent a clear question, .."* Is it that nobody who asks about transformations is able to frame a clear question, or that we are so eager to play with them what we don't need one? ;) – Andrew Thompson Nov 20 '11 at 08:12
  • @Andrew: Excellent point; some APIs are more intuitive than others. Such examples have helped my mental model of `AffineTransform` to evolve. I think the same applies to `AlphaComposite`. – trashgod Nov 20 '11 at 17:21