Why won't my circles show?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

SpyKiller112

Thread Starter
Guest
Joined
Aug 23, 2004
Messages
521
I'm trying to make it so that both of my circles' center points are 25 pixels away from each other. But, I'm not really sure how to do that. I know that the (x,y) coordinate you put in as a parameter for Ellipse2D.Double is at the top left of the bounding box, but really that's all I know.

Two_Circles class: http://fpaste.org/paste/6201
Two_Circles tester: http://fpaste.org/paste/6202

Thanks in advance!
 
Joined
Jul 29, 2004
Messages
6,650
Hi SpyKiller112,

I've tested your coding, your circles are showing.

Unfortunately, you have to apply the Pythagoras' theorem to find the coordinates of the circle center.
In your example, the coordinate of the first circle center (a, b) are :
a = 5 + sqrt((50*50)/2)
b = 10 + sqrt((50*50)/2)

Also, if D is the distance between 2 points (x1,y1) and (x2,y2), then :
(x1-x2)**2 + (y1-y2)**2 = D**2
 

SpyKiller112

Thread Starter
Guest
Joined
Aug 23, 2004
Messages
521
Oh god, thanks a ton. I never would have thought of that! It would be so much easier if the coordinates referred to the center of the ellipse in the first place though.
 
Joined
Jul 29, 2004
Messages
6,650
Hi SpyKiller112,

To solve the problem, you may add the following class in your program :
Code:
[SIZE=2]
package [COLOR=BLUE]your.package.name[/COLOR];

import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;

/**
 *
 * @author Chicon
 */
public abstract class Circle2D extends Ellipse2D {
    public static class Double extends Ellipse2D implements Serializable {
        public double x;
        public double y;
        public double height;
        public double width;
        
        public Double (double x, double y, double radius) {
            double v = this.getValue(radius);
            this.x = x - v;
            this.y = y - v;
            this.width = radius;
            this.height = radius;
        }

        @Override
        public double getX() {
            return this.x;
        }

        @Override
        public double getY() {
            return this.y;
        }

        @Override
        public double getWidth() {
            return this.width;
        }

        @Override
        public double getHeight() {
            return this.height;
        }

        @Override
        public boolean isEmpty() {
            return (width <= 0.0 || height <= 0.0);
        }

        @Override
        public void setFrame(double x, double y, double w, double h) {
            this.x = x;
            this.y = y;
            this.width = w;
            this.height = h;
        }

        public Rectangle2D getBounds2D() {
            return new Rectangle2D.Double(x, y, width, height);
        }
        
        private double getValue(double radius) {
            return Math.sqrt((radius*radius)/2);
        }
    }
}
[/SIZE]
And you adapt your coding this way :
Code:
[SIZE=2]
      //recover Graphics2D
      Graphics2D g2 = (Graphics2D) g;
      
      //draws first circle
      Circle2D.Double circle1 = new Circle2D.Double(40, 45, 50);
      g2.draw(circle1);
[/SIZE]
;)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Top