-In this tutorial, I create a Point-Line-Rectangle system. In the coordinate system, x and y values of a point will be the root. Firstly, I build a Point class then the Point class will support us to build the Line class and the Rectangle class. Download all files here:
private int y;
or
private int x, y;
Java point class
Learn point class in java with basic methods:
-The following code will create the basic for point class in java class:- Remember that for the instance variables, we can initialize in different ways:
private int y;
or
private int x, y;
- The getter methods help us retrieve the values of x and y coordinate because in some situation we want to use them to execute some calculation. The getter methods will return an integer value.
- Setter methods are optional. In some cases, we don't really need them, but here i also display for you to understand how i use it. The setter methods simply set instance variables equal there new values. Their new values will be passed into the setter methods by the users. Just clarify, "this.x" and "this.y", x and y here are the instance variables of the object. "x" and "y" mean the parameters we pass in.
- To display the values of the object, people often prefer to override the toString() method, either do i. To override the default toString() method of the Point class, you have to name it "toString()" and the return value has to be a string.
- After override and run the system, I got a result as follow:
- If i didn't override the toString() method, the output value will be:
- You can create a new class which is called PointApp as the following code to test the class Point:
Point class in java-move one point to another point:
-Now i will add a simple method into the point class in java which will move our object point to another point, watch the following code:- To clarify, we are still in the javapoint class. In the moveTo() method, the data type of the "other" parameter is Point. When we pass an other point into the moveTo(), it simply set the x and y values of the point object equal the x and y values of the "other" point. Here we can understand as telling the point object move to other point. We can test the moveTo() method by the following code:
Point class in java-java distance between two points:
-In javapoint class, we usually need a method to calculate the distance between the object point and the other point. In math, the distance between two points in coordinate system is calculated by the following formula:- To apply this formula into java programming we do as the following code:
- Here you can see, we have two methods to find out the distance. The first method differnceBetweenTwoPoints (parameter is a Point type), it will figure out the difference of x and y values between the object point and the passing point. This method will return a Point type. Here you can see the newX and newY are defined with integer data type and they contain the value of the difference between two x and two y corresponding. Then we construct those newX and newY into a Point.
- The second method distance() method, which will calculate the distance between two points. The first line in this method, it will call the differnceBetweenTwoPoints method in order to identify the difference of x and y between two points. The second line, it applies the distance formula and use getX(), getY() methods which we have created in the previous parts to get the x and y values of two points to insert into the formula. The final line, it will return the result according to the returned data type of the method. Because we declared the distance as a double data type but the return value of the method is integer data type, then we have to cast double into integer, using the round() method which is supported by java to round our result.
- To test the distance method you can apply the following code:
Point class in java-move a point by an offset point:
- Now, it's turn for moveBy() method, which will move the object point by an offset point (plus x and y values of the object point by x and y values of the offset point):- The asNegative() method will return a point. This method will add a subtract operator (-) to the x and y values of the offset point in order to reuse the differentBetweenTwoPoints method (now the operator inside the differentBetweenTwoPoints method will be plus (+) not minus (-) anymore).
- The moveBy() method with no return value, calling the asNegative() method to add an minus operator (-) before the x and y values of offset point. Then it call the differentBetweenTwoPoints() method to plus the object point by the offset point and return a new point. Finally, it set the x and y values of the object point by the x and y values of the new point.
- You can test the moveBy() method by apply the following code:
Java Line class
Java line- basic methods:
- After we created class Point, we continue to create class Line with the support from Point class. You can check the following code:- The instance variables of Line object will have Point type, which is from class point. Here we have the start point and the end point of the Line, they are instance variables.
- Two constructors will be built in the Line class. One constructor (the first one) will take four integer numbers which are passed into the Line class by the users, then build those four numbers into two points (start point and end point of the line object). One constructor (the second one) will directly take two passing points (two passing parameters) and construct them into the start point and end point of Line object.
- Similar to the Point class, Line class also has getter methods which help us retrieve the start point and end point values, and setter methods which help us set the values of those start and end points.
- Finally, overriding the toString() method by a new toString() method to show off the result in the order we want.
- To run the Line class you can create a new class which is called LineApp.java, consider the following code:
Java line-move line by an offset point:
- Similar to the point class, we will move the start point and the end point of the line object by a point offset (type point). To do this, we just add a method which is called moveLineBy() into the Line class, considering the following code:- The idea here is move startPoint and end Point of the line by an offset point. Remember we created a moveBy() method in class Point, now we just need recall to that method to reuse it, this will make the action of moving the line become much more easier, as you can see above, it is just a couple lines of code.
- The moveBy() method in class Point automatically set new values for x and y of the start and end points. Considering the following code to run the moveLineBy() method:
Java line-move end and start with certain conditions:
- The next method we will add into the java line class would be called moveEnd(). The idea of this method is having two parameters, one is Point type and one is boolean type (true/false). We simply move the endPoint of the line object to the point parameter, then if true, we continue moving the startPoint of the line object by a point offset which is calculated from the difference between the endPoint of the line and the point parameter, see the following code:- p (Point type) and b (boolean type) are two parameters which are passed into the moveEnd() method.
- Find the offset between the endPoint of the line and the p parameter. Remember, we have the differentBetweenTwoPoints() method in the Point class, that will help us to find out the offset, now we just recall to reuse it. Example for an offset point: if we want to move point A=(3, 4) to point B=(15, 30), we have to add an offset to x and y coordinates of the point A. In detail, we have 3 + 12 =15 and 4 + 26 = 30, then we find out the offset C=(12, 26).
- In this step, we will check if the b parameter is true, we will proceed to the next step.
- In step 4, we recall the moveBy() method in the class Point in order to move the startPoint of the line by the offset which we had found in the step 2. This method will automatically set the x and y of the startPoint for us.
- Step 5, we simply set the endPoint of the line object by the p parameter.
- Step 6, we check if the b parameter is false then proceed to the next step.
- Step 7, if b parameter is false, we just do one thing which is set the value of the endPoint by the p parameter.
Java line-length of the line object:
- One more method in java Line class, which is called lengh() method. The idea of this method is finding out the length of the line object. Watch the following code:- Remember, we have the distance() method in class Point, which will figure out the distance between two points or we can say the length from this point to other point. Now, we recall to that distance() method to reuse it in order to find out the length of our line object.
- Run the following code to test the length() method:
Java rectangle class
Java rectangle basic methods:
- If you are here, you are about to finish the Point-Line-Rectangle class system. The following code will build some basic parts of the java Rectangle class:- We have three instance variables which are leftTop (Point type), width (int type) and height (int type).
- Two constructors are built. The first one will have three parameters, one with point type which is set as the leftTop instance variable, one with integer type which is set as the width instance variable, and the last one with integer type and set as the height instance variable. The second constructor will have four integer type parameters. Inside of this, it will construct the leftTop instance by the first two variables, the width instance by the third parameter, and the height instance by the fourth parameter.
- Similar to the Point and Line class, the java Rectangle class also has getter methods to help us access to the instance variables. Setter methods help us to set those instance variables.
- Of course, overriding the toString() method to show off the result in an arrangement we desire.
- To run those basic methods in Rectangle class, we might create RectangleApp.java to test it, see the following code:
Java rectangle-contain point method:
- Now we will create a method in java rectangle class which is called containPoint() to determine whether a point is inside the object Rectangle or not. To do that, first, we have to add some more helper methods to the Rectangle class. Watch the following code:- The methods from 1 to 4 will get the sides of the rectangle object. To have a better understand them you follow the virtual chart below:
- After having four sides of the rectangle, we apply them to the methods 5 and 6 to determine whether x and y values of a specific point inside the rectangle object or not. Example: if there is a point A=(4, 6), then in the method 5 will be 2<4<9, so x value inside the rectangle. Similar to the method 5, the method 6 will be 2<6<8, so y value inside the rectangle.
- Finally, in the method 7, the containPoint() method will recall the methods 5 and 6 and return true value if the conditions inside are right.
- To detail running those above methods, you might follow the code below:
Java rectangle contain line:
- Next, we will build a method which is called containLine(). It has one parameter with Line type. It's job is figure out whether a line inside the object rectangle or not. See the following code:- The condition for a line to be inside a rectangle is the startPoint and endPoint of the line both inside the rectangle. Remember, in the previous section, we had created containPoint method, which help us determine if a point inside a rectangle or not. Now, we just recall that method to determine the startPoint and endPoint of the line inside the rectangle.
- The following code will test the contaniLine method for us:
Java rectangle contain other rectangle:
- The final method that i want to add into the java Rectangle class is containRectangle(). It has one parameter with Rectangle type. Similar the previous two methods, this method's job will determine whether a bigger rectangle contains a smaller rectangle or not. See the following code:We have three conditions to determine whether a rectangle being inside another bigger rectangle or not:
- First, the leftTop instance variable of the smaller rectangle has to be inside the bigger rectangle. To check this condition, we recall the containPoint method which was created in the previous section.
- Second, getRight of the smaller rectangle has to be less than or equals getRight of the bigger rectangle .
- Third, getBottom of the smaller rectangle has to be less than or equals getBottom of the bigger rectangle .
- For the second and third conditions, you follow a virtual chart below to have a better understand:
- The final step, it will be running the containRectangle method:
-Now, we are done with the Point-Line-Rectangle system, which are very important to know if you intend to work with the coordinate system and graphic in the future.
-Next article, i will draw some kind of fans in graphic. Which also relates to the Point-Line-Rectangle system we have learned today, see you!
Comments
Post a Comment