• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to move diagonally?  RSS feed

 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i have to make a circle with a menu and the menu gives options to alter the circle. you can change color, diameter, position, and also move to top/bot/left/right. the last few are trickier. i have to move the circle/ball up and right, down and right, up and left, and down and left. this is how i did the left/right/top/bot:



it all works perfectly until i try to do the diagonal movements. i want it to do it gradually like a pong ball's motion would be minus the bouncing back. so just go up and right until you hit either y = 0 or x = 1000 - diameter of circle. this is what i have so far and i cant get it to work.



i know we cant give answers but any help would be appreciated. ill post my full code so far for my main class below as well. there is also a circle and canvas class both of which were given to us already. all of the comments are just for me so i can keep things straight. the menu is 13 items long plus the default im setting to input invalid as usual. originally i was going to use a for loop but that seemed dumb to me. maybe im wrong. a do while loop seemed to fit better once i started to type the code. thanks all!

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something wrong with a moveTo method taking such complicated arguments. Consider giving your Circle class moveRight and moveDown methods. You can move up and left by giving negative arguments. For example...will move the Circle about 14px in this direction: ⭩
14px ≈ 10 × √2.
 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is something wrong with a moveTo method taking such complicated arguments. Consider giving your Circle class moveRight and moveDown methods. You can move up and left by giving negative arguments. For example...will move the Circle about 14px in this direction: ⭩
14px ≈ 10 × √2.


so i def would have done this except that we are strictly for no reason at all allowed to change the circle class or the canvas class. once again as ive said in many of my posts, the rules of the assignment are taking away from the experience of learning but i suppose it is making me think and as questions and learn none the less lol
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Mo wrote:. . . i def would have done this except that we are strictly for no reason at all allowed to change the circle class . . .
But the code you posted earlier with calls to moveTo is modifying the state of a Circle object. If you have those constraints, you are going to have to think again. A moveTo method appears to be inconsistent with those constraints. Consider some other way to implement this, possible matching a Circle object with a Position object.
I do not like assignments with such strict definitions; I believe the more is specified for you in advance, the less you will be able to do, and the less the assignment assesses your abilities.
 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mike Mo wrote:. . . i def would have done this except that we are strictly for no reason at all allowed to change the circle class . . .
But the code you posted earlier with calls to moveTo is modifying the state of a Circle object. If you have those constraints, you are going to have to think again. A moveTo method appears to be inconsistent with those constraints. Consider some other way to implement this, possible matching a Circle object with a Position object.
I do not like assignments with such strict definitions; I believe the more is specified for you in advance, the less you will be able to do, and the less the assignment assesses your abilities.


yeah so i will try to attach the other classes. my professor made them and told us we are to not alter them in any way for any reason and can only use the given methods to do what she is asking. i could have had this done in no time at all but the given methods fall short in my opinion. i have already made a menu method bc i didnt want to have to print that menu a few times in my switch statement. so to make my code a little cleaner, i made that method. im hoping i dont get in trouble for doing that lol i am going to email her when she is back from her family vacation over this spring break.

i tried to upload the pdf that describes the assignment but i cant upload a pdf? is there a specific was im supposed to upload it?







those are the given classes. only thing i changed as mentioned was adding the printMenu() method. the main class earlier in the post is 100% done by me. My professor and her colleagues are responsible for the work done on these 2 classes and im taking no credit for them. the main thing about this diagonal movement is that it shouldnt just jump to the spot it will end at. i want it to move some distance that is pretty small then redraw and keep looping that movement until it reaches an edge. i didnt think this would be super difficult but nothing i try works so far. thanks for the responses everyone!
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want it to move some distance that is pretty small then redraw and keep looping that movement until it reaches an edge.

That implies some waiting between the drawing at the intermediate positions.  Is there some delay mechanism in the given classes?
 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
i want it to move some distance that is pretty small then redraw and keep looping that movement until it reaches an edge.

That implies some waiting between the drawing at the intermediate positions.  Is there some delay mechanism in the given classes?


im not really sure to be honest. i am on week 5 or 6 of programming and have 0 experience before that. this coupled with restrictions on assignments makes communicating my issues and solutions somewhat of a pain in the ass. thank you for trying to help tho!

i would guess, however, that every time i incremented the coordinates that it would have to be redrawn then incremented again until the while condition is broken. (hits the max x or y) this is the only way i can think of doing this with the given restrictions.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd imagine the sequence of events would be: move a little, wait a little so user can see, move a little, wait, move, wait ... until at destination
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is really easy to make something move smoothly. Create a Swing Timer and add an ActionListener to it. The ActionListener causes your object to move a small distance, and you make the timer fire freuently; try once every 25ms. Read all about it in the Java™ Tutorials.
 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It is really easy to make something move smoothly. Create a Swing Timer and add an ActionListener to it. The ActionListener causes your object to move a small distance, and you make the timer fire freuently; try once every 25ms. Read all about it in the Java™ Tutorials.


this is like reading cuneiform lol xD im going to try to figure this out for sure bc it looks interesting and useful! just going to take me some time bc im super new to all of this.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the other classes. my professor made them and told us we are to not alter them in any way for any reason

That will prevent your using some of the useful suggestions you can receive here.

If you could make a suggestion to your professor, ask her to add this to the Canvas class's constructor.
 
The code does not stop the JVM.  If you run the code several times, it  leaves the java program running for each time.

Try doing some debugging to the code where you think it should be moving the shape.  Inside the loop print out the shapes x and y positions for every time around the loop.
 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far this is what i have and it is all working! the growing and changing at the same time will be interesting to do but i have a few ideas! thanks for everyone input!


 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code has many copies of these three lines.  There should be a way to only have one set of these 3 lines:


How about outside of the switch
 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:The code has many copies of these three lines.  There should be a way to only have one set of these 3 lines:


How about outside of the switch


cant believe i didnt think of that haha xD thank you for pointing that out! cleaned up the code alot by doing that. im working on the case 12 and 13 now and i have it working...just not exactly how i want it to. is there a way to change/set the timing on how fast things happen? idk how to ask this question really.



the above code is for the case 12. it should grow the ball and change its color as it grows. my problem with this is that it does in all suuuper fast and you cant really even see the individual colors. is there a way to make it take 2ish times as long to fully grow the 7 times? sorry if im unclear on the question. not sure the best way to ask this.
 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i looked this topic up and came across...

Thread.sleep(your value in im guessing MS?);

this seems to do what im looking for but the catching exception is kind of losing me. we are just now learning about things where we have to throw or catch an exception. once i get it working i can play with the timing and get it how i want it.

i feel like asking 100's of questions is super annoying but i guess thats why you guys/gals do this! i appreciate it all! you all are more help than you know <3
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the Thread.sleep() method will work for you.  The required catch block can be empty as you don't expect anything to interrupt the sleep
 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:I think the Thread.sleep() method will work for you.  The required catch block can be empty as you don't expect anything to interrupt the sleep


anywhere i insert it in my do-while loop i get "unreported exception java.lang.InterruptedException; must be caught or declared to be thrown"

i put it above the first line of the do statement.



 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i ended up trying this and it worked. i make it into its own method that i call in between the lines where i want the pauses.



im going to keep playing with this assignment (extra credit assignment) for a while just to see what i can do with it.  any other comments about this rough draft main are welcome! thanks all
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could pass the sleep time as an arg to the timing method to allow different delays in different places.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!