GUI AND GRAPHICS
CLASS LIBRARIES import java.awt.Graphics; import javax.swing.JPanel; import javax.swing.JFrame;
HOW DO WE DRAW? Graphics: reference for shapes, contains the paintComponent JPanel: provides an area to draw JFrame: Holds and displays the panel
A Simple Application Window A stand-alone GUI (Graphical User Interface) application runs in a window. Code for application window exists in class ‘JFrame’
Main Class JFrame Create a window Set the frame to exit Add panel to frame Set the size of the frame Make the frame visible
Code for Application Window
Displaying ‘MyClass’ Panel This happens in the main class Create a window (JFrame) with an object to call ‘MyClass’ Window has specifications
‘MyClass’ extends JPanel Create a new class – Panel to be displayed MyClass for drawing must extend JPanel Inheritance relationship, ‘MyClass’ will begin with existing members of class JPanel JPanel is the superclass ; MyClass is the subclass
paintComponent Method Every JPanel contains a paintComponent method Used to display panel Syntax: public void paintComponent( Graphics g) Requires one argument, a Graphics object: super.paintComponent(g);
Create a New Class to hold Panel
Add Panel to Frame