After installing development environment (IDE) The Eclipse and create a new project, We can add our first graphical program. The first objective is to define the minimal structure needed to draw a straight line in a window, later, generate a complex structure that supports advanced graphics based on a model of object-oriented programming.
Add a JAVA file to the project
We can add a file from the alphanumeric menu or the menu icon graphic.
En el primer caso, as shown in the image, select:
In the second case will click the icon representing a folder and has the symbol “+”- Then, from the menu displayed, select “Class“. Actually we will be creating a file with “JAVA” that occur when compiling another extension “class” containing the “bytecodes” or executable code.
In both cases, A new window will immediately appear to define a set of data needed to create the new file, that we complete. in which we can keep some default settings or modify according to our needs.
All files need to be stored somewhere. This information will appear in the field “Source folder” usually no need to modify.
The most important field is the file name (Name) that, to meet the standard JAVA, It should begin with a capital letter.
If we want our first file executable, namely, have an entry point to the program, should extend (have the right properties) of an object type JAVA: a Applet.
This is defined in the field “Superclass”. We can introduce the name of the object which will lead our program, or look on the set of libraries that accompany the development kit, in which case press the button “Browse“.
Pressing “Browse” will display a new window to find the object that will extend our program. If we begin to write the word “Applet” in cudro “Choose a type” us items that match the search are. Select Applet and click the OK button. Back to previous page, after Bookmarking options listed, pulsating finish Finish.
Options marked us create two basic functions to start the program.
Content is simple, defines two functions that will serve to implement the program in one of two possible modes: Applet in a browser or standalone program.
Function main() be the entry point when the program is run independently. If you need the constructor or function is executed implicitly Example1 we call.
We can now run the program to see the result. We must therefore define the embodiment. In the first instance we will use the mode “Applet” that serve to embed the code in a web page.
The menu button that launches the application is represented by a green triangle, button-like “play” of a video or music player. Along with this, find a small arrow that allow us to adjust some parameters. The select to set the execution mode.
We can also place the mouse cursor over the file “Example1” and right-click get a popup menu with the same options. Therefore select;
Run As -> Java Applet
The application will launch an emulator and get a window or “Visor they Applets” in which our program runs. At this point the window will be empty, because our code is only the structural part and we have not defined any action to be taken.
To draw a first line, substantially modify the code. We introduce a function to diujar: paint(Graphics g).
This function overloading function drawn from Applet.Es say, the applet has an identical function will use to run which will define.
The error occurs because the system can not identify what is “Graphics”.
Graphics is a library for drawing that takes the core JAVA, and we indicate (import) location to use. This is done with the judgment “import” that will place the top of the program.
To add the sentence automatically will place the mouse cursor on the symbol error. Pressing the right button will have a menu with options corrective error. We select “Import ‘Graphics’ (java.awt) so that the IDE automatically adds the required instruction.
At the top of the file includes a new line with the sentence:
Return to run the program to see the changes. In this case we can simply press the button Run, since we have previously defined how we wanted to perform this operation: as an Applet.
The result is visible in the “Visor they Applets”. A line is drawn in the coordinates entered. We built our first graphical program.
To end this introduction we can revise Article DrawWorld ! We detailing some aspects of those referred to in this tutorial, and a more complex program with the code you will find in Recursive Fractals: Sierpinski Triangle.
Gradually delve into the functions available to draw, and advance in the complexity of our program structure, introducing interactive graphics techniques to manipulate.