Graphic PIZiadas

Graphic PIZiadas

My world is in..

DrawWorld ! [JAVA]

We started a series of articles that introduce the programming language JAVA guiding its approach to the production of applets with graphic content.

The framework of a blog is especially useful for testing, environment and twitter (su API) may serve to illustrate its application in the data access available on social networks.

JAVA It is a language that can also be used for programming mobile devices. The Android operating system is a clear example of how far the expansion has come from this language, because their applications are programmed in Java before being converted to a code that understands the terminal.

Does this make sense training material? Not enough books, Tutorials, manuals for a new introduction?. Does it depend on the approach we take to work? Fundamentally makes sense if we can bring something new, a “value added”.

  • A approach oriented production software with graphical content It is a model that is currently not well documented (paradoxical in environments that are increasingly visual).
  • A training-oriented graphical programming approach university technical environments pragmatism can bring in the realization of the examples.

HelloWorld adapted to graphics

Overall everything manual to learn a programming language begins by explaining the “set of instructions and labels” or also called “code”, which is necessary for, simply, write text; this code is in the form of a sample program and is often referred HelloWorld.

In our example we will not write a text (we could do graphically); instead draw a simple circle on the screen (viewport) in which the application is displayed. Using a semantic parallelism, we can call the first program DrawWorld.

import java.applet.Applet;
import java.awt.Graphics;
/**
* @author joju
*/
public class DrawWorld extends Applet {
public DrawWorld() { }
public static void main(String[] args) { }
public void paint(Graphics g){
g.drawOval(10, 10, 100, 100);
}
}

The above text should be in a file called DrawWorld.java execute it from the command line or from an IDE development as seen later. We will initially focus on understanding the taxonomy of program.

Importing classes

The first lines:

 

 

 

import java.applet.Applet;
import java.awt.Graphics;
indicate that will use objects and functions defined in other files- In particular the class “Applet” serves as a major element of the program, as they execute the same functionality through implementing. To run in a browser need a “objeto Applet”;

Class documentation

To document the code reviews are used. A comment may be limited to a line of text or use several complete lines.

  • Two bars are used “//” to indicate the start of a comment which ends at the end of the line.

// This is a comment

  • Used (style JAVA) bar and two asterisks “/**” (it can also be a single asterisk”/*“) to indicate the beginning of the commentary that you can use multiple lines. The review closes with the inverted sequence of the bar and the asterisk, ie with “*/“.
/**
* @author joju
*/

 

 

 

The review may include “tags” subsequently facilitate automatic generation of help documentation. The “author” program, in this case, can be found from within the application to have been declared in the commentary.

Class

JAVA fichero, ie extension “.java” contains a “Class”. The file name and the class name must be the same.

public class DrawWorld extends Applet {
}

File DrawWorld.java contains a public class DrawWorld using or “extends” class “Applet“. This means that it is actually DrawWorld applet. We can incorporate a web page your applet, as it has inherited all the capabilities to use the operator “extends“.

The implementation of the functionality of the “class” is performed using variables and functions that are among the keys “{……}” that delimit.

Class Constructor

I program to initiate or request a new object in JAVA operator “new”, the application executes the code found in what is called the “class constructor”. We may think that is a “starting point”

public DrawWorld() { }

In our simple example is merely empty and therefore any action performed.

Another starting point is the application of the method “main”, used to start the program if run as a non-embedded application in a browser

public static void main(String[] args) { }

Drawing on the screen

Our classic greeting will become a simple drawing. The method “paint” It is responsible for “draw” application. Each time the screen is refreshed the system automatically calls this method, in which we have incorporated a simple line of code to “paint” una curva oval (the ellipse circumference in this case) to be found within the space of a square bounding 100 Length units at coordinates (10,10):

 

public void paint(Graphics g){
g.drawOval(10, 10, 100, 100);
}
Function “drawOval” is a procedure with the “clase Graphics”. JAVA need to draw an object of this nature (objeto Graphics), that is implemented in the core of the application. The paint function takes as argument a Graphics object that is labeled with the letter “g” in a standard.
To access the object's methods “Graphics” add a “point” name (g) Object and then the name of the function.

 

From hand we O'REILLY publishing a document of great interest.

Java 2D Graphics: One of the most interesting books of introduction to graphic programming with JAVA. Scroll in every aspect of the Java2D graphics library with examples that highlight the most important aspects and used.

A classic 2D programming in JAVA

 

JAVA

JAVA Course