Graphic PIZiadas

Graphic PIZiadas

My world is in..

Graphical Programming: Animated Graphics Engine [JAVA]

motor_animaciónAfter defining a first interface to draw graphical objects in JAVA, we will implement a engine animation that allows us to manage the dynamics of the application and, later, add user interaction.

The engine is responsible for calling functions that encourage each of the graphic objects of the database.

A game engine is a term referring to a series of programming routines allowing the design, the creation and representation of a game.

Similarly, there are game engines operating in both game consoles and OS. The basic functionality of a game engine is to provide a rendering engine for 2D and3D, the physics engine collision detector, sounds, scripting, animation, artificial intelligence, networks, streaming, memory management and a scene graph.(W).

Our first engine implementation will rely on the functionality that we have in the class responsible for the storage and representation: “Blackboard“. Define a new interface to add new functionality, the possibility of animate an object.

Interface Animable

The new “interface” declare a single method that will incorporate the classes that implement.

package graph;

public interface Animatable {
	public void soul();

For example, we can modify one of our classes to be anime graphics, or create a new one that extends the functionality of other, as is the case of the new class Point2DAnimable that extends the class Point2D to store coordinates, and implements the interface animatable encourages the function only changes the coordinates stored randomly.

package graph;

public class Point2DAnimable extends Point2D implements Animatable{
	public Point2DAnimable(int x, int and){
	public void soul() {
		x =(int)(Math.random()*5);
		and   =(int)(Math.random()*5);

Add new objects to the storage system Blackboard class from the main applett

	private void iniciarElementos() {
		pizarra.addElement(new Point2DAnimable(50,50));
		pizarra.addElement(new Point2D(70,50));
		pizarra.addElement(new Point2D(50,70));

To animate the objects in the database, implement a new method in the class Blackboard that will analyze whether they are animatable, in which case the function call itself encourages each object

	public void anima(){
		Enumeration enum=bd.elements();
		Object ob;
			ob = enum.nextElement();
			if(ob instanceof Animable){
				((Animatable) whether).soul();


Animation engine

The process that handles updating of animation is what we call animation engine (constructed as a thread or “Thread“). Your responsibility is to call the animation procedure we implemented in class Blackboard.

In OS, a thread, strand or thread is the smallest unit of processing that can be scheduled by an operating system.

A thread (Thread) is a feature that allows an application to perform several tasks at once (concurrently). The different threads share a number of resources such as memory space, open files, Authentication situation, etc.. This technique simplifies the design of an application that must perform different functions simultaneously.

A thread is basically a task that can be executed in parallel with another task.(W)

Our animation engine implement the class “Runnable” to have start and stop methods.

package graph;

public class MotorGrafico implements Runnable {
long interval = 100;

We will have a series of variables Boolean control to pause the animation.

boolean parar = false;
public void stop() { stop = true; }

boolean paused = false;
void pausar() { paused = !slow; }

The constructor of the class will receive an object “Blackboard” as a parameter. Serve to know what element has the main animation function. This object will store it in a variable to reference it later.

private Blackboard graph;
public MotorGrafico(Blackboard graph) {
	this.count = count;

Before starting the thread we must ensure that there is active, in which case we must destroy it to not run anything more than one process at a time.

private volatile Thread blinker;
public void start() {
		blinker =zero;
	blinker = new Thread(this);

To stop the animation engine will destroy the thread, simply assigning a null value to the variable that controls the “Thread”.

public void stop() {
	blinker = zero;

The main process is performed in the function “run()”.

First check we do not want to stop program execution, and verify that the thread is right. If so, suspend program execution milliseconds with the call to “sleep” and, to resume, animation method call. Once again animated graphic elements to draw (repaint)

public void run() {
	Thread thisThread = Thread.currentThread();

	while (!stop && blinker == thisThread) {
		try {
		}  catch (InterruptedException e) {}

		if (graf != zero && !slow) {

Function “animate” call the function “soul()” class “Blackboard”, responsible for storing the graph database.

	private void animate() {

Implementation of the program

The sample class should include the declaration of an object “MotorGrafico” which will pass the corresponding parameter to locate the graphic display or “Blackboard”.

public class Example1 extends Applet {
	Blackboard slate =new Blackboard();
	MotorGrafico motor =new MotorGrafico(slate);

Also, we start the animation engine by calling the method “start()”

	public Example1() throws HeadlessException {
		this.setLayout(new GridLayout());

It remains only to add some elements animatable graphics database to complete the exercise. We will see that some points do not move, while implementing the interface “Animatable” if they do randomly.

	private void iniciarElementos() {
		pizarra.addElement(new Point2DAnimable(50,50));
		pizarra.addElement(new Point2DAnimable(60,50));
		pizarra.addElement(new Point2DAnimable(50,80));
		pizarra.addElement(new Point2DAnimable(50,70));
		pizarra.addElement(new Point2D(70,50));
		pizarra.addElement(new Point2D(50,70));

JAVA Course