Makes use of graphical screen components such as windows, buttons, check boxes, menus, and text fields
At least 3 kinds of objects needed to create a GUI
Components
Events
Listeners
Component
A screen element that is used to display information or to allow the user to interact with the program
Container
A special type of component that is used to hold and organize other components
Containers
Frame
Panel
Frame
A container that is used to display GUI-based applications in Java, displayed as a window with a title bar, defined by the JFrame class
Panel
A container that cannot be displayed on its own, must be added to another container, used to organize other components, defined by the JPanel class
Event
An object that represents an occurrence of an action by the user of the program, such as pressing a mouse over a button or typing a key on the keyboard
A component generates an event to indicate an action of the user on the component
Event-driven program
A program that is oriented around GUI such that responses to events from the user are done
Listener
An object that waits for an event to occur and responds in a way
Creating a GUI-based program
1. Instantiate and set up necessary components
2. Implement listener classes that define what happens when particular events occur
3. Establish the relationship between the listeners and the components that generates the events
java.awt and javax.swing
Java components and other GUI-related classes are defined in these two packages
The abstract windowing toolkit (awt) was the original Java GUI package and it contains many important classes that are still being used in GUI programming
The Swing package is created later than awt and the Swing package provides components that are more versatile than those in the awt package
Both packages are necessary for GUI programming but Swing components are preferred whenever there is an option
Heavyweight vs. Lightweight containers
Heavyweight containers are managed by the operating system, lightweight containers are managed by the Java program
A frame is a heavyweight component, a panel is a lightweight component
A frame has a content pane where all visible elements of a Java interface are displayed
Add components to a container by invoking the add() method
The two main containers
Window
Panel
Window
A free floating window on the display
Panel
A container of GUI components that exists in the context of some other container such as the Window
The position and size of a component in a container is determined by a layout manager
The Frame is a subclass of Window
Frame
Has a title and a resizing corner, is initially invisible until the setVisible(true) method is invoked
Panel
Provides space for components, the default layout is FlowLayout which adds components from left to right with centered alignment
BorderLayout
The default layout for the Frame class, adds components to specific regions (North, South, West, East, Center) which are equally sized and adjust accordingly when resizing
GridLayout
Allows components to be added left to right, top to bottom
Event
Objects that describe what has happened, sent to the component from which the event originated
Event source
The generator of an event
Event handler
A method that receives an event object, deciphers it, and processes the user's interaction
Listener
Classes that implement the EventListener Interface and contain event handlers that receive and process events
Modern GUI programming in Java involves the utilization of JavaFX
JavaFX
A Java class that renders a Graphical User Interface (GUI) by extending the Applications class
The computer screen is made up of small squares called pixels that are arranged in rows and columns at usually 100 pixels per inch
Each pixel is identified by a pair of integers called coordinates often called x and y where x specifies horizontal position and y specifies vertical position
Graphics context
An object in Java that has routines for drawing basic shapes like lines, rectangles, ovals and text
Fill and stroke
Ways to draw a shape in JavaFX, fill sets the colors of the pixels inside the shape, stroke sets the color of the pixels along the border of the shape
A computer animation is a sequence of pictures that are displayed quickly one after another, each picture is called a frame