Getting coordinates of points in Shape object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: Getting coordinates of points in Shape object

  1. #1
    Join Date
    Apr 2005
    Posts
    18

    Question Getting coordinates of points in Shape object

    I was wondering if it's possible to get the coordinates of the points that make up a Shape object.

    These shapes are polygons with straight edges and use float precision (I make them out of a GeneralPath). I just wanted to know because I'm rotating these shapes to various angles and want to get the point coordinates after rotation.

    Thank you.

    [UPDATE]
    I figured out how to get the vertices of a Shape object using a PathIterator. I got it from here under "Understanding Shapes Through GeneralPath and the PathIterator Interfaces."
    This is how you do it:
    Code:
    // Gets the vertices of a shape.  By Aaron J. M. (May 15, 2005).
    public Point2D.Float[] returnVertices(Shape s) {
       // Initialize Point2D object array that will store coordinates of shape's vertices.
       Point2D.Float[] coords = new Point2D.Float[vNum]; // vNum is the number of vertices shape has.
       for (int x = 0; x < coords.length; x++)
          coords[x] = new Point2D.Float();
       
       // Create PathIterator using a "blank" AffineTransform object.		
       PathIterator p = s.getPathIterator(new AffineTransform());
       
       // Finally get the coordinates and put them into coords array.		
       for (int i = 0; i < coords.length; i++) {
          float[] pCoords = new float[6];
          p.currentSegment(pCoords); // Will put values pertinent to current segment into pCoords.
          p.next();
          
          // First two values of pCoords are coordinates of vertex.
          coords[i].setLocation(pCoords[0], pCoords[1]);
       }
    		
       return coords;
    }
    Last edited by AaronJM; 05-15-2005 at 08:57 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center