How to Make a Moving Object in Java
The Rectangle class provides two equivalent ways to move the rectangle:
- manipulate the object's
x
,y
variables directly - call the
move()
method
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 itsx
andy
variables. The next section will show you how to move the rectangle by calling itsmove()
method.To reference an object's variables, simply append the variable name to an object reference with an intervening '.' (period).
You can use this notation to either view or modify an object's variables. For example, if you have a rectangle namedobjectReference.variablerect
you reference itsx
andy
variables withrect.x
andrect.y
, respectively. Now that you have a way to referencerect
'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:The Rectangle class has two other accessible variables--rect.x = 15; // change x position rect.y = 37; // change y positionwidth
andheight
. So you can use the same notation to reference those Rectangle attributes:rect.width
andrect.height
. Thus you can calculate the rectangle's area using this statement:When you access a variable through an object, you are referencing that particular object's variables. So, ifarea = rect.height * rect.width;bob
is also a rectangle with a different height and width thanrect
, this instruction:calculates the area of the rectangle namedarea = bob.height * bob.width;bob
and will give a different result than the previous instruction (which calculates the area of the rectangle namedrect
).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.So let's see what this means in terms of moving the rectangle. To moveobjectReference.methodName(argumentList); or objectReference.methodName();rect
to a new location using itsmove()
method write this:This Java statement callsrect.move(15, 37);rect
'smove()
method with two integer parameters, 15 and 37. This statement has the effect of moving therect
object by modifying itsx
andy
variables and is equivalent to the assignment statments used previously:If you want to move a different rectangle, the one namedrect.x = 15; rect.y = 37;bob
, to a new location you would write: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 namedbob.move(244, 47);rect
amove()
message,rect
moves to the new location. When you send the object namedbob
amove()
message,bob
moves. Very different results. To understand messages more fully, please see the page What are Messages?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'sinside()
method does. Theinside()
method takes an x, y coordinate and returns true if that point lies within the rectangle. So you could use theinside()
method to determine if some point, say the current mouse location, was inside the rectangle and do something special if it were.Remember that the method call is like a message to the object named. In this case, the object named is the Rectangle namedif (rect.inside(mouse.x, mouse.y)) { // mouse is in the rectangle . . . } else { // mouse is outside of the rectangle . . . }rect
. Thus,is askingrect.inside(mouse.x, mouse.y)rect
if the mouse cursor location represented bymouse.x
andmouse.y
is contained within it. You would likely get a different response if you sent the same message tobob
.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:The expressionnew Rectangle(0, 0, 100, 50).equals(anotherRect)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'sequals()
method to determine if the new rectangle is equal to the one specified inequals()
'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