سؤال

Right now I have a basketball court drawn using Graphics (drawLine, drawArc, etc) and have a mouseClicked event that will draw an 'X' or an 'O' on the court based on whether the boolean shotMade is true (set using "Make" and "Miss" buttons at bottom frame). Right now, I actually will produce the 'X' or 'O' at getX()-6, and getY()+6 so that the center of the 'X' or 'O' will be drawn at the point where the mouse was clicked (using g.drawString() will draw the 'X' or 'O' starting with the bottom left corner so it is not directly centered).

My question comes in my differentiation between a two-pointer and a three-pointer. Right now, there are straight vertical lines in the corners of the court on the three point line at the x value 52 and 448, so I know any clicked point with an x less than 52 or more than 448 will be a three, as well as any point with a y value more than 250, indicating the top of the key, or the highest y-value at any point along the arc. In order to determine a three or two anywhere along the arc, I had an idea to store every single x and y pixel that is used to draw the arc into two ArrayLists (one for x, one for y), and when the mouse is clicked at a point, it would search for the x value in its list, and check to see if the y point is more than that at the corresponding point in its list. I drew the arc with g.drawArc(52,-110,396,360,0,-180), putting its center at the point (250, 70).

So using all this information, I am first wondering how I might go about storing each pixel value into an ArrayList using some formula to calculate each of the points around the ellipse, and then how I might check to see if the clicked point is indeed a three or two.

Any help is appreciated, including any more efficient ideas or other thoughts. Thanks!

هل كانت مفيدة؟

المحلول

Have a look at java.awt.geom.Area

notice that in class "Area"... http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Area.html

.... there is a method contains(double x, double y), which returns boolean. The method "Tests if the specified coordinates are inside the boundary of the Shape..."

Perhaps you could use this to find whether or not the user clicked within the ellipse shape that defines the 2 point zone.

Also note that the Area class can take in a Shape to define it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top