Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6831047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:41:28+00:00 2026-05-26T22:41:28+00:00

i am trying to flip a 2d object any object. Is it possible to

  • 0

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 );
     }}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:41:28+00:00Added an answer on May 26, 2026 at 10:41 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to cast kx.c class flip object to a string: String test
I am simply trying to flip the bits of a character. I can get
I'm trying to flip/mirror an image as I paint it on an HTML canvas;
On Android, I am trying to create a dynamic image viewer that flip images
I'm trying to write a coin flip program where I can analyze the percentage
I'm trying to flip between two views. That's easy, the code is below, but
I'm trying to flip a bit field in SQL Server using an update query,
I'm trying to implement a flip animation to be used in board game like
I'm trying to get a basic flip animation transition working when I push a
Trying to parse an HTML document and extract some elements (any links to text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.