Drawing custom shapes (Graphics2D)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Drawing custom shapes (Graphics2D)

  1. #1
    Join Date
    Apr 2005
    Posts
    18

    Question Drawing custom shapes (Graphics2D)

    Hello. I am trying to draw my own defined shapes in Java using Graphics2D. I've being using the Polygon class at first, but I'm later going to want double or float precision. I'm just wondering what class I should use.

    For the shapes I want to draw I define arrays of th x-y coordinates of the points used to draw them, which worked fine for Polygon objects. But since I want better point precision, I've taken a look at the GeneralPath and Shape classes in the Java 2 platform S.E. 5.0 A.P.I. My problem is that I saw no obvious way to specify the points of a shape constructed from either of these classes. I know points in a Shape object are stored in a PathIterator, but I can't tell how to define geometric points with that either. Would there be other classes that I overlooked either?

    Thank you.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    When you draw (using the mouseDragged callback I presume) and add points to the
    drawing line as you go, there is no such thing as e.g. pixel (x:5.5, y:3.14). Their
    positions are integer pairs, I'm not quite sure here, but I can't see any relevance for
    decimal pixels except for zooming.
    eschew obfuscation

  3. #3
    Join Date
    Apr 2005
    Posts
    18
    I'm not drawing these shapes during runtime. I'm defining these shapes in the code.

    I wanted the extra precision because I'm doing a physics-based animation where the shapes move with inertia, rotational motion, etc.

    I'm just wondering about the Shape and GeneralPath classes because a lot of the tutorials on Graphics2D I've seen seem to just deal with predefined shapes like Ellipse2D or Rectangle2D, or have I missed something?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Ok.
    The predefined shapes all implement the Shape interface, so if you define
    yourself a custom shape class that implement this, you should be ok...or ?

    Could you not create this class as a Polygon wrapper/extension ?
    Last edited by sjalle; 05-08-2005 at 08:21 PM.
    eschew obfuscation

  5. #5
    Join Date
    Apr 2005
    Posts
    18
    I was just wondering how you define a custom shape with the Shape interface.

    I recently came across some example code on using the GeneralPath class here, which was something like what I was looking for. I think I'll use that for now.

    Thanks.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Just to answer your question, you do it by making a class that implements the
    methods defined in the interface. To do that I would extend one of the classes that
    already does that, or just plain simple; use that class.
    eschew obfuscation

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