farm ostrich
farm ostrich

Reputation: 5959

Is there a way to get the coordinates for an oval drawn with Graphics2D?

I know the parametric equations to 'hand draw' a circle. I would like to know if there's a quick way to get the coordinates, because that would be dope. Yay for pink unicorns.

Upvotes: 1

Views: 1869

Answers (3)

StanislavL
StanislavL

Reputation: 57381

Use Ellipse2D.contains(Point) method. You can pass all coordinates in rectangular rea and see whether they are in the oval area.

Upvotes: 2

NawaMan
NawaMan

Reputation: 25687

You can but you may have to use Sun's proprietary code. I am not sure what is the license but Eclipse does not let me compile the code. However, you can compile it with command line Java so I believe it may be usable.

The following is an example of how you may use it.

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import sun.java2d.loops.ProcessPath;
public class Test
{
    static public void main(
            final String ... args)
    {
        final Ellipse2D       e2D  = new Ellipse2D.Float(0, 0, 100, 200);
        final AffineTransform at   = new AffineTransform();
        final Path2D.Float    p2D  = new Path2D.Float(e2D, at);
        final sun.java2d.loops.ProcessPath.DrawHandler dhnd =
        new ProcessPath.DrawHandler(0, 0, 100, 200)
        {
            public void drawLine(
                    final int x1,
                    final int y1,
                    final int x2,
                    final int y2)
            {
                System.out.printf("Line: %d,%d -> %d,%d\n", x1, y2, x2, y2);
            }
            public void drawPixel(
                    final int x,
                    final int y)
            {
                System.out.printf("Pixel: %d,%d\n", x, y);
            }
            public void drawScanline(
                    final int x1,
                    final int x2,
                    final int y)
            {
                System.out.printf("ScanLine: %d,%d -> %d,%d\n", x1, y, x2, y);
            }
        };
        sun.java2d.loops.ProcessPath.drawPath(dhnd, p2D, 0, 0);
    }
}

The code print out what pixel is drawn and what line is draw (calculating pixels of a line is not so difficult).

As you see, this code does not involve stealing the drawing code. It just listen to what is being drawn. So it may be good to go.

And if license is really a problem, you can use PathIterator. Eclipse2D can give you the PathIterator that you can use and track the pixel from there.

Upvotes: 2

holygeek
holygeek

Reputation: 16185

You can look at the source code for drawOval() and steal that.

Or you can grab the area where you think the oval will be drawn, then draw the oval, then grab that same area again and compare it with the previous one, pixel by pixel.

Upvotes: 1

Related Questions