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?
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.
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?
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.
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.
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.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL