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