SlideShare a Scribd company logo
1 of 35
Download to read offline
Java Graphics & GUIs
(and Swing/AWT libraries)
CSE 331
Software Design & Implementation
Slides contain contributions from: M. Ernst, M. Hotan, R.
Mercer, D. Notkin, H. Perkins, S. Regis, M. Stepp;
Oracle docs & tutorial, Horstmann, Wikipedia, 2
1
Why study GUIs?
• Learn about event-driven programming techniques
• Practice learning and using a large, complex API
• A chance to see how it is designed and learn from it:
– design patterns: model-view separation,
callbacks, listeners, inheritance vs. delegation
– refactoring vs. reimplementing an ailing API
• Because GUIs are neat!
• Caution: There is way more here than you can memorize.
– Part of learning a large API is "letting go."
– First, learn the fundamental concepts and general ideas.
– Then, look things up as you need them
– Don’t get bogged down implementing eye candy
References
Today: Java graphics and Swing/AWT class libraries
Only an introduction! Also see
• Sun/Oracle Java tutorials
http://docs.oracle.com/javase/tutorial/uiswing/index.html
• Extra slides, on class website
• Core Java vol. I by Horstmann & Cornell
• If you have another favorite, use it
Next lecture:
Event-driven programming and user interaction
3
Outline
Organization of the Swing/AWT library
Graphics and drawing
Repaint callbacks, layout managers, etc.
Handling user events
Building GUI applications
MVC, user events, updates, &c
4
Java GUI libraries
• Swing: the main Java GUI library
– Benefits: Features; cross-platform compatibility; OO design
– Paints GUI controls itself pixel-by-pixel
• Does not delegate to OS’s window system
• Abstract Windowing Toolkit (AWT): Sun's initial GUI library
– Maps Java code to each operating system's real GUI system
– Problems: Limited to lowest common denominator (limited set of
UI widgets); clunky to use.
• Advice: Use Swing. You occasionally have to use AWT (Swing is built
on top of AWT). Beware: it’s easy to get them mixed up.
GUI terminology
window: A first-class citizen of the graphical desktop
Also called a top-level container
Examples: frame, dialog box, applet
component: A GUI widget that resides in a window
Also called controls in many other languages
Examples: button, text box, label
container: A component that hosts (holds) components
Examples: panel, box
6
Components
Component & container classes
8
Component
Container
Jcomponent
Jpanel JFileChooser
Tons of
Jcomponents
Various AWT
containers
Lots of AWT
components
Every GUI-related class
descends from
Component
“Atomic” components:
labels, text fields,
buttons, check boxes,
icons, menu items2
Containers can
hold nested
subcomponents
Swing/AWT inheritance hierarchy
Component (AWT)
Window
Frame
JFrame (Swing)
JDialog
Container
Jcomponent (Swing)
JButton JColorChooser JFileChooser
JComboBox JLabel JList
JMenuBar JOptionPane JPanel
JPopupMenu JProgressBar JScrollbar
JScrollPane JSlider JSpinner
JSplitPane JTabbedPane JTable
JToolbar JTree JTextArea
JTextField ...
9
Component fields (actually properties)
Each has a get (or is) accessor and a set modifier.
Examples: getColor, setFont, isVisible, 2
name description
background background color behind component
border border line around component
enabled whether it can be interacted with
focusable whether key text can be typed on it
font font used for text in component
foreground foreground color of component
height, width component's current size in pixels
visible whether component can be seen
tooltip text text shown when hovering mouse
size, minimum / maximum
/ preferred size
various sizes, size limits, or desired
sizes that the component may take
Types of containers
• Top-level containers: JFrame, JDialog, 2
– Often correspond to OS windows
– Can be used by themselves, but usually as a host for
other components
– Live at top of UI hierarchy, not nested in anything else
• Mid-level containers: panels, scroll panes, tool bars
– Sometimes contain other containers, sometimes not
– JPanel is a general-purpose component for drawing or
hosting other UI elements (buttons, etc.)
• Specialized containers: menus, list boxes, 2
• Technically, all J-components are containers
11
JFrame – top-level window
Graphical window on the screen
Typically holds (hosts) other components
Common methods:
JFrame(String title) – constructor, title optional
setSize(int width, int height) – set size
add(Component c) – add component to window
setVisible(boolean v) – make window visible
or not. Don’t forget this!
12
Example
SimpleFrameMain.java
13
More JFrame
• public void setDefaultCloseOperation(int op)
Makes the frame perform the given action when it closes.
– Common value passed: JFrame.EXIT_ON_CLOSE
– If not set, the program will never exit even if the frame is closed.
• public void setSize(int width, int height)
Gives the frame a fixed size in pixels.
• public void pack()
Resizes the frame to fit the components inside it snugly.
JPanel – a general-purpose container
Commonly used as a place for graphics, or to hold a
collection of button, labels, etc.
Needs to be added to a window or other container
frame.add(new Jpanel(…))
JPanels can be nested to any depth
Many methods/fields in common with JFrame (since
both inherit from Component)
Advice: can’t find a method/field? Check the
superclass(es)
Some new methods. Particularly useful:
setPreferredSize(Dimension d)
15
Containers and layout
What if we add several components to a container?
How are they positioned relative to each other?
Answer: each container has a layout manger.
Layout managers
Kinds:
– FlowLayout (left to right, top to bottom) – default for
JPanel
– BorderLayout (“center”, “north”, “south”, “east”,
“west”) – default for JFrame
– GridLayout (regular 2-D grid)
– others... (some are incredibly complex)
The first two should be good enough for now2.
17
Place components in a container; add the container to
a frame.
– container: An object that stores components and
governs their positions, sizes, and resizing
behavior.
18
pack()
Once all the components are added to their containers,
do this to make the window visible
pack();
setVisible(true);
pack() figures out the sizes of all components and
calls the layout manager to set locations in the
container (recursively as needed)
If your window doesn’t look right, you may have
forgotten pack()
19
Example
SimpleLayoutMain.java
20
Sizing and positioning
How does the programmer specify where each component appears,
how big each component should be, and what the component should
do if the window is resized / moved / maximized / etc.?
• Absolute positioning (C++, C#, others):
Programmer specifies exact pixel coordinates of every component.
– "Put this button at (x=15, y=75) and make it 70x31 px in size."
• Layout managers (Java):
Objects that decide where to position each component based on
some general rules or criteria.
– "Put these four buttons into a 2x2 grid and put these text boxes in
a horizontal flow in the south part of the frame."
JFrame as container
A JFrame is a container. Containers have these methods:
• public void add(Component comp)
public void add(Component comp, Object info)
Adds a component to the container, possibly giving extra information
about where to place it.
• public void remove(Component comp)
• public void setLayout(LayoutManager mgr)
Uses the given layout manager to position components.
• public void validate()
Refreshes the layout (if it changes after the container is onscreen).
Preferred sizes
• Swing component objects each have a certain size they would "like"
to be: Just large enough to fit their contents (text, icons, etc.).
– This is called the preferred size of the component.
– Some types of layout managers (e.g. FlowLayout) choose to
size the components inside them to the preferred size.
– Others (e.g. BorderLayout, GridLayout) disregard the
preferred size and use some other scheme to size the
components.
Buttons at preferred size: Not preferred size:
FlowLayout
public FlowLayout()
• treats container as a left-to-right, top-to-bottom "paragraph".
– Components are given preferred size, horizontally and vertically.
– Components are positioned in the order added.
– If too long, components wrap around to the next line.
myFrame.setLayout(new FlowLayout());
myFrame.add(new JButton("Button 1"));
– The default layout for containers other than JFrame (seen later).
BorderLayout
public BorderLayout()
• Divides container into five regions:
– NORTH and SOUTH regions expand to fill region horizontally,
and use the component's preferred size vertically.
– WEST and EAST regions expand to fill region vertically,
and use the component's preferred size horizontally.
– CENTER uses all space not occupied by others.
myFrame.setLayout(new BorderLayout());
myFrame.add(new JButton("Button 1"), BorderLayout.NORTH);
– This is the default layout for a JFrame.
GridLayout
public GridLayout(int rows, int columns)
• Treats container as a grid of equally-sized rows and columns.
• Components are given equal horizontal / vertical size, disregarding
preferred size.
• Can specify 0 rows or columns to indicate expansion in that direction
as needed.
Graphics and drawing
So far so good – and very boring2
What if we want to actually draw something? A map, an
image, a path, 2?
Answer: Override method paintComponent
Method in JComponent that draws the component
In JLabel’s case, it draws the label text
27
Example
SimplePaintMain.java
28
Graphics methods
Many methods to draw various lines, shapes, etc., 2
Can also draw images (pictures, etc.). Load the image
file into an Image object and use g.drawImage(…):
– In the program (not in paintComponent):
Image pic =
Toolkit.getDefaultToolkit()
.getImage(image path);
– Then in paintComponent:
g.drawImage(pic, ...);
29
Graphics vs Graphics2D
Class Graphics was part of the original Java AWT
Has a procedural interface: g.drawRect(…),
g.fillOval(…)
Swing introduced Graphics2D
Added a object interface – create instances of
Shape like Line2D, Rectangle2D, etc., and add
these to the Graphics2D object
Parameter to paintComponent is always Graphics2D.
Can always cast it to that class. Graphics2D supports
both sets of graphics methods.
Use whichever you like for CSE 331
30
So who calls paintComponent?
And when??
• Answer: the window manager calls paintComponent
whenever it wants!!!
– When the window is first made visible, and whenever
after that it is needed
• Corollary: paintComponent must always be ready to
repaint – regardless of what else is going on
– You have no control over when or how often – must
store enough information to repaint on demand
• If you want to redraw a window, call repaint() from the
program (not from paintComponent)
– Tells the window manager to schedule repainting
– Window manager will call paintComponent when it
decides to redraw (soon, but maybe not right away)
31
Example
FaceMain.java
32
How repainting happens
33
program window manager (UI)
repaint()
paintComponent(g)
It’s worse than it looks!
Your program and the
window manager are
running concurrently:
• Program thread
• User Interface thread
Do not attempt to mess
around – follow the rules
and nobody gets hurt!
Rules for painting – Obey!
• Always override paintComponent(g) if you want to
draw on a component
• Always call super.paintComponent(g) first
• NEVER call paintComponent yourself. That means
ABSOLUTELY POSITIVELY NEVER!!!
• Always paint the entire picture, from scratch
• Use paintComponent’s Graphics parameter to do
all the drawing. ONLY use it for that. Don’t copy it, try
to replace it, permanently side-effect it, etc. It is quick
to anger.
• DON’T create new Graphics or Graphics2D objects
• Fine print: Once you are a certified™ wizard, you may find reasons to do
things differently, but you aren’t there yet.
34
What’s next – and not
Major topic next time is how to handle user interactions
Key idea: the observer pattern
Beyond that you’re on your own to explore all the
wonderful widgets in Swing/AWT. Have fun!!!
(But don’t sink huge amounts of time into eye candy)
35

More Related Content

Similar to Java GUI Programming for beginners-graphics.pdf

RomaFramework Tutorial Basics
RomaFramework Tutorial BasicsRomaFramework Tutorial Basics
RomaFramework Tutorial BasicsLuca Garulli
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892renuka gavli
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in javaAdil Mehmoood
 
8layout Managers
8layout Managers8layout Managers
8layout ManagersAdil Jafri
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Javayht4ever
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on SwingsMohanYedatkar
 
Goodbye Nightmare : Tops and Tricks for creating Layouts
Goodbye Nightmare : Tops and Tricks for creating LayoutsGoodbye Nightmare : Tops and Tricks for creating Layouts
Goodbye Nightmare : Tops and Tricks for creating LayoutsLuc Bors
 
AWT controls, Listeners
AWT controls, ListenersAWT controls, Listeners
AWT controls, ListenersKalai Selvi
 

Similar to Java GUI Programming for beginners-graphics.pdf (20)

RomaFramework Tutorial Basics
RomaFramework Tutorial BasicsRomaFramework Tutorial Basics
RomaFramework Tutorial Basics
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
8layout Managers
8layout Managers8layout Managers
8layout Managers
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
13457272.ppt
13457272.ppt13457272.ppt
13457272.ppt
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
08graphics
08graphics08graphics
08graphics
 
Lesson 4
Lesson 4Lesson 4
Lesson 4
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
 
Ingles 2do parcial
Ingles   2do parcialIngles   2do parcial
Ingles 2do parcial
 
Goodbye Nightmare : Tops and Tricks for creating Layouts
Goodbye Nightmare : Tops and Tricks for creating LayoutsGoodbye Nightmare : Tops and Tricks for creating Layouts
Goodbye Nightmare : Tops and Tricks for creating Layouts
 
Goodbye Nightmare: Tips and Tricks for Creating Complex Layouts with Oracle A...
Goodbye Nightmare: Tips and Tricks for Creating Complex Layouts with Oracle A...Goodbye Nightmare: Tips and Tricks for Creating Complex Layouts with Oracle A...
Goodbye Nightmare: Tips and Tricks for Creating Complex Layouts with Oracle A...
 
Applet in java
Applet in javaApplet in java
Applet in java
 
AWT controls, Listeners
AWT controls, ListenersAWT controls, Listeners
AWT controls, Listeners
 
Swing
SwingSwing
Swing
 
Swing
SwingSwing
Swing
 
UNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdfUNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdf
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 

Java GUI Programming for beginners-graphics.pdf

  • 1. Java Graphics & GUIs (and Swing/AWT libraries) CSE 331 Software Design & Implementation Slides contain contributions from: M. Ernst, M. Hotan, R. Mercer, D. Notkin, H. Perkins, S. Regis, M. Stepp; Oracle docs & tutorial, Horstmann, Wikipedia, 2 1
  • 2. Why study GUIs? • Learn about event-driven programming techniques • Practice learning and using a large, complex API • A chance to see how it is designed and learn from it: – design patterns: model-view separation, callbacks, listeners, inheritance vs. delegation – refactoring vs. reimplementing an ailing API • Because GUIs are neat! • Caution: There is way more here than you can memorize. – Part of learning a large API is "letting go." – First, learn the fundamental concepts and general ideas. – Then, look things up as you need them – Don’t get bogged down implementing eye candy
  • 3. References Today: Java graphics and Swing/AWT class libraries Only an introduction! Also see • Sun/Oracle Java tutorials http://docs.oracle.com/javase/tutorial/uiswing/index.html • Extra slides, on class website • Core Java vol. I by Horstmann & Cornell • If you have another favorite, use it Next lecture: Event-driven programming and user interaction 3
  • 4. Outline Organization of the Swing/AWT library Graphics and drawing Repaint callbacks, layout managers, etc. Handling user events Building GUI applications MVC, user events, updates, &c 4
  • 5. Java GUI libraries • Swing: the main Java GUI library – Benefits: Features; cross-platform compatibility; OO design – Paints GUI controls itself pixel-by-pixel • Does not delegate to OS’s window system • Abstract Windowing Toolkit (AWT): Sun's initial GUI library – Maps Java code to each operating system's real GUI system – Problems: Limited to lowest common denominator (limited set of UI widgets); clunky to use. • Advice: Use Swing. You occasionally have to use AWT (Swing is built on top of AWT). Beware: it’s easy to get them mixed up.
  • 6. GUI terminology window: A first-class citizen of the graphical desktop Also called a top-level container Examples: frame, dialog box, applet component: A GUI widget that resides in a window Also called controls in many other languages Examples: button, text box, label container: A component that hosts (holds) components Examples: panel, box 6
  • 8. Component & container classes 8 Component Container Jcomponent Jpanel JFileChooser Tons of Jcomponents Various AWT containers Lots of AWT components Every GUI-related class descends from Component “Atomic” components: labels, text fields, buttons, check boxes, icons, menu items2 Containers can hold nested subcomponents
  • 9. Swing/AWT inheritance hierarchy Component (AWT) Window Frame JFrame (Swing) JDialog Container Jcomponent (Swing) JButton JColorChooser JFileChooser JComboBox JLabel JList JMenuBar JOptionPane JPanel JPopupMenu JProgressBar JScrollbar JScrollPane JSlider JSpinner JSplitPane JTabbedPane JTable JToolbar JTree JTextArea JTextField ... 9
  • 10. Component fields (actually properties) Each has a get (or is) accessor and a set modifier. Examples: getColor, setFont, isVisible, 2 name description background background color behind component border border line around component enabled whether it can be interacted with focusable whether key text can be typed on it font font used for text in component foreground foreground color of component height, width component's current size in pixels visible whether component can be seen tooltip text text shown when hovering mouse size, minimum / maximum / preferred size various sizes, size limits, or desired sizes that the component may take
  • 11. Types of containers • Top-level containers: JFrame, JDialog, 2 – Often correspond to OS windows – Can be used by themselves, but usually as a host for other components – Live at top of UI hierarchy, not nested in anything else • Mid-level containers: panels, scroll panes, tool bars – Sometimes contain other containers, sometimes not – JPanel is a general-purpose component for drawing or hosting other UI elements (buttons, etc.) • Specialized containers: menus, list boxes, 2 • Technically, all J-components are containers 11
  • 12. JFrame – top-level window Graphical window on the screen Typically holds (hosts) other components Common methods: JFrame(String title) – constructor, title optional setSize(int width, int height) – set size add(Component c) – add component to window setVisible(boolean v) – make window visible or not. Don’t forget this! 12
  • 14. More JFrame • public void setDefaultCloseOperation(int op) Makes the frame perform the given action when it closes. – Common value passed: JFrame.EXIT_ON_CLOSE – If not set, the program will never exit even if the frame is closed. • public void setSize(int width, int height) Gives the frame a fixed size in pixels. • public void pack() Resizes the frame to fit the components inside it snugly.
  • 15. JPanel – a general-purpose container Commonly used as a place for graphics, or to hold a collection of button, labels, etc. Needs to be added to a window or other container frame.add(new Jpanel(…)) JPanels can be nested to any depth Many methods/fields in common with JFrame (since both inherit from Component) Advice: can’t find a method/field? Check the superclass(es) Some new methods. Particularly useful: setPreferredSize(Dimension d) 15
  • 16. Containers and layout What if we add several components to a container? How are they positioned relative to each other? Answer: each container has a layout manger.
  • 17. Layout managers Kinds: – FlowLayout (left to right, top to bottom) – default for JPanel – BorderLayout (“center”, “north”, “south”, “east”, “west”) – default for JFrame – GridLayout (regular 2-D grid) – others... (some are incredibly complex) The first two should be good enough for now2. 17
  • 18. Place components in a container; add the container to a frame. – container: An object that stores components and governs their positions, sizes, and resizing behavior. 18
  • 19. pack() Once all the components are added to their containers, do this to make the window visible pack(); setVisible(true); pack() figures out the sizes of all components and calls the layout manager to set locations in the container (recursively as needed) If your window doesn’t look right, you may have forgotten pack() 19
  • 21. Sizing and positioning How does the programmer specify where each component appears, how big each component should be, and what the component should do if the window is resized / moved / maximized / etc.? • Absolute positioning (C++, C#, others): Programmer specifies exact pixel coordinates of every component. – "Put this button at (x=15, y=75) and make it 70x31 px in size." • Layout managers (Java): Objects that decide where to position each component based on some general rules or criteria. – "Put these four buttons into a 2x2 grid and put these text boxes in a horizontal flow in the south part of the frame."
  • 22. JFrame as container A JFrame is a container. Containers have these methods: • public void add(Component comp) public void add(Component comp, Object info) Adds a component to the container, possibly giving extra information about where to place it. • public void remove(Component comp) • public void setLayout(LayoutManager mgr) Uses the given layout manager to position components. • public void validate() Refreshes the layout (if it changes after the container is onscreen).
  • 23. Preferred sizes • Swing component objects each have a certain size they would "like" to be: Just large enough to fit their contents (text, icons, etc.). – This is called the preferred size of the component. – Some types of layout managers (e.g. FlowLayout) choose to size the components inside them to the preferred size. – Others (e.g. BorderLayout, GridLayout) disregard the preferred size and use some other scheme to size the components. Buttons at preferred size: Not preferred size:
  • 24. FlowLayout public FlowLayout() • treats container as a left-to-right, top-to-bottom "paragraph". – Components are given preferred size, horizontally and vertically. – Components are positioned in the order added. – If too long, components wrap around to the next line. myFrame.setLayout(new FlowLayout()); myFrame.add(new JButton("Button 1")); – The default layout for containers other than JFrame (seen later).
  • 25. BorderLayout public BorderLayout() • Divides container into five regions: – NORTH and SOUTH regions expand to fill region horizontally, and use the component's preferred size vertically. – WEST and EAST regions expand to fill region vertically, and use the component's preferred size horizontally. – CENTER uses all space not occupied by others. myFrame.setLayout(new BorderLayout()); myFrame.add(new JButton("Button 1"), BorderLayout.NORTH); – This is the default layout for a JFrame.
  • 26. GridLayout public GridLayout(int rows, int columns) • Treats container as a grid of equally-sized rows and columns. • Components are given equal horizontal / vertical size, disregarding preferred size. • Can specify 0 rows or columns to indicate expansion in that direction as needed.
  • 27. Graphics and drawing So far so good – and very boring2 What if we want to actually draw something? A map, an image, a path, 2? Answer: Override method paintComponent Method in JComponent that draws the component In JLabel’s case, it draws the label text 27
  • 29. Graphics methods Many methods to draw various lines, shapes, etc., 2 Can also draw images (pictures, etc.). Load the image file into an Image object and use g.drawImage(…): – In the program (not in paintComponent): Image pic = Toolkit.getDefaultToolkit() .getImage(image path); – Then in paintComponent: g.drawImage(pic, ...); 29
  • 30. Graphics vs Graphics2D Class Graphics was part of the original Java AWT Has a procedural interface: g.drawRect(…), g.fillOval(…) Swing introduced Graphics2D Added a object interface – create instances of Shape like Line2D, Rectangle2D, etc., and add these to the Graphics2D object Parameter to paintComponent is always Graphics2D. Can always cast it to that class. Graphics2D supports both sets of graphics methods. Use whichever you like for CSE 331 30
  • 31. So who calls paintComponent? And when?? • Answer: the window manager calls paintComponent whenever it wants!!! – When the window is first made visible, and whenever after that it is needed • Corollary: paintComponent must always be ready to repaint – regardless of what else is going on – You have no control over when or how often – must store enough information to repaint on demand • If you want to redraw a window, call repaint() from the program (not from paintComponent) – Tells the window manager to schedule repainting – Window manager will call paintComponent when it decides to redraw (soon, but maybe not right away) 31
  • 33. How repainting happens 33 program window manager (UI) repaint() paintComponent(g) It’s worse than it looks! Your program and the window manager are running concurrently: • Program thread • User Interface thread Do not attempt to mess around – follow the rules and nobody gets hurt!
  • 34. Rules for painting – Obey! • Always override paintComponent(g) if you want to draw on a component • Always call super.paintComponent(g) first • NEVER call paintComponent yourself. That means ABSOLUTELY POSITIVELY NEVER!!! • Always paint the entire picture, from scratch • Use paintComponent’s Graphics parameter to do all the drawing. ONLY use it for that. Don’t copy it, try to replace it, permanently side-effect it, etc. It is quick to anger. • DON’T create new Graphics or Graphics2D objects • Fine print: Once you are a certified™ wizard, you may find reasons to do things differently, but you aren’t there yet. 34
  • 35. What’s next – and not Major topic next time is how to handle user interactions Key idea: the observer pattern Beyond that you’re on your own to explore all the wonderful widgets in Swing/AWT. Have fun!!! (But don’t sink huge amounts of time into eye candy) 35