Hi, this is my first post and I need help with a project. I'm trying to create a target with a recognizable boundary so when you click anywhere inside the circle, a point is added to the top-left corner of the applet. I've succeeded in being able to click inside the 'boundary box' that encapsulates the circle/target; however, I need the boundary to be the exact shape of the circle, not the 'invisible' boundary box around it. Here is what I got so far...

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;


public class Target extends Applet
{
private Random generator = new Random();
Images target1;
private double x;
private double y;
private double score;
private String point = "";


public void init()
{
addMouseListener(
new MouseAdapter()
{
public void mousePressed( MouseEvent event )
{


if( target1.isInside( event.getX(), event.getY() ) )
score++;


repaint();
}


}
);


}
public void paint(Graphics g)
{


Graphics2D g2 = (Graphics2D)g;



target1 = new Images( generator.nextInt( getWidth() - 100 ),
generator.nextInt( getHeight() - 100 ), 100.0, 100.0 );
target1.drawTarget(g2);
point = "" + score;
g2.drawString( point, 20, 20 );
}
}


import java.awt.*;
import java.applet.*;
import java.awt.geom.*;


public class Images extends Applet
{
private double xLeft;
private double yTop;
private double width;
private double height;
private double x;
private double y;
private double[] quad;


public Images( double xLoc, double yLoc, double iWidth, double iHeight )
{
xLeft = xLoc;
yTop = yLoc;
width = iWidth;
height = iHeight;
}


public double getXLeft()
{
return xLeft;
}


public double getYTop()
{
return yTop;
}


public double getXWidth()
{
return width;
}


public double getYHeight()
{
return height;
}


public double getXCenter()
{
return width / 2;
}


public double getYCenter()
{
return height / 2;
}


public boolean isInQuad1()
{
return ( ( ( x > xLeft + ( width / 2 ) ) && ( x < xLeft + width ) )
&& ( y < yTop + height / 2 && y > yTop ) );
}


public boolean isInQuad2()
{
return ( x > xLeft + width / 2 && x < xLeft + width &&
y > yTop + height / 2 && y < yTop + height );
}


public boolean isInQuad3()
{
return ( x > xLeft && x < xLeft + width / 2 &&
y > yTop + height / 2 && y < yTop + height );
}


public boolean isInQuad4()
{
return ( x > xLeft && x < xLeft + width / 2 &&
y > yTop && y < yTop + height / 2 );
}


public boolean isInside( double x, double y )
{
double[] quad;


if( isInQuad1() )
{
quad = new double[ (int)width / 2 ];
x = x - xLeft;
for( int ct = 0; ct <= width / 2; ct++ )
{
quad[ct] = *I don't know what goes here!*;
}
}


return false;
}
public void drawTarget( Graphics2D g2 )
{
Ellipse2D.Double outerCircle = new Ellipse2D.Double(
xLeft, yTop, 100, 100 );
g2.setColor( Color.red );
g2.fill( outerCircle );
g2.draw( outerCircle );
Ellipse2D.Double middleCircle = new Ellipse2D.Double(
xLeft + 25, yTop + 25, 50, 50 );
g2.setColor( Color.white );
g2.fill( middleCircle );
g2.draw( middleCircle );
Ellipse2D.Double innerCircle = new Ellipse2D.Double(
xLeft + 37.5, yTop + 37.5, 25, 25 );
g2.setColor( Color.red );
g2.fill( innerCircle );
g2.draw( innerCircle );
}
}

Am I approaching this the right what? Do I need a special algorithm? Any help that can be offered will be greatly appreciated! :D

... create a target with a recognizable boundary ... I need the boundary to be the exact shape of the circle ... Am I approaching this the right what? Do I need a special algorithm?

All of that "isInQuad" setup is more complicated than it has to be, and it will be a nightmare to maintain if you want to change anything.

If it's a circle you want, a little coordinate geometry should do the trick:

public boolean isInCircle(int x, int y, int left, int top, int diameter)
    /*
     *    test whether point (x, y) is inside a circle.
     *    specify (top, left) coordinate of bounding rectangle.
     *    diameter covers both the width and height values.
     */
{
    int radius = diameter / 2;
    int center_x = left + radius;
    int center_y = top + radius;

    int x_distance = x - center_x;
    int y_distance = y - center_y:

    return ((x_distance * x_distance) + (y_distance * y_distance) <= (radius * radius));
}

I've left in a bunch of intermediate values to make sure the math isn't that badly obfuscated--it could be smaller and faster. I did leave off the square roots in the final distance calculation, though, which works fine because you don't need the exact distance, just an "in" or "out" determination; f(x) = x^2 is monotonic, so the "<=" comparison is still valid. Speaking of which, "<=" counts anything right on the edge as on the circle, while a "<" wouldn't. Pick your favorite.

--sg

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.