IndexoutofBoundException[kryonet]

Hi all,
inspite of my best efforts i m unable to run to move a ball with
kryo ,i m beginner java programmer can somebody please pointing me
where i m wrong in adapting kryonet as i did lot of efforts and tries
but this code is not working and everytime coming with some
exception,am i doing so much obvious mistake as i m exhausted with
trying different ways and started hit and try approach will this kind
of project will be complicated with kryonet,

package com.esotericsoftware.kryonet;

import java.awt.Color;
import java.awt.Point;

public interface IBall {
	public void setColor (Color newColor);

	public Color getColor ();

	public void setMotion (double dx, double dy);

	public int radius ();

	public Point location ();

	public double changeX ();

	public double changeY ();

	public void move ();
}



package com.esotericsoftware.kryonet;

import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;
import com.esotericsoftware.minlog.Log;

// This class is the server for a simple chat client/server example that uses RMI.
// It is recommended to review the non-RMI chat example first.
// While this example uses only RMI, RMI can be mixed with non-RMI KryoNet usage.
// RMI has more overhead (usually 4 bytes) then just sending an object.
public class BallServer {
	Server server;

	public BallServer () throws IOException {
		server = new Server();

		// Register the classes that will be sent over the network.
		Common.registerMessages(server.getKryo()); // just another way/try
		Common.register(server);
		server.start();
		server.bind(Common.DEFAULT_PORT_TCP, Common.DEFAULT_PORT_UDP);

		Ball requestBall = new Ball(new Point(300, 200), 15);

		final ObjectSpace objectSpace = new ObjectSpace();
		objectSpace.register(42, requestBall);
		// new ObjectSpace().register(Common.BALL, requestBall);

		server.addListener(new Listener(){
			public void connected (Connection connection) {
				objectSpace.addConnection(connection);
			}
		});
		
		// For serialization
		Kryo kryo = server.getKryo();
		kryo.register(IBall.class);

		// Open a window to provide an easy way to stop the server.
		JFrame frame = new JFrame("Ball Server");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosed (WindowEvent evt) {
				server.stop();
			}
		});
		frame.getContentPane().add(new JLabel("Close to stop the chat server."));
		frame.setSize(320, 200);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	public static void main (String[] args) throws IOException {
		Log.set(Log.LEVEL_DEBUG);
		new BallServer();
	}
}





package com.esotericsoftware.kryonet;

import java.awt.*;
import java.io.IOException;

import javax.swing.*;



import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;

import java.util.Random;

public class BallWorldClient extends JFrame implements Runnable {

	private volatile boolean Play;

	public static final int FrameWidth = 600;
	public static final int FrameHeight = 400;
	MyKeyListener pit;
	private static final long serialVersionUID = 1L;

	private Client client;
	
	private IBall ball;

	private Random rand = new Random(System.currentTimeMillis());

	BallWorldClient (Color ballColor) {

		setSize(FrameWidth, FrameHeight);
		setTitle("Ball World");
		
		pit = new MyKeyListener();
		pit.setPreferredSize(new Dimension(FrameWidth, FrameHeight));
		setContentPane(pit);

		pack();
	setVisible(true);

	setBackground(Color.white);

		try {
			client = new Client();

			Common.registerMessages(client.getKryo());
			Common.register(client);
			client.start();

			new Thread("Connect") {
				public void run () {
					try {
						client.connect(50000, Common.DEFAULT_IP, Common.DEFAULT_PORT_TCP, Common.DEFAULT_PORT_UDP);
					} catch (IOException ex) {
						ex.printStackTrace();
						System.exit(1);
					}

				}
			}.start();

			//ball1 = new Client();

			ObjectSpace objectSpace = new ObjectSpace();

			Kryo kryo = client.getKryo();
			kryo.register(Ball.class);
			kryo.register(IBall.class);

//			Ball requestBall = new Ball(new Point(300, 200), 15);
//
////			Common.registerMessages(ball1.getKryo());
////			Common.register(ball1);
//			objectSpace.register(42, requestBall);

			ball = ObjectSpace.getRemoteObject(client, 42, IBall.class);
			ball.setColor (ballColor);
		    ball.setMotion(3.0, 6.0);
			client.sendTCP(ball);

//			client.addListener(new Listener() {
//				public void received(Connection connection, Object object) {
//					if (object instanceof Ball) {
//						final Ball ball1 = (Ball) object;
//						new Thread("Connect1") {
//							public void run () {
//								for (int i = 0; i < 1000; i++)
//								runball();
//							}
//						}.start();
//						
//
//					}
//				}
//			});

		} catch (Exception e) {
			e.printStackTrace();
			Play = false;
		}
		start();

	}

