Help with java program

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Hello, I got this final homework program to do for my intro to CS class, but I can't seem to get it working properly.

Basically, there is a 500x500 JPanel that has a 10x10 array of Dot objects in it. In each of those dot objects there is a fillOval to represent a dot. The point of the program is when you click somewhere on the JPanel, a line is drawn between the two closest dots to the click. So it's like a simple version of the game Dots.

My problem is determining which two dots the click was closest to. I've tried using the distance formula to find the closest one, then run through the same loop except specify != the closest (therefore find next closest), but that didn't work. I got some code now that uses the modulus operator on the click coordinates to sorta of place them on the same row/column as the dot (move them to wherever is closest). But that doesn't quite work, since when I click once, it draws a line on everything. When the two closest objects are found, I have them send a boolean of true to either drawUp or drawLeft (since you only need two directions to connect them all, either Up or Left, or Down or Right). I'm not looking for you guys to do my code, but I'm just sorta stuck on what way I should determine the two closest dots.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
If your code isn't too lengthy, could you post it? Try the Attach Code function for extra readability =)

I'd suggest a straightforward (although inelegant) - use the distance formula, and place the results in a sortable container. Each entry would consist of a reference to the Dot as well as the distance from the click. Then sort by distance. Take the first two (or last two, depending on your sort), and you have the correct Dots. Draw a line from the coordinates of the first to the coordinates of the second.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Yeah I think using the distance formula is the way to go. When I tried that originally I just ran into all kinds of problems with incompatible types. Like I'm not sure if I should have a direct reference to the row and column of the object that is closest/next closest, or use their actual coordinates, cause I was trying to do stuff with an int = an object or something like that... Anyway, here's code of the important parts of the program. So the only thing left to do is write my determineDot method that will decide to tell a given dot to draw left or draw up. I will give the distance formula a try later today and post my code to that.

 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
The really easy way to do it is to calculate the distance from the click to each point and save those values in an array. Then search the array 2x for the 2 smallest values and voila!

-silver
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Originally posted by: agnitrate
The really easy way to do it is to calculate the distance from the click to each point and save those values in an array. Then search the array 2x for the 2 smallest values and voila!

-silver
So I would I be able to use a double for loop to calculator each distance and see if it's smallest, or would I need a separate array specifically for the distances?
 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
It should be just 1 array to calculate the distance from each point to that click. You run the loop and get the minimum. Then you have to be clever and run the loop again and get the min and ignore the initial min that you got and find the second smallest. Thus you get the two smallest distances and your two points.

-silver
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Ok... well perhaps this code is sort of correct, but I'm not sure how to determine when to draw a horizontal line and when to draw a vertical. The way I have it set up (whether or not this is a good way to do it??) is that once the two closest are found, they need to send the boolean true to either the drawLeft or drawUp method. And then in the draw method, if ( drawLeft ) is true then it will draw a horizontal line, or if (drawUp) is true then it will draw a vertical. I think this is a pretty good way since that there also needs to be a 'clear' button to erase any lines, and I'm thinking that a loop running through the array setting each dot to send setLeft(false) and setUp(false) so that no lines are drawn. Anyway, in the code I had them drawLeft and drawUp just to see if it worked, but it drew left and up off of more than one dot everytime.
 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
Why do you need that functionality for horizontal vs vertical? Doesn't the draw API only take 4 args, x0, y0, x1, y1 ? If so, don't worry about whether it's horizontal or vertical. Just send the coords into the API and let it work.

With regards to the 'clear' feature, it depends on you how you implement it. You could create a linked list of lines and when you call print, it only prints the linked list of lines. Destroying the pointer of the front of the list effectively clears it. This all depends on what you're using to 'draw' this stuff to the screen.

-silver
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Well the values I have are stored in the 2D 1x1 arrays smallest and nextSmallest. But I don't see how I can get the 4 coordinates out of them to use in the g.drawLine method.