What is java

Java is an object-oriented, operating system-independent programming language that was introduced in 1995 by Sun Microsystems. The language is constantly being developed, version 9 has been available since September 2017. You can use the Java Development Kit (JDK) to develop a Java application. A Java Runtime Environment (JRE) is required on the target computer to run a Java application. Both software packages can be downloaded from the Oracle website ( www.oracle.com ), which Sun Microsystems acquired in 2010.

Various applications can be created using Java, including:

  • Standard desktop applications,
  • Internet applications
  • Applets, i.e. applications that run within an Internet browser and
  • Apps for smartphones and tablets, for example Android apps.

How do I work with Java?

You can develop Java applications with simple means. You can write the Java program using a simple text editor and save it in a file with the extension java. Using the compiler from the JDK, you can use this to create bytecode in a file with the extension class. Thanks to the JRE, this can be carried out on the target computer.

However, an integrated development environment (IDE) is very useful when building Java applications. Examples are NetBeans (netbeans.org) and Eclipse (eclipse.org). Both software packages were developed on the basis of Java. They were primarily intended for developing Java applications. However, they can also be used for software development in other languages, for example for C, C ++ or PHP.

What is JavaFX?

The JavaFX framework offers the possibility to develop Java applications with graphical user interfaces. It has been completely redeveloped since 2008 and is intended to replace the older program libraries AWT (Abstract Window Toolkit) and Swing. JavaFX can also be used to create two-dimensional drawings and animations (2D) as well as the 2D representation of three-dimensional drawings and animations (3D). It can directly access the corresponding interfaces of modern graphics cards.

An example program

In this section follows an example of a simple 2D animation using JavaFX. First, an object is created in the upper left corner of the screen: a yellow square with a black border. A “film” then runs, during which the object is animated in different ways one after the other, each lasting two seconds:

  • The object is moved to the right (see Figure 1),
  • then to the left and
  • right again,
  • the object is moved downwards (see Figure 2),
  • the fill color of the object is changed to green (see Figure 3),
  • the color of the edge of the object is changed to red,
  • the object is moved to the top left and rotated 45 degrees at the same time (see Figure 4) and
  • the object becomes completely transparent, so it disappears.

The entire process then runs back to the start state.

If you already have knowledge of another programming language, you will find many familiar elements in the code and the short comments, but also many elements that are specific to Java. Of course, this demo example is not yet suitable for a thorough learning of Java.

The code of the program

The following is the program code in the “Fx2DAnimation.java” file:
// Zuordnung der Java-Klasse zu einem Package
package fx2danimation;

// Benötigte JavaFX-Bibliotheken
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;

import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;

import javafx.animation.*;
import javafx.util.*;

// Klasse, erbt von JavaFX-Klasse
public class Fx2DAnimation extends Application
{
// Hauptmethode der Java-Anwendung
public static void main(String[] args)
{
// Start der Anwendung
launch(args);
}

// Methode start() der JavaFX-Klasse Application muss überschrieben werden
// Verweis auf Hauptfenster = Bühne wird geliefert
@Override
public void start(Stage hauptFenster)
{
// Layout-Element, das die Zeichnungselemente beinhaltet
Pane pn = new Pane();

// Rechteck-Objekt erzeugen, Lage X=2 / Y=2, Breite=50 / Höhe=50
Rectangle re = new Rectangle(2, 2, 50, 50);
// Füllfarbe, Liniendicke und -farbe
re.setFill(Color.YELLOW);
re.setStrokeWidth(5);
re.setStroke(Color.BLACK);
// Rechteck dem Layout-Element zuordnen
pn.getChildren().add(re);

// Animations-Objekt erzeugen, vom Typ “Verschiebung”
// dauert zwei Sekunden, bezieht sich auf das Rechteck
TranslateTransition tt1 =
new TranslateTransition(Duration.millis(2000), re);
// Animation verschiebt nach rechts, von X=2 auf X=100
tt1.setToX(100);
// Animation durchläuft drei Zyklen
tt1.setCycleCount(3);
// Animation läuft zurück zu X=2, zählt auch als Zyklus
tt1.setAutoReverse(true);

TranslateTransition tt2 =
new TranslateTransition(Duration.millis(2000), re);
// Animation verschiebt nach unten, von Y=2 auf Y=100
tt2.setToY(100);

// Animations-Objekt erzeugen, vom Typ “Ändern der Füllfarbe”
FillTransition fi3 =
new FillTransition(Duration.millis(2000), re);
// Animation ändert Füllfarbe von Gelb zu Grün
fi3.setToValue(Color.GREEN);

// Animations-Objekt erzeugen, vom Typ “Ändern der Linienfarbe”
StrokeTransition sr4 =
new StrokeTransition(Duration.millis(2000), re);
// Animation ändert Linienfarbe von Schwarz zu Rot
sr4.setToValue(Color.RED);

TranslateTransition tt5 =
new TranslateTransition(Duration.millis(2000), re);
// Animation ändert zwei Eigenschaften gleichzeitig
tt5.setToX(50);
tt5.setToY(50);

// Animations-Objekt erzeugen, vom Typ “Drehung”
RotateTransition rt6 =
new RotateTransition(Duration.millis(2000), re);
// Animation dreht von 0 Grad (=Standard) zu 45 Grad
rt6.setToAngle(45);

// Animations-Objekt erzeugen
//    vom Typ “Sammlung für gleichzeitige Animationen”
//    hat hier keine eigenen Eigenschaften
ParallelTransition pt7 = new ParallelTransition();
// Fasst mehrere Animationen zusammen
pt7.getChildren().addAll(tt5, rt6);

// Animations-Objekt erzeugen, vom Typ “Ändern der Transparenz”
FadeTransition ft8 =
new FadeTransition(Duration.millis(2000), re);
// Verzögerung des Starts einstellen
ft8.setDelay(Duration.millis(1000));
// Animation ändert Transparenz
// von 1 (=Standard, vollständig sichtbar) zu 0 (=vollständig unsichtbar)
ft8.setToValue(0);

// Animations-Objekt erzeugen
//    vom Typ “Sammlung für Animationen nacheinander”
//    ergibt eine Abfolge wie in einem Film
SequentialTransition sq9 = new SequentialTransition();
// Fasst mehrere Animationen in dieser Reihenfolge zusammen
sq9.getChildren().addAll(tt1, tt2, fi3, sr4, pt7, ft8);
// Der gesamte Ablauf läuft wieder zurück bis zum Startzustand
sq9.setCycleCount(2);
sq9.setAutoReverse(true);
sq9.play();

// Titel für Hauptfenster
hauptFenster.setTitle(“2D-Animationen”);
// Szene wird erzeugt, beinhaltet Layout-Element,
// hat Breite und Höhe, wird in Hauptfenster eingebettet
hauptFenster.setScene(new Scene(pn, 400, 200));
// Hauptfenster wird angezeigt
hauptFenster.show();
}
}

Java Guide

Java Guide