	public void start () {
		Play = true;
		Thread t = new Thread(this);
		t.start();
	}

	public void stop () {
		Play = false;
	}

	public void run () {

		while (Play == true) {

			repaint();
			runball();
			try {
				Thread.sleep(50);
			} catch (InterruptedException ie) {
				stop();
			}
		}
	}

	public void runball () {
		try {
			ball.move();
			Point pos = ball.location();
			if ((pos.x < ball.radius()) || (pos.x > FrameWidth - ball.radius())) ball.setMotion(-ball.changeX(), ball.changeY());

			if ((pos.y < ball.radius()) || (pos.y > FrameHeight - ball.radius())) ball.setMotion(ball.changeX(), -ball.changeY());
			 repaint ();
			try {
				Thread.sleep(50);
			} catch (Exception e) {
				System.exit(0);
			}
		} catch (Exception e) { // System.out.println ("Exception: " + e);
			e.printStackTrace();
		}
	}

	public void drawworld(Graphics g) {

		try {
			g.setColor(ball.getColor());
			g.fillOval(ball.location().x - ball.radius(), ball.location().y - ball.radius(), ball.radius() * 2, ball.radius() * 2);
		} catch (Exception e) { // System.out.println ("Exception: " + e);
			e.printStackTrace();
		}
	}
	

	public static void main (String[] args) {

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run () {

				BallWorldClient world = new BallWorldClient(Color.red);
			}
		});
	}
	
	
	public class MyKeyListener extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
			super.paintComponent(g);

			drawworld(g);

		}

	}
	
}




package com.esotericsoftware.kryonet;

import java.awt.Color;
import java.awt.Point;
import java.nio.ByteBuffer;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.serialize.FieldSerializer;
import com.esotericsoftware.kryo.serialize.IntSerializer;
import com.esotericsoftware.kryo.serialize.SimpleSerializer;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;

public class Common {

	public static final String DEFAULT_IP = "127.0.0.1";

	public static final int DEFAULT_PORT_TCP = 54555;
	public static final int DEFAULT_PORT_UDP = 54777;

// public static final int DEFAULT_PORT_TCP=6456;
// public static final int DEFAULT_PORT_UDP=6466;
	static public final short BALL = 1;

	static public final int port = 54777;

	/**
	 * Registers all messages used in the game with JGN which optimizes them
	 */
	public static void registerMessages (Kryo kryo) {
		ObjectSpace.registerClasses(kryo);
		kryo.register(IBall.class);
		kryo.register(Ball.class);
		kryo.register(Point.class, new SimpleSerializer<Point>() {
			public Point read (ByteBuffer buffer) {
				return new Point(IntSerializer.get(buffer, true), IntSerializer.get(buffer, true));
			}

			public void write (ByteBuffer buffer, Point point) {
				IntSerializer.put(buffer, point.x, true);
				IntSerializer.put(buffer, point.y, true);
			}
		});
		kryo.register(Color.class, new SimpleSerializer<Color>() {
			public Color read (ByteBuffer buffer) {
				return new Color(buffer.getInt());
			}

			public void write (ByteBuffer buffer, Color color) {
				buffer.putInt(color.getRGB());
			}
		});
	}

//

	static public void register (EndPoint endPoint) {
		Kryo kryo = endPoint.getKryo();
		ObjectSpace.registerClasses(kryo);
		kryo.register(String[].class);
		kryo.register(IBall.class);
		kryo.register(Ball.class);
	}
}



package com.esotericsoftware.kryonet;

import java.awt.Color;
import java.awt.Point;

public class Ball implements IBall {

	protected Point loc ;
	protected int rad = 15;
	protected double changeInX = 0.0;
	protected double changeInY = 0.0;
	protected Color color ;


	public Ball (Point lc, int r) {
		loc = lc;
		rad = r;
	}

	public void setColor (Color newColor) {
		color = newColor;
	}

	public Color getColor () {
		return color;
	}

	public void setMotion (double dx, double dy) {
		changeInX = dx;
		changeInY = dy;
	}

	public int radius () {
		return 15;
	}

	public Point location () {
		return loc;
	}

	public double changeX () {
		return changeInX;
	}

	public double changeY () {
		return changeInY;
	}

	public void move () {
		loc.translate((int)changeInX, (int)changeInY);
	}
}

