| | |
Trouble creating a boundary around a target or circle
![]() |
•
•
Join Date: May 2004
Posts: 2
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: May 2004
Posts: 82
Reputation:
Solved Threads: 4
•
•
•
•
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?
If it's a circle you want, a little coordinate geometry should do the trick:
Java Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Agent class trouble ** Please help! ** (C++)
- Little help with creating a C++ password function (C++)
- Creating a program to split numbers into there separate digits (C++)
- Creating a source/target in Dojo (JavaScript / DHTML / AJAX)
- trouble with creating a database (C)
- trouble with double linked list (C++)
- Creating Login (Java)
Other Threads in the Java Forum
- Previous Thread: Application Output Help
- Next Thread: Need help in thread handling pplease read
| Thread Tools | Search this Thread |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline object pong print problem producer program programming project projectideas read recursion recursive replaysolutions rim scanner sell server set size sms sort sql string swing terminal threads tree web websites windows





