Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default How can i move multiple objects at once?

    Hi,

    I am new to Java programming and i am using BLUEJ to create some basic objects etc.
    I have 3 circle objects (individual) and i am wanting to use "slowMoveHorizontal" method to move these 3 objects about 50px to the right.

    So far, everything works BUT the issue is that they do NOT move together simultaneously. They move on a ONE BY ONE basis.

    I would appreciate any help.

    Thank you in advance.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: How can i move multiple objects at once?

    Hi patstan, welcome.

    If you could provide us with the code we would be able to better assist.
    And please use the [code]CODE[/code] tags.

    Regards.

  3. #3
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Re: How can i move multiple objects at once?

    Java Code:
    public CirclesMoving() {
    
    }
    
    public void draw()
        {
    SOME CODE HERE THAT GIVES CIRCLE1, CIRCLE2 and CIRCLE3 its characteristics
    
    }
    
    public void moveRight()
        {
            circle1.slowMoveHorizontal(60);
            circle2.slowMoveHorizontal(60);
            circle3.slowMoveHorizontal(60);
            
        }
    Thank you!

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: How can i move multiple objects at once?

    Without seeing more of the code, I would suspect your issue is either with your draw method or possibly something effecting the position of the object themselves.
    I would also be worth presenting the code in the draw method.

    Regards.

  5. #5
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Re: How can i move multiple objects at once?

    Here are the draw methods for the 3 circles.

    Java Code:
            circle1 = new Circle();
            circle1.changeColor("yellow");
            circle1.moveHorizontal(180);
            circle1.moveVertical(250);
            circle1.changeSize(50);
            circle1.makeVisible();
            System.out.println("circle1: " + circle1);
            
            circle2 = new Circle();
            circle2.changeColor("yellow");
            circle2.moveHorizontal(180);
            circle2.moveVertical(250);
            circle2.changeSize(50);
            circle2.makeVisible();
            System.out.println("circle2: " + circle2);
            
            circle3 = new Circle();
            circle3.changeColor("yellow");
            circle3.moveHorizontal(270);
            circle3.moveVertical(250);
            circle3.changeSize(50);
            circle3.makeVisible();
            System.out.println("circle3: " + circle3);

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: How can i move multiple objects at once?

    At what point do you call the redraw for the animation?

    Regards.

  7. #7
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Re: How can i move multiple objects at once?

    Quote Originally Posted by Ronin View Post
    At what point do you call the redraw for the animation?

    Regards.
    Basically i just right click on the Circle Object and click on the void moveRight() function. Everything moves to the right one by one starting from circle1 to circle 3, but i want to move them all at once.

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: How can i move multiple objects at once?

    Ah, again without seeing the code, I wonder if the slowMoveHorizontal() method is triggering the repaint after each call. Can you also post the code for this.

    Regards.

  9. #9
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Re: How can i move multiple objects at once?

    Which code? I've posted all of it above.

  10. #10
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: How can i move multiple objects at once?

    Googling the method slowMoveHorizontal() throws up a number of examples where the draw() method is called at the end of this method. This would explain why each of your objects are redrawn after each object has moved instead of after all objects have moved.

    Regards.

  11. #11
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Re: How can i move multiple objects at once?

    Quote Originally Posted by Ronin View Post
    Googling the method slowMoveHorizontal() throws up a number of examples where the draw() method is called at the end of this method. This would explain why each of your objects are redrawn after each object has moved instead of after all objects have moved.

    Regards.
    Sorry, but what do you exactly mean? Can you sketch out a small example?
    Thanks

  12. #12
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: How can i move multiple objects at once?

    I have no experience with BlueJ but I have found the following code which I believe is symptomatic of your particular issue.
    java - Rectangle Class for BlueJ - Stack Overflow. Please ignore the question asked by this user and just refer to their code.

    Scroll to the method public void slowMoveHorizontal(int distance).
    The for loop at the end of this method increments the x coordinate and calls the draw() method.
    The draw() method calls the canvas, draw the rectangle, then waits for 10 milliseconds before moving to the next iteration in the for loop.

    In your case, it is only once the call to slowMoveHorizontal() (for loop and all) has been completed that slowMoveHorizontal() is called on the next circle.
    What you would need to do is to remove this call to draw() from the slowMoveHorizontal() (and presume slowMoveVertical()) and call this only once all three circles have been moved. This will require a small bit of work but I will let you have a play before going any further.

    Regards.

  13. #13
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Re: How can i move multiple objects at once?

    Thats somewhat confusing for me to understand. Lets keep things simple.

    1. Here is the code for drawing the 3 circles.

    public void draw()
    {
    Java Code:
    circle1 = new Circle();
    circle1.changeColor("yellow");
    circle1.moveHorizontal(180);
    circle1.moveVertical(250);
    circle1.changeSize(50);
    circle1.makeVisible();
    System.out.println("circle1: " + circle1);
     
    
    circle2 = new Circle();
    circle2.changeColor("yellow");
    circle2.moveHorizontal(180);
    circle2.moveVertical(250);
    circle2.changeSize(50);
    circle2.makeVisible();
    System.out.println("circle2: " + circle2);
     
    circle3 = new Circle();
    circle3.changeColor("yellow");
    circle3.moveHorizontal(270);
    circle3.moveVertical(250);
    circle3.changeSize(50);
    circle3.makeVisible();
    System.out.println("circle3: " + circle3);}
    Ok. Now i have draw my 3 circles and i want to move all of these to the right at the same time (i.e. not one by one).

    ---------------------------------------------

    2. This is the code i am using and this works, but its moving each circle one by one.

    Java Code:
    public void moveRight()
        {
            circle1.slowMoveHorizontal(50);
            circle2.slowMoveHorizontal(50);
            circle3.slowMoveHorizontal(50);
           
    
        }
    3. Its understandable that this is the case, because java is following each line one by one.
    My question is how can i put "circle1", "circle2" and "circle3" into ONE class so that i will need only one line of code to initiate the move?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How can i move multiple objects at once?

    i want to move all of these to the right at the same time (i.e. not one by one).
    Can you explain what you mean by "at the same time"? Computers do things one at a time. With multiple processors it is possible to do more than one thing at a time with concurrent threads.

    If a program updates its GUI at fixed intervals, all the moves made before the end of the current interval will appear to have been done "at the same time".

    What does the slowMoveHorizontal() method do?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Re: How can i move multiple objects at once?

    @norm

    sure. I will explain it.

    So what i want to do is to get the 3 circle objects to move to the right (i.e. slow move horizontally) all at once. Imagine you have 3 planets moving to the right, obviously they will be moving together. In my case, the first circle moves and once that has stopped, circle2 will start moving etc.

    Hope you understand.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How can i move multiple objects at once?

    Did you understand this:
    If a program updates its GUI at fixed intervals, all the moves made before the end of the current interval will appear to have been done "at the same time".


    The code needs to change the position of all the circles BEFORE the next time the GUI is updated.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Re: How can i move multiple objects at once?

    @Norm

    So how can i solve this in my case?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How can i move multiple objects at once?

    Is the slowMoveHorizontal() method the problem? Does it move and show the GUI before returning?

    Find another way to change the circles' positions BEFORE updating the GUI to show their new positions.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: How can i move multiple objects at once?

    It would be easier if you could show us the code in the circle class.
    I still recon your issue is with the slowMoveHorizontal() method.

    Regards.

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How can i move multiple objects at once?

    Quote Originally Posted by Ronin View Post
    It would be easier if you could show us the code in the circle class.
    I don't really know anything about BlueJ but based on a quick web search I believe the Circle class is part of the
    BlueJ IDE (which is used to help folks learn Java at a higher level). I would recommend the OP just use the
    JDK to move various shapes around a window. It would also be of more general use when developing other
    apps.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Page 1 of 2 12 LastLast

Similar Threads

  1. move multiple columns in jtable
    By ssarvjeet in forum New To Java
    Replies: 1
    Last Post: 09-30-2013, 04:08 AM
  2. Replies: 8
    Last Post: 08-16-2012, 09:41 PM
  3. Replies: 3
    Last Post: 07-04-2011, 12:50 PM
  4. do we need multiple connection objects
    By Pacerier in forum Java Servlet
    Replies: 4
    Last Post: 11-27-2010, 06:16 PM
  5. Move objects with Timer
    By ProGenius in forum New To Java
    Replies: 6
    Last Post: 12-27-2009, 02:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •