943,398 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4796
  • Java RSS
May 11th, 2004
0

Trouble creating a boundary around a target or circle

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Radahl is offline Offline
2 posts
since May 2004
May 13th, 2004
0

Re: Trouble creating a boundary around a target or circle

Quote originally posted by Radahl ...
... 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:

Java Syntax (Toggle Plain Text)
  1. public boolean isInCircle(int x, int y, int left, int top, int diameter)
  2. /*
  3.   * test whether point (x, y) is inside a circle.
  4.   * specify (top, left) coordinate of bounding rectangle.
  5.   * diameter covers both the width and height values.
  6.   */
  7. {
  8. int radius = diameter / 2;
  9. int center_x = left + radius;
  10. int center_y = top + radius;
  11.  
  12. int x_distance = x - center_x;
  13. int y_distance = y - center_y:
  14.  
  15. return ((x_distance * x_distance) + (y_distance * y_distance) <= (radius * radius));
  16. }

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
Reputation Points: 182
Solved Threads: 71
Posting Pro in Training
gusano79 is offline Offline
475 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Application Output Help
Next Thread in Java Forum Timeline: Need help in thread handling pplease read





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC