使用 RMI 移动多个球时出现问题?
我正在制作一个使用 RMI 移动球的分布式动画。
我的目标是以某种方式移动多个球,以便多个客户端观察球的相同运动/位置,我使用的是远程对象的球对象。
当球只有一个时,球移动得很好,但当我试图增加球的数量时,我失败了。
这里有一些代码片段,我在其中应用了循环来处理多个球:
在服务器上:
b[0] = new BallImpl(0, 50, 2, 3 ,Color.YELLOW,20);
b[1] = new BallImpl(50, 50, 4, 3,Color.BLUE,10);
b[2] = new BallImpl(40, 40, 5, 5, Color.PINK,30);
b[3] = new BallImpl(60, 70, 4, 6, Color.GREEN,40);
for (int i = 0; i < currentNumBalls; i++) {
Naming.rebind ("rmi://localhost/BouncingBall", b[i]); // registers Ball object
System.out.println ("remote ball object registered.");
}
在客户端站点上:
我如何查找远程球:
for (int i = 0; i < currentNumBalls; i++) {
try {
this.aBall[i] = (Ball) Naming.lookup("rmi://localhost/BouncingBall");
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
start();
那是移动球代码:
public void moveballs() {
for (int i = 0; i < currentNumBalls; i++) {
try {
aBall[i].setBounds(pit.getWidth(), pit.getHeight());
aBall[i].move();
pit.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
}
那是绘图代码:
public void drawballs(Graphics g) {
for (int i = 0; i < currentNumBalls; i++) {
try {
g.setColor(aBall[i].getColor());
g.fillOval(aBall[i].getX(), aBall[i].getY(), aBall[i].getradius(), aBall[i].getradius());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
有人可以指导我为什么我可以只看到一个球在移动,那么其他球呢?或者这个设计存在一些问题并且我以错误的方式使用了 RMI?或者向我推荐一些可以实现我的目标的设计。
非常感谢,
吉比
I am working on a distributed animation of moving BALLS with RMI.
My goal is to move multiple balls in a way, so that multiple clients observe the same movement/position of balls, i m using ball object which is remote object.
The ball is moving fine, when it is only one, but when i m trying to increase the number of balls, i m failing.
here are some code snippets where i applied loops to work for multiple balls:
on server:
b[0] = new BallImpl(0, 50, 2, 3 ,Color.YELLOW,20);
b[1] = new BallImpl(50, 50, 4, 3,Color.BLUE,10);
b[2] = new BallImpl(40, 40, 5, 5, Color.PINK,30);
b[3] = new BallImpl(60, 70, 4, 6, Color.GREEN,40);
for (int i = 0; i < currentNumBalls; i++) {
Naming.rebind ("rmi://localhost/BouncingBall", b[i]); // registers Ball object
System.out.println ("remote ball object registered.");
}
on client site :
that how i look up for remote balls:
for (int i = 0; i < currentNumBalls; i++) {
try {
this.aBall[i] = (Ball) Naming.lookup("rmi://localhost/BouncingBall");
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
start();
thats moving balls code:
public void moveballs() {
for (int i = 0; i < currentNumBalls; i++) {
try {
aBall[i].setBounds(pit.getWidth(), pit.getHeight());
aBall[i].move();
pit.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
}
and that's the drawing code:
public void drawballs(Graphics g) {
for (int i = 0; i < currentNumBalls; i++) {
try {
g.setColor(aBall[i].getColor());
g.fillOval(aBall[i].getX(), aBall[i].getY(), aBall[i].getradius(), aBall[i].getradius());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
Can somebody please guide me why i can see only one ball moving, what about other balls or there is some problem in this design and i m using the RMI in wrong way? or recommend me some design by which i can accomplish my goal.
thanks a lot,
jibby
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您将所有球绑定在同一个名称下。您需要给它们不同的名称,如下所示:
然后在查找它们时,使用以下命令:
It looks like you are binding all of your balls under the same name. You need to give them different names, like this:
Then when looking them up, use this: