Hi guys,i need help,
how to display multiple moving objects at the same time in java.
Printable View
Hi guys,i need help,
how to display multiple moving objects at the same time in java.
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:
But since I can't see what you're working with, that's just a guess.Code:obj1.X += 1;
obj2.X += 1;
obj3.X += 1;
obj4.X += 1;
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.
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)
i'm using microsoft visual studio.net 2003
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.
hmm,.................thnx :) for ur help.
Strange that it says a console program. You don't think of them as using GUI.Quote:
have to develop a console program
it has to be console application in java.
@Norm: do you have solution to this??
I understand a console application is one that uses System.out.println() to write to the console. Is that what you want to do?
You really can't have multiple moving objects displayed at the same time. That requires GUI.Quote:
display multiple moving objects at the same time
Can you explain what you mean by "console application"?
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
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++;
}
}
}
correction,
it have to be four objects showing some movement at the same time,(in gui
)
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