使用 RMI 移动多个球时出现问题?

发布于 2024-09-27 23:12:47 字数 1823 浏览 4 评论 0原文

我正在制作一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2024-10-04 23:12:47

看起来您将所有球绑定在同一个名称下。您需要给它们不同的名称,如下所示:

for (int i = 0; i < currentNumBalls; i++) {

  Naming.rebind ("rmi://localhost/BouncingBall"+i, b[i]); //add index to the name
  System.out.println ("remote ball object registered.");
}

然后在查找它们时,使用以下命令:

 for (int i = 0; i < currentNumBalls; i++) {
    try {
        this.aBall[i] = (Ball) Naming.lookup("rmi://localhost/BouncingBall"+i);

    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

It looks like you are binding all of your balls under the same name. You need to give them different names, like this:

for (int i = 0; i < currentNumBalls; i++) {

  Naming.rebind ("rmi://localhost/BouncingBall"+i, b[i]); //add index to the name
  System.out.println ("remote ball object registered.");
}

Then when looking them up, use this:

 for (int i = 0; i < currentNumBalls; i++) {
    try {
        this.aBall[i] = (Ball) Naming.lookup("rmi://localhost/BouncingBall"+i);

    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文