Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interfaces
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Corresponds with Chapter 12
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
CS3 - AWT/Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
(c) University of Washington07-1 CSC 143 Introduction to Graphical Interfaces in Java: AWT and Swing Reading: Ch. 17.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Graphical User Interface Components Chapter What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Session 27 Swing vs. AWT. AWT (Abstract Window ToolKit) It is a portable GUI library for stand-alone applications and/or applets. The Abstract Window.
GUI development in Java L. Grewe Two methodologies for things to appear on the screen 1. By painting / drawing e.g. drawing an Image e.g. drawing an.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Layout Managers Arranges and lays out the GUI components on a container.
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Review_6 AWT, Swing, ActionListener, and Graphics.
Basics of GUI Programming Chapter 11 and Chapter 22.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
GUI Lecture 10 Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code. GUI is the “view.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Java Swing.
Tim McKenna Layout Mangers in Java Tim McKenna
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Introduction to Graphical Interfaces in Java: AWT and Swing
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Advanced Java Class GUI – part 1

Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code. –GUI is the “view layer”, and it should be separated from the controller and model layers. AWT = Abstract Windowing Toolkit Swing = better versions of AWT classes

GUI terminology Components = buttons, text fields, frames, dialog boxes, scrollbars, popup menus, scrollable lists, etc. Containers = things that hold Components. A container is also a component. Examples: Panel, Window. LayoutManagers arrange Components inside of Containers. Use Graphics objects to draw directly onto a component. This can draw shapes, text, images, etc. “Painting” is when a component is drawn onto the screen. Events occur when a user clicks the mouse, types on the keyboard, types in a text field, or selects an item from a list. Listeners (or adapters) handle events, and are specific to a certain type of event.

A few important GUI packages AWT (java.awt and subpackages) –AWT Events (java.awt.event) –Java2D (java.awt.geom) Swing (javax.swing and subpackages) –Swing Events (javax.swing.event)

Swing vs. AWT Don’t mix AWT with Swing, and always use Swing if available AWT was original GUI API – quite limited! –not very platform independent –you must do double-buffering yourself –native windows consume a lot of system resources Swing is built on AWT, and is a big improvement –mostly light-weight classes –100% pure java = platform independent, because it doesn’t depend on native code –pluggable look and feel allows the program to reflect the platform it’s currently running on –double buffers automatically Note that Swing components start with “J”. –“Frame” refers to the old AWT class.  –“JFrame” refers to the new Swing class.

Swing Components common swing components: see figure 6- 3, p MVC layers –The model layer is a separate class, which may be shared by multiple Swing components –The Controller and View layers of a component are not separated, because of the nature of GUI components – the view is the controller! –See figure 6-4 on page 397 & 398

Layout Managers LayoutManagers arrange components within Containers. They account for things that you cannot know at compile time, like differing screen resolutions, platform differences, fonts, etc. Containers are components, and child containers can have a LayoutManger that is different from their parent’s.

Using LayoutManagers Create a new Container. It has a default LayoutManager, which you can override by calling the setLayout method on it. LayoutManager classes: –BorderLayout –BoxLayout –CardLayout –FlowLayout –GridBagLayout –GridLayout

Layout Paradigms BorderLayout: North, South, East, West, and Center. figures 6-12 & 6-13 FlowLayout: lines the components up horizontally from left to right. Can specifiy justification; default is center. fig GridLayout: has columns and rows. components are added left to right and top to bottom, like a text editor. figure 6-15 GridbagLayout: more flexible than GridLayout – components can be added to any cell, and can span multiple rows and columns. figure 6-16 CardLayout: imagine a deck of cards – only one component is visible at a time. BoxLayout: components are in a single-file line, which can be horizontal or vertical. figure 6-17 Note: the above figures are in a JTabbedPane, which does it’s own LayoutManaging.

Painting You can override a Swing component’s paintComponent method if you don’t want to use the component’s default appearance. Call super.paintComponent (unless you want transparency). Call repaint() when you want to explicitly refresh what the user is seeing. You can also override update() if you want close control over the refreshing of a part of the component’s appearance.

Methods you can paint with Graphics class –drawString –drawLine –drawRect –drawPolygon –drawArc –drawImage –fillRect –fillPolygon –fillArc Graphics class, continued... –setColor –setFont –setXORMode( Color c) gives colors that are an XOR of c and what was already there – fun to play with –setPaintMode() just overwrite what’s there – this is the default

Java 2D Parts of API –Graphics2D class in java.awt –java.awt.geom –java.awt.font –java.awt.image Advantages over Graphics class –antialiasing smooths edges –can fill with contents other than a solid color –can use any system font –printing is easier (see chapter 7) –can paint any shape you want, not just predefined ones –“Strokes” – any width, and varying patterns (dashed, etc.) –can squish, stretch, rotate, etc. anything you draw

GUI Task static class ImageDisplayer extends JComponent { private boolean showing = false; private ImageIcon imageIcon = new ImageIcon("images/happy-frog.gif"); private java.util.Timer timer = new java.util.Timer(); public void showImageBriefly() { // fill in to show the image for 1.5 seconds, // and then stop showing it } public void paintComponent(Graphics g) { // fill in to draw a blue rectangle, // and then paint the imageIcon inside of it }

GUI Task Solution static class ImageDisplayer extends JComponent { private boolean showing = false; private ImageIcon imageIcon = new ImageIcon("images/happy-frog.gif"); private java.util.Timer timer = new java.util.Timer(); public void showImageBriefly() { showing = true; repaint(); timer.schedule(new TimerTask() { public void run() { showing = false; repaint(); } }, 1500); } public void paintComponent(Graphics g) { super.paintComponent(g); if (!showing) { g.setColor(new Color(0,0,255)); g.fillRect(10, 10, imageIcon.getIconWidth()+10, imageIcon.getIconHeight()+10); imageIcon.paintIcon(this, g, 15,15); }