8 March 2021

Introduction to GUIs

The JavaFX GUI framework allows you to create modern, professional-looking applications. You will begin to see that inheritance and interfaces are going to play a ginormous role here.

Important Packages

What is an Abstract Class? The class Application is abstract; we will explore the meaning of this.

Abstract classes allow us to partially implement objects. Like regular classes, they can have state. Unlike regular classes, they can have unimplemented methods which we mark abstract. Classes that are not abstract are called concrete classes. You can only create instances of concrete classes.

Here is the syntax for creating an abstract method. These methods are bodyless and are terminated with a semicolon.


access abstract returnType methodName(arg(s));

Here are the rule of the road.

Interfaces vs. Abstract Classes

A Minimal Program that Shows a Window We will build this in a couple of dozen lines of code.


import javafx.application.Application;
import javafx.stage.Stage;
public class SimpleGUI extends Application
{
    @Override
    public void start(Stage primary)
    {
        primary.setTitle("SimpleGUI Demo");
        primary.show();
    }
}

The Complete Program Life-Cycle We will write and app that demonstrates this.


import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.Scene;
import javafx.application.Platform;
public class LifeCycleDemo extends Application
{
    public LifeCycleDemo()
    {
        System.out.println("I am being Constructed");
    }
    @Override
    public void init()
    {
        System.out.println("I am setting up housekeeping");
    }
    @Override
    public void start(Stage primary)
    {
        primary.setTitle("SimpleGUI Demo");
        HBox v = new HBox();
        Button butt = new Button("Quit Rudely");
        butt.setOnAction( e -> Platform.exit());
        v.getChildren().add(butt);
        v.getChildren().add(new Button("button2"));
        v.getChildren().add(new Button("button3"));
        v.getChildren().add(new Button("button4"));
        v.getChildren().add(new Button("button5"));
        Scene s = new Scene(v, 400,400);
        primary.setScene(s);
        primary.show();
    }
    @Override
    public void stop()
    {
        System.out.println("I am hosing down Bourbon Street.");
    }
}

We saw this.

An Abstract Class Use Case

We have no idea whatsoever as to how to draw a general "shape," so we put and make its draw method absract.


import javafx.scene.paint.Color;
import javafx.scene.canvas.GraphicsContext;
public abstract class AShape  //you cannot create an instance of this class
{
    private Color color;
    private double xLoc;
    private double yLoc;
    public AShape(Color color, double xLoc, double yLoc)
    {
        this.color = color;
        this.xLoc = xLoc;
        this.yLoc = yLoc;
    }
    public Color getColor()
    {
        return color;
    }

    public abstract void draw(GraphicsContext pen);
}

Now let us subclass it with a Rectangle class.


public class Rectangle extends AShape
{
    public Rectangle(Color color, double xLoc, double yLoc)
    {
        super(color, xLoc, yLoc);
    }

Here is where we start next time.