The next step is to define a interface will be a kind JAVA designed to define the action of drawing graphs and assign to objects, so that the class in charge of painting the recognized.
A interface in Java is a collection of abstract methods and properties. These specify what should be done but no implementation. Will the classes that implement these interfaces that describe the behavior of logic methods.(W)
A “Interface” similarly declares a “Class” I do not JAVA, is stored in a file with extension “.java” and its name must match the interface.
On “IDE” add an interface to project similar to the one we have seen from the menu:
or we can do through the graphical menu as shown in Figure
In the window that appears to define the interface we have to indicate the name and the package we want to be saved.
We will use the package “graph” where we are generating the library and we will call “SimpleDrawable” since the interface will be responsible for defining the property of being paintable (drawable) the objects that implement.
By implementing the interface only define a method or function:
This feature will allow can represent objects that implement,
Then create a graphic objectto implement this “interface”, allowing us to understand the basic operation of this object-oriented methodology and based on “interfaces”.
The object will be to implement a single point in two dimensions, class “Point2D”. What added to the package “graph” like other elements of our bookstore, later, we can re-build more complex objects.
The declared purpose a pair of variables involved in depositing point coordinates defining. The variable “x” maintain the orderly and “and” the abscissa of the point.
We will have two constructors of the class, one that takes no parameters and one that will serve to launch the object with specific coordinates.
public Point2D(int x1, int y1)
The class will implement the “interdace SimpleDrawable”. This will require implementing the method “paint” is stated in the “interface” and will serve to make the object is represented graphically on screen.
For the class responsible to store and paint the objects meet your requirements we need to define a way to store the graphic objects.
One solution is to define a “Vector” JAVA. This class is in a package of those distributed in the core of the application, and represents a dynamic memory storage.
Add a function called “addElement” who will be responsible for adding items to the database. This class must be public to allow access to it from other kinds of program.
We will implement the logic that paints the objects in the method “paint”. This function will analyze each of the elements found in the database (“Vector” storage) and check first whether interface derived “SimpleDrawable” to know if they are representable. Should be, force type conversion and call your function “paint”.
To check if the object implements the “interface” use checking with the keyword “instanceof“
This logic can be implemented in different ways. In the first case has an integrated structure type “for-next” To extract the “Vector”. In the second case presented an alternative with a more generic object orientation, using a “Enumeration“.
Please check your basic JAVA programming manuals for detailed analysis of these alternatives.
The project structure has changed with the addition of both “interface” as Class “Point2D”. The graphics library begins to complete its basic operating structure.
We can only change the sample main program to add some objects to the database that we have defined in the class “Blackboard”.
To add items we have defined a new function called “iniciarElementos()” will create three new items to the operator “new” and, by calling the function “addElement” Object “slate” , incorporate them into our class Object Management.
The result of the execution of the program is shown in the attached image..
The points you have stored in the database are represented in the coordinates with which we initialized.
If objects start with the constructor parameters, be represented by the coordinates (0,0) superimposed on each other, as they would have default values.
The next step in our structure will see how to incorporate the interaction program. First define a animation engine elements that interact to manage or move alone, and then show you how to manipulate objects with the mouse.