Results 1 to 6 of 6
  1. #1
    dorairaj is offline Member
    Join Date
    Jan 2009
    Location
    Hyderabad,India.
    Posts
    13
    Rep Power
    0

    Default how to select Shape object after rotation.

    Hi aLL,
    I have a problem while selecting a Shape Object after rotation has made to that when mouse clicked on that object. I am able to select a shape(Rectangle) Object without rotation(0 Degrees) using contains method in the Shape Object. Pls help me... how to select the Shape Object.
    Thanks in Advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    My thought was this -- when testing to see if a point is contained by a shape, first re-transform the Shape, then test it. You can transform the shape without a graphics object via:

    AffineTransform#createTransformedShape(Shape s)

    So, I've created a MyShape class that holds a Shape object and a boolean filled variable that tells if the Shape should be filled or not. I've given this class methods to allow the shape to be drawn, filled, and also two contains method, one taking a Point2D paramter is a straightforward calling of the Shape's own contain method, the other takes a Point2D and an AffineTransform parameters and first rotates the Shape, then checks whether it contains the point.

    Here are my classes:

    RotateShape.java
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class RotateShape {
       private static final int PANEL_WIDTH = 500;
       private static final int SHAPE_WIDTH = 100;
       private static final Dimension MAIN_SIZE = 
          new Dimension(PANEL_WIDTH, PANEL_WIDTH);
       private static final int SLIDER_MIN = -180;
       private static final int SIDER_MAX = 180;
       private static final int MAJOR_TICK = 30;
       private static final int MINOR_TICK = 15;
    
       private List<MyShape> myShapeList = new ArrayList<MyShape>();
       private JPanel mainPanel = new JPanel();
       private JPanel drawingPanel = createDrawingPanel();
       private AffineTransform transform = new AffineTransform();
       private JSlider rotationSlider;
    
       public RotateShape() {
          mainPanel.setLayout(new BorderLayout());
          mainPanel.add(drawingPanel, BorderLayout.CENTER);
          mainPanel.add(createTransformPanel(), BorderLayout.SOUTH);
       }
    
       private JPanel createTransformPanel() {
          rotationSlider = new JSlider(SLIDER_MIN, SIDER_MAX, 0);
          rotationSlider.setMajorTickSpacing(MAJOR_TICK);
          rotationSlider.setMinorTickSpacing(MINOR_TICK);
          rotationSlider.setPaintLabels(true);
          rotationSlider.setPaintTicks(true);
          rotationSlider.setPaintTrack(true);
          rotationSlider.setSnapToTicks(true);
          rotationSlider.addChangeListener(new SliderListener());
    
          JPanel transformingPanel = new JPanel(new BorderLayout());
          transformingPanel.setBorder(BorderFactory.createEtchedBorder());
          transformingPanel.add(rotationSlider);
          return transformingPanel;
       }
    
       @SuppressWarnings("serial")
       private JPanel createDrawingPanel() {
          if (drawingPanel != null) {
             return drawingPanel;
          }
          drawingPanel = new JPanel() {
             @Override
             protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                myPaint(g);
             }
          };
          int leftUpperCenter = PANEL_WIDTH / 4;
          int x = leftUpperCenter - SHAPE_WIDTH / 2;
          int y = x;
          MyShape rect = new MyShape(
                new Rectangle2D.Double(x, y, SHAPE_WIDTH, SHAPE_WIDTH));
          MyShape circle = new MyShape(
                new Ellipse2D.Double(PANEL_WIDTH - x - SHAPE_WIDTH, 
                      y, SHAPE_WIDTH, SHAPE_WIDTH));
          MyShape roundedRect = new MyShape(new RoundRectangle2D.Double(x,
                PANEL_WIDTH - y - SHAPE_WIDTH, SHAPE_WIDTH, SHAPE_WIDTH, 30, 30));
          myShapeList.add(rect);
          myShapeList.add(circle);
          myShapeList.add(roundedRect);
    
          drawingPanel.setPreferredSize(MAIN_SIZE);
          drawingPanel.addMouseListener(new MouseAdapter() {
             public void mousePressed(MouseEvent e) {
                myMousePressed(e);
             }
          });
    
          return drawingPanel;
       }
    
       private void myMousePressed(MouseEvent e) {
          for (MyShape myShape : myShapeList) {
             if (myShape.contains(e.getPoint(), transform)) {
                // toggle fill if shape is clicked
                myShape.setFilled(!myShape.isFilled());
             }
          }
          drawingPanel.repaint();
       }
    
       private void myPaint(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g2.setStroke(new BasicStroke(3));
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
          AffineTransform oldTransform = g2.getTransform();
          if (transform != null) {
             g2.setTransform(transform);
          }
          g2.setColor(Color.red);
          for (MyShape myShape : myShapeList) {
             if (myShape.isFilled()) {
                myShape.fill(g2);
             }
          }
          g2.setColor(Color.blue);
          for (MyShape myShape : myShapeList) {
             myShape.draw(g2);
          }
    
          g2.setTransform(oldTransform);
       }
    
       public JComponent getPanel() {
          return mainPanel;
       }
    
       private class SliderListener implements ChangeListener {
          public void stateChanged(ChangeEvent e) {
             double theta = Math.PI * rotationSlider.getValue() / 180;
             double center = (double) PANEL_WIDTH / 2;
             transform = AffineTransform.getRotateInstance(theta, center, center);
             drawingPanel.repaint();
          }
       }
    
       private static void createAndShowGUI() {
          JFrame frame = new JFrame("RotateShape Application");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new RotateShape().getPanel());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGUI();
             }
          });
       }
    }
    MyShape.java
    Java Code:
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Point2D;
    
    /**
     * Holds a Shape and a boolean filled that tells if Shape is to be filled or not
     */
    class MyShape {
       private Shape shape;
       private boolean filled = false;
    
       public MyShape(Shape shape) {
          this.shape = shape;
       }
    
       public boolean isFilled() {
          return filled;
       }
    
       public void setFilled(boolean filled) {
          this.filled = filled;
       }
    
       public Shape getShape() {
          return shape;
       }
    
       public void draw(Graphics2D g2) {
          g2.draw(shape);
       }
    
       public void fill(Graphics2D g2) {
          g2.fill(shape);
       }
    
       public boolean contains(Point2D p) {
          return shape.contains(p);
       }
    
       /**
        * 
        * @param p Point2D to test
        * @param at AffineTransform -- to move shape
        * @return true if point contained by shape after transformation
        */
       public boolean contains(Point2D p, AffineTransform at) {
          Shape s = at.createTransformedShape(shape);
          return s.contains(p);
       }
    }

  4. #4
    dorairaj is offline Member
    Join Date
    Jan 2009
    Location
    Hyderabad,India.
    Posts
    13
    Rep Power
    0

    Default Thank U very much....Fubarable

    Hi fubarable,
    Now m able to select a Shape object. But when i transform the graphics object to draw the shape object code is not working fine i hope because of using AffineTransform without interaction of Graphics2D object...right?
    I tried like this

    public boolean contains(Point2D p, AffineTransform at,
    Rectangle2D shape)
    {
    at.translate(origin, origin); //origin means translated position ex:100.
    Shape s = at.createTransformedShape(shape);

    return s.contains(p);
    }

    but its not working fine... how can i proceed......Pls make to overcome this issue.

    Thanks in Advance....

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For us to fully understand your problem, I recommend that you create and post a Short, Self Contained, Correct (Compilable), Example or SSCCE. Please check out the link to see exactly what I mean, but this will allow us to run your code and manipulate your code and then help you come up with a solution.

  6. #6
    dorairaj is offline Member
    Join Date
    Jan 2009
    Location
    Hyderabad,India.
    Posts
    13
    Rep Power
    0

    Default

    Hi Fubarable,
    I Got it(Object selection after translate),
    before passing shape object to contains method i have shifted the x,y axis
    with the translated value and finally i got wt i expected.

    i tried like this,

    Shape shape = new Rectangle2D.Double(x+originSiftedValue,y+originSif tedValue,width,height);

    AffineTransform af1 = AffineTransform.getRotateInstance(Math.toRadians(4 5), rect.getCenterX()+originSiftedValue, rect
    .getCenterY()+originSiftedValue);

    Thank u very much for reply.

Similar Threads

  1. How to retain value in struts 2 using <s:select></s:select> tag
    By SaiPrasad@Sella in forum Web Frameworks
    Replies: 0
    Last Post: 02-09-2009, 07:23 AM
  2. Need help with 2d Array rotation!
    By TrungTran in forum Advanced Java
    Replies: 1
    Last Post: 11-23-2008, 05:35 AM
  3. Log rotation in ubuntu
    By sarah11 in forum New To Java
    Replies: 0
    Last Post: 11-04-2008, 07:46 AM
  4. Rotation in java 3D
    By émilie- in forum Java Applets
    Replies: 0
    Last Post: 02-13-2008, 10:37 AM
  5. affineTransform rotation
    By MichYer in forum AWT / Swing
    Replies: 0
    Last Post: 07-18-2007, 08:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •