I'm currently trying to create a client program that sends location variables (x and y) to a server, which then passes it on to another client and vice versa so when one client moves the object, the other client see's the change immediately on screen.

I think get and set methods are the way forward for my task, but I can't figure out how to do this.

The client creates a new instance of an object which has the following methods:

public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
	 
	 public void setX(int x) {
	 		this.x = x;
	}
	
	public void setY(int y) {
			this.y = y;
	}

The client code has:

player1.getX();
		player1.getY();

While the server code has:

player1.setX();
	    player1.setY();

The object code and client code compile fine, but the server code refuses to compile stating:

setX(int) in Car cannot be applied to ()
            player1.setX();

With the same thing for setY();

Could someone please explain what i'm doing wrong? Or a better method to pass the location coordinates between clients and servers?

Thanks

Recommended Answers

All 5 Replies

If you declare a method as taking an int as a parameter, then you need to pass that int. i.e. in your setX and setY methods, you cannot call setX(), you have to call setX(PUTANINTHERE!!!)

hope that helps.

And one more thing - having getX and getY methods might not be helpful, unless you are returning random x and y positions. If you are actually getting the position of a mouse click or something of that nature, Swing's MouseListener class (and corresponding tutorial) will be helpful. You should start by just writing a client/server pair where the client can send the server the x and y position. Then add on another client, one by one.

If you declare a method as taking an int as a parameter, then you need to pass that int. i.e. in your setX and setY methods, you cannot call setX(), you have to call setX(PUTANINTHERE!!!)

hope that helps.

And one more thing - having getX and getY methods might not be helpful, unless you are returning random x and y positions. If you are actually getting the position of a mouse click or something of that nature, Swing's MouseListener class (and corresponding tutorial) will be helpful. You should start by just writing a client/server pair where the client can send the server the x and y position. Then add on another client, one by one.

Thanks for the quick reply, so basically I need to pass an int in the setX and Y methods, I can't tell it to pass say...the updated location? My initial x and y have values, for example 50, 50 to place the car to that location, I was hoping there was a way to set the updated value after movement takes place to the set method (Movement is controlled via keys, each key either increases or decreases the x or y value which I am trying to pass to the server).

From what you've said I'm guessing I can't do that using setX as it wants me to give it a value rather than being able to do setX(x); or something?

Any ideas how I can go about doing what I have outlined in this post?

Thanks for your help

If "x" is declared as an int variable (which it seems to be) then of course you can pass it to your setX method. You can pass any int variable to a method declared as taking an int. Did I misunderstand your question?

PS if you are waiting for a key to be pressed in order to add to X's value, then you can implement Swing's KeyListener class I think, but it's been a while since I've looked at that.

thank for sharing this useful and informative posting with us its really very useful for us.

If "x" is declared as an int variable (which it seems to be) then of course you can pass it to your setX method. You can pass any int variable to a method declared as taking an int. Did I misunderstand your question?

PS if you are waiting for a key to be pressed in order to add to X's value, then you can implement Swing's KeyListener class I think, but it's been a while since I've looked at that.

No you didn't, thanks it is what I assumed and realised what was preventing me was that x is stored privately (though using get and set methods I was led to believe it made publicly accessible versions which I tried to call and didn't work).

Thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.