Hack Forums
[Java] Lab Examples - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Lab Examples (/showthread.php?tid=5491796)



Lab Examples - makawan - Dec 12, 2016

Q1.
Spoiler (Click to View)
Q2.
Spoiler (Click to View)


* - Stealthâ„¢ - Dec 12, 2016

Code
if (check = true){
            //HELP
            
        }

Oh boy. What is this for?


* - makawan - Dec 12, 2016

It is where you put the code to enlarge the circle. If the point is in the circle then it will execute the code below check=true


* - av00 - Dec 12, 2016

(Dec 12, 2016 12:26 PM)makawan Wrote:  It is where you put the code to enlarge the circle. If the point is in the circle then it will execute the code below check=true

= is for assignment, use == for comparison.
But it's best to only leave the boolean check in your if statement.
if (check) would already execute if check were true, because you would have if (true) and viceversa it would not if check is false.


* - makawan - Dec 12, 2016

Oh i c. Thanks for the help. Any ideas to resize the selected circle?


* - JavaGuy - Dec 12, 2016

Quote: This program should initially show a dot on the canvas, and enlarge the dot when a mouse grabs and drags it.

Okay, not so hard I see. Lets tackle it thinking in logical steps. I see The code to draw the dot is already given to you (or you have already completed it). Nice.

You have a method setting a boolean to either true or false. Since the
Code
onMousePress(Location point)
method is only called when you press the mouse, this check will only set
Code
check
to true when you press the mouse and it's inside the dot. Nice.

Lets look at this next part without any knowledge of the onMouseDrag (since I don't know which library this mouse controller is coming from specifically).

First step, find what Location is being passed in through that argument "point". You can either print out some variables from point (probably and x and y) or just print out the object itself:
Code
System.out.println(point.toString())
or
Code
System.out.println(point.getX() + " " + point.getY())
Okay, so I'm assuming this point passed in is the current position of your mouse when you have the button pressed down.

First off, the "=" operator is an assignment while "==" is conditional, so change:
Code
if (check = true) {}
to
Code
if (check) {}
or
Code
if (check == true) {}

To change the size of the dot, we want to change its radius. Following a simple rule, we can say as the mouse is being dragged away from the center, we should increase the radius.

To do this, have global (class) variables storing the initial X and Y (center) location of the original dot. As the user drags their mouse, find the X and Y position of the mouse (through the point argument) and calculate the distance (distance formula) and set that distance to the radius of the circle.