java.lang.IndexOutOfBoundsException: Index: 482, Size: 482
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at com.esotericsoftware.kryonet.rmi.ObjectSpace$RemoteInvocationHandler.waitForResponse(ObjectSpace.java:344)
at com.esotericsoftware.kryonet.rmi.ObjectSpace$RemoteInvocationHandler.invoke(ObjectSpace.java:329)
at $Proxy0.location(Unknown Source)
at com.esotericsoftware.kryonet.BallWorldClient.runball(BallWorldClient.java:135)
at com.esotericsoftware.kryonet.BallWorldClient.run(BallWorldClient.java:123)
at java.lang.Thread.run(Thread.java:662)
java.lang.IndexOutOfBoundsException: Index: 483, Size: 483
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at com.esotericsoftware.kryonet.rmi.ObjectSpace$RemoteInvocationHandler.waitForResponse(ObjectSpace.java:344)
at com.esotericsoftware.kryonet.rmi.ObjectSpace$RemoteInvocationHandler.invoke(ObjectSpace.java:329)
at $Proxy0.getColor(Unknown Source)
at com.esotericsoftware.kryonet.BallWorldClient.drawworld(BallWorldClient.java:153)
at com.esotericsoftware.kryonet.BallWorldClient$MyKeyListener.paintComponent(BallWorldClient.java:192)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at java.awt.Window.paint(Window.java:3375)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

lot of thanks in advance ,
wishes,
jibbylala

The thread on the KryoNet discussion group contains my replies:
http://groups.google.com/group/kryonet-users/browse_thread/thread/594a89ae3739f026

Note this is the same advice (don’t use RMI) you were given by princec and gouessej 5 months ago:
http://www.java-gaming.org/index.php/topic,23283.msg193250.html#msg193250
KryoNet’s RMI is much better suited for this task then Java’s RMI, but as a beginner it will be much better for you to send a receive objects explicitly. There is no magic and it is easier to understand.

what do you mean by not using java RMI, i m already using kryonet instead of javaRMI, so what u wanna convey.

P.S : about there suggestion to create a simple case is quite trivial, but going far from them is not that much trivial and achieving the robust solutions, u can see my this post, is it kryonet able to give me the solution for this scenario :

and more importantly what would the complexity level as far as implementation is concerned,

i tried that with javaNIO but find that way harder to go further. if kryonet will gave me such solution for that with moderate implementation efforts, i would be glad but till now i m not seeing.

You can use KryoNet in two ways. One is by sending and receiving objects. Another is by using KryoNet’s ObjectSpace class, which does remote method invocation (RMI). You are using KryoNet, but you are using ObjectSpace. I suggest using the simpler mechanism and not RMI. It will be easier for you to understand what is happening.

See the non-RMI chat example here:
http://code.google.com/p/kryonet/source/browse/#svn%2Ftrunk%2Fkryonet%2Fexamples%2Fcom%2Fesotericsoftware%2Fkryonet%2Fexamples%2Fchat

For a solution to the scenario you posted on StackOverflow: for each ball, write bytes that identify the ball’s owner and the ball’s location. Periodically send these bytes from the server to the clients. How you send these bytes is separate from solving your problem, it could be KryoNet, JavaRMI, NIO, old IO, etc. What these bytes look like is also separate from solving your problem, the bytes could be in the form of a byte array, Strings, ints, etc.

I recommend reading the docs for BOTH Kryo and KryoNet:
http://code.google.com/p/kryo/
http://code.google.com/p/kryonet/
Kryo does serialization (going from an object to bytes and back). KryoNet does the sending and receiving over the network. You can also use the projects’ javadocs for information:
http://kryo.googlecode.com/svn/api/index.html
http://kryonet.googlecode.com/svn/api/index.html

I am getting the feeling you are getting ahead of yourself. Use the non-RMI chat example linked above to help you understand what is going on, but don’t copy and paste it and don’t just edit it as a basis for your app. Instead, start a new, empty project, and use the Kryo and KryoNet docs to create a client and a server. If you don’t understand something, consult the docs, consult the javadocs, consult the Kryo and KryoNet example code and tests, Google it, and search the Kryo and KryoNet discussion groups. If you still don’t understand it, post a question on the Kryo or KryoNet discussion group. No matter what, DO NOT continue until you understand all the previous steps you have taken.

Note that the Kryo and KryoNet discussion groups each have over 50 members that all receive an email when you send your question. Take time drafting your question. Make sure it clearly and succinctly describes the problem you are having, what you have tried, and what you would like to happen. Many people will take your grammar errors and lack of capitalization and punctuation as a sign that you have not put significant thought into your question, and this can cause many people to not put significant thought into answering you. Some people will not even bother responding. I understand English may be your second language, however, non-English keyboards still have a “shift” key and using extremely informal words such as “u” and “wanna” implies you have not taken the time to write a well thought out question.