Javascript required
Skip to content Skip to sidebar Skip to footer

How to Make a Moving Object in Java

Once you've created an object, you will very likely want to use it for something. Suppose, for example, that you would like to move your new rectangle to a different location (the rectangle is an object in a drawing program and the user just clicked the mouse over the rectangle and dragged it to a new location).

The Rectangle class provides two equivalent ways to move the rectangle:

  1. manipulate the object's x, y variables directly
  2. call the move() method
Option 2 is often considered "more object-oriented" and safer because you manipulate the object's variables indirectly through its protective layer of methods rather than twiddling directly with them. Manipulating an object's variables directly is often considered error-prone; you could potentially put the object into an inconsistent state. However, a class would not (and should not) make its variables available for direct manipulation by other objects if it were possible for those manipulations to put the object in an inconsistent state. So, we can assume that manipulating a Rectangle's x and y variables directly is as safe as calling its move()method.

A class can restrict or allow access to its instances' variables and methods by other objects. The following two sections discuss calling methods and manipulating variables that have been made accessible to other objects. To learn more about controlling access to member variables refer to Controlling Access to Member Variables. And for more information about controlling access to methods refer to Controlling Access to Methods.

Referencing an Object's Variables

First, let's focus on how to inspect and modify the Rectangle's position by modifying its x and y variables. The next section will show you how to move the rectangle by calling its move() method.

To reference an object's variables, simply append the variable name to an object reference with an intervening '.' (period).

              objectReference.variable            
You can use this notation to either view or modify an object's variables. For example, if you have a rectangle named rect you reference its x and y variables with rect.x and rect.y, respectively. Now that you have a way to reference rect's variables, you can use those references in a Java statement and expressions as though they were "regular" variables. Thus to move the rectangle a new location you can simply use these references in two assignment statements, like this:
rect.x = 15;	// change x position rect.y = 37;	// change y position            
The Rectangle class has two other accessible variables--width and height. So you can use the same notation to reference those Rectangle attributes: rect.width and rect.height. Thus you can calculate the rectangle's area using this statement:
area = rect.height * rect.width;            
When you access a variable through an object, you are referencing that particular object's variables. So, if bob is also a rectangle with a different height and width than rect, this instruction:
area = bob.height * bob.width;            
calculates the area of the rectangle named bob and will give a different result than the previous instruction (which calculates the area of the rectangle named rect).

Note that the first part of a variable reference (the objectReference in objectReference.variable ) must be a reference to an object. You are not limited to just using an object's name here (if it even has one); you can use an expression to specify the object whose variables you want to inspect. One common use of an expression-as-object-reference is just after an object is created. For example, you can find out the height of a new, uninitialized Rectangle with:

height = new Rectangle().height;            

Calling an Object's Method

To call an object's method, simply append the method name to an object reference with an intervening '.' (period), and provide any arguments to the method within enclosing parentheses. If the method does not require any arguments, just use empty parentheses.
              objectReference.methodName(argumentList);              or              objectReference.methodName();            
So let's see what this means in terms of moving the rectangle. To move rect to a new location using its move() method write this:
rect.move(15, 37);            
This Java statement calls rect's move() method with two integer parameters, 15 and 37. This statement has the effect of moving the rect object by modifying its x and y variables and is equivalent to the assignment statments used previously:
rect.x = 15; rect.y = 37;            
If you want to move a different rectangle, the one named bob, to a new location you would write:
bob.move(244, 47);            
As you see from these examples, method calls are directed at a specific object; the object specified in the method call is the object that responds to the instruction. Method calls are also called messages. Just like real-world messages, object messages must be addressed to a particular recipient. You get different results depending on which object is the recipient of the message. In the example above, when you send the object named rect a move() message, rect moves to the new location. When you send the object named bob a move() message, bob moves. Very different results. To understand messages more fully, please see the page What are Messages?(in the Writing Java Programs trail)

A method call is an expression and evaluates to some value. The value of a method call is its return value, if it has one. You will often wish to assign the return value of a method to a variable or use the method call within the scope of another expression or statement. The move() method doesn't return a value (it's declared void). However, Rectangle's inside() method does. The inside() method takes an x, y coordinate and returns true if that point lies within the rectangle. So you could use the inside() method to determine if some point, say the current mouse location, was inside the rectangle and do something special if it were.

if (rect.inside(mouse.x, mouse.y)) { 	// mouse is in the rectangle     . . . } else {			 	// mouse is outside of the rectangle     . . . }            
Remember that the method call is like a message to the object named. In this case, the object named is the Rectangle named rect. Thus,
rect.inside(mouse.x, mouse.y)            
is asking rect if the mouse cursor location represented by mouse.x and mouse.y is contained within it. You would likely get a different response if you sent the same message to bob.

As stated previously, the objectReference in the method call objectReference.method() must be a reference to an object reference. You are not limited to using the object's name here (if it even has one), you can use an expression to determine the object to which to send a message. One common use of an expression as object reference is just after an object is created. For example, you can create an object and immediately thereafter call one of its methods:

new Rectangle(0, 0, 100, 50).equals(anotherRect)            
The expression new Rectangle(0, 0, 100, 50) evaluates to an object reference that refers to a Rectangle object. Thus you can use the dot notation to then call the new rectangle's equals() method to determine if the new rectangle is equal to the one specified in equals()'s argument list.

How to Make a Moving Object in Java

Source: https://www.cs.princeton.edu/courses/archive/spr96/cs333/java/tutorial/java/javaOO/usingobject.html