Download presentation
Presentation is loading. Please wait.
1
GUI AND GRAPHICS
2
CLASS LIBRARIES import java.awt.Graphics; import javax.swing.JPanel; import javax.swing.JFrame;
3
HOW DO WE DRAW? Graphics: reference for shapes, contains the paintComponent JPanel: provides an area to draw JFrame: Holds and displays the panel
4
A Simple Application Window
A stand-alone GUI (Graphical User Interface) application runs in a window. Code for application window exists in class ‘JFrame’
5
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
6
Code for Application Window
7
Displaying ‘MyClass’ Panel
This happens in the main class Create a window (JFrame) with an object to call ‘MyClass’ Window has specifications
8
‘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
9
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);
10
Create a New Class to hold Panel
11
Add Panel to Frame
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.