Old Business: An Abstract Class Use Case
We created this class as a base class for shapes to be drawn on the screen.
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);
}
We then started to create a subclass for rectangles
public class Rectangle extends AShape
{
public Rectangle(Color color, double xLoc, double yLoc)
{
super(color, xLoc, yLoc);
}
}
Then the ref blew time. Let us resume this first. We will build it in such a way that we can actually use it.
Event Handling: Buttons
We begin by building a very simple app with a button in it. We will make this buttton "live" by attaching a listener to it.
OK, What's going on under the hood?
Let's talk about something that seems to be 1,000,000 miles away but
it's not. This is the forEach
method for a
Collection
. You have seen this before so it's familiar.
We will do a second go-round with this because it is somewhat confusing and
very central to making widgets live.
import java.util.ArrayList;
public class ConsumeMe
{
public static void main(String[] args)
{
ArrayList<String> items = new ArrayList<>();
for(int k = 0; k < 10; k++)
{
items.add("" + (k*k*k));
}
items.forEach( e -> System.out.println(e));
}
}
This is exactly the same mechanism we use for event handling.
Layouts
Here is one kind of layout expert.
We are going to see how to use layouts to position widgets in a content pane.