Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default code for moving objects at same time

    Hi guys,i need help,
    how to display multiple moving objects at the same time in java.
    Last edited by Muhammad Assad; 06-19-2010 at 08:13 PM.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Uhh... I think you're going to have to be more specific.

    What code do you have so far? What kind of objects are you trying to move? How do you want them to move (arrow keys, etc.)?

    If you had four objects with X and Y coordinates, all you'd have to do is:
    Java Code:
    obj1.X += 1;
    obj2.X += 1;
    obj3.X += 1;
    obj4.X += 1;
    But since I can't see what you're working with, that's just a guess.

  3. #3
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    i have no idea about the java thing,i'm doing this for the first time.
    well,the statements says that i have to develop a console program in java in which i have to show four different objects that maybe a triangle or circle or anything showing some movement at the same time.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Here is a good example of how to move a ball across the screen using an Applet:
    Java Cooperation: how to move a ball

    Since you're new to Java, are you using an IDE? (A specific editor and compiler)

  5. #5
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    i'm using microsoft visual studio.net 2003

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Hmm... if that's what you'd like to use, go ahead. For Java programming, I'd recommend simply using Notepad and using javac to compile the code. Alternately, you could use the NetBeans IDE, but that might be a bit difficult to learn at first.

    Either way, the tutorial above should show you how to move a simple object. Shouldn't be much trouble to add three more objects to the Applet.

  7. #7
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    hmm,.................thnx :) for ur help.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    have to develop a console program
    Strange that it says a console program. You don't think of them as using GUI.

  9. #9
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    it has to be console application in java.

  10. #10
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    @Norm: do you have solution to this??

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    I understand a console application is one that uses System.out.println() to write to the console. Is that what you want to do?
    display multiple moving objects at the same time
    You really can't have multiple moving objects displayed at the same time. That requires GUI.
    Can you explain what you mean by "console application"?

  12. #12
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Not sure what you was asking here, but in the code below it creates 4 objects called robots, then the user enters in the distance he/she wants them all to move and then at the end of the program they all move at the same time. lol

    PHP Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javaapplication15;
    
    import java.util.Scanner;
    
    
    /**
     *
     * @author alacn
     */
    public class Main {
    
        //nested class for robot objects
        public static class robotClass {
    
            private int dist=0;
    
            public void move(int distPassed){
                dist = dist + distPassed; //=+
            }
        }
        
    
        public static void main(String[] args) {
    
    
            Scanner sc = new Scanner(System.in);
            robotClass robot[] = new robotClass[4];
    
            int enteredDistance[]= new int[robot.length];
            
            //initialising the robot objects
            for(int i=0;i<robot.length;i++)
                robot[i] = new robotClass();
    
            int i=0;
            while(i < robot.length){  //user enters in distance to travel for each robot object. The distances are stored
                System.out.printf("please enter how far you would like robot object to move! %d/%d \n",i+1,robot.length );
                enteredDistance[i] = sc.nextInt();
                i++;
            }
    
            i=0;
            while(i < robot.length){  //Another loop is performed so it seems all robots moves at same time
                robot[i].move(enteredDistance[i]);
                System.out.printf("Robot %s has moved by %s meters \n", i+1,enteredDistance[i]);
                i++;
            }
    
        }
    }
    Last edited by alacn; 06-20-2010 at 07:41 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  13. #13
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    correction,
    it have to be four objects showing some movement at the same time,(in gui
    )

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    Ah GUI!!! That will be a different program.

    Many programs that have "moving" shapes override the paintComponent() method and draw the shapes there. Code outside the paint method computes where the next position should be and call repaint() to have the System call paintComponent() to draw the shapes at the next location.

    See the link in post#4

Similar Threads

  1. Replies: 3
    Last Post: 06-16-2010, 10:27 PM
  2. Moving GRect: Bad Code or Bug?
    By adamg106 in forum New To Java
    Replies: 11
    Last Post: 05-02-2010, 12:35 AM
  3. Moving objects
    By rsjava24 in forum AWT / Swing
    Replies: 1
    Last Post: 02-26-2010, 10:47 AM
  4. Moving objects - setting different speed
    By Antigol in forum Java 2D
    Replies: 2
    Last Post: 08-14-2009, 01:14 AM
  5. examples of moving objects
    By fred in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:06 PM

Posting Permissions

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