• Help with Java Programming assignment.
    1 replies, posted
Hey guys, I have an assignment due for Thursday for my programming course, its sort of basic Java sort of stuff, but I am pretty stuck on it. Here is what we are told to do: [quote] [B]Reaction Testor[/B] Write a program that will test a user's mouse control. The program should draw a small circle at a random position. The user should attempt to click the mouse inside the circle as quickly as possible. If the user does manage to click the circle, the circle will disappear and the program will print the time required to click the mouse. After displaying the time, the program draws another circle and the process repeats. If this seems difficult, just do part of the program. Remember, to generate a Random integer between 0 and N-1, use Math.random() * N. You will have to cast it to an int, so to assign this number to an int. E.g to generate a random number between 0 and 999, you would use int x = (int) (Math.random() * 1000); There are some problems in the lab that contain hints which can help with this problem. [B]Extra Credit 10%[/B] Remember the fastest time and print a message if the user beats the previous record.[/quote]Basically I am stuck on the whole click on the button method, I get errors when I put the circle name in the isPressed method, but when I put the window name in it works, but It has to work on the circle. Any help would be great, here is the code I have so far: [quote]import graphics.*; public class reaction { public static void main(String [] args) { int window_x = (int) (Math.random() * 900 + 1); int window_y = (int) (Math.random() * 900 + 1); long start = 0; long stop = 0; Window win = new Window(500, 500); Color red = new Color (255, 0, 0); win.setForeground(red); Circle click = new Circle(window_x, window_y, 30); win.draw(click); win.fill(click); System.out.println ("" + window_x + ""); System.out.println ("" + window_y + ""); while(!win isPressed): ; if(win.isPressed()); win.erase(click); } } [/quote]
What are those graphics.Window and graphics.Circle classes you're using? Something specific to your programming course? If so, none of us know what operations they support or how they're supposed to be used. That being said, there's a fair chance that the Circle class is just a drawing tool and not something that can receive click events. If the click events go to your window, just calculate the distance from the click coordinates to the circle's center coordinates (Pythagorean theorem), and if it's less than the circle's radius, the click was within the circle.
Sorry, you need to Log In to post a reply to this thread.