Introduction to Graphical Interfaces in Java: AWT and Swing

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
CS18000: Problem Solving and Object-Oriented Programming.
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
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
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.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 112 GUI 06 May 2008 Bilkent. Java GUI API Containers: ◦ contain other GUI components. E.g, Window, Panel, Applet, Frame Dialog. Components: ◦ Buttons,
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Java Programming Chapter 10 Graphical User Interfaces.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
JAPPLET.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
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.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
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.
Field Trip #19 Animations with Java By Keith Lynn.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
Laying Out Components Interior Design for GUIs. Nov 042 What is Widget Layout? Positioning widgets in their container (typically a JPanel or a JFrame’s.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
University of Limerick1 Software Architecture Java Layout Managers.
3461 Laying Out Components Interior Design for GUIs.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
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.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
A Quick Java Swing Tutorial
Object-Orientated Analysis, Design and Programming
Graphical User Interfaces
Chapter 9: Graphical User Interfaces
Provision for GUIs in Java
A First Look at GUI Applications
Java Swing.
Provision for GUIs in Java
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
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
GUIS: Graphical User Interfaces
Creating Graphical User Interfaces
Lecture 4: Standard Java Graphics
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Introduction to Graphical Interfaces in Java: AWT and Swing CSC 143 Introduction to Graphical Interfaces in Java: AWT and Swing

Graphical User Interfaces GUIs are a hallmark of modern software Hardly existed until Mac's came along Picked up by PC's and Unix later User sees and interacts with “controls” or “components” menus scrollbars text boxes check boxes buttons radio button groups graphics panels etc. etc.

Opposing Styles of Interaction “Algorithm-Driven” When program needs information from user, it asks for it Program is in control Typical in non-GUI environments “Event Driven” When user wants to do something, he/she signals to the program Moves or clicks mouse, types, etc. These signals come to the program as “events” Program is interrupted to deal with the events User has more control Typical in GUI environments

A Bit of Java History Java 1.0: AWT (Abstract Windowing Toolkit) Java 1.1: AWT with new event handling model Java 1.2 (aka Java 2): Swing Greatly enhanced user interface toolkit built on top of AWT Same basic event handling model as in Java 1.1 AWT developed originally on top of Java 1.1, standard in Java 1.2 Java 1.3 – 1.8 Incremental changes; no major revolution Naming Most Swing components start with J. No such standard for AWT components Other Java Library : Javafx

Bit o' Advice Use Swing whenever you can Use AWT whenever you have to

Starting a Swing application

Components & Containers Every GUI-related component descends from Component, which contains dozens of basic methods and fields common to all AWT/Swing component "Atomic" components: labels, text fields, buttons, check boxes, icons, menu items, … Some components are Containers – components like panels that can contain other subcomponents

Types of Containers Top-level containers: JFrame,JDialog, JApplet Often correspond to OS Windows Mid-level containers: panels, scroll panes, tool bars, … can contain certain other components JPanel is best for general use An Applet is a special kind of container Specialized containers: menus, list boxes, combo boxes... Technically, all J components are containers 01au: if we change the beginning of the course to avoid relying on the uwcse graphics libraries from CSE142, these examples will need to be reworked

JFrame – A Top-Level Window Top-level application window JFrame win = new JFrame(“Optional Window Title”); Some common methods setSize(int width, int height); // frame width and height setBackground(Color c); // background color show( ); //make visible (for the first time) repaint( ); // request repaint after content change setPreferredSize(Dimension d);// default size for window; also can set min // and max sizes dispose( ); // get rid of the window when done Look at project GUIs to see some of these at work

JPanel – A General Purpose Container Commonly added to a window to provide a space for graphics, or collections of buttons, labels, etc. JPanels can be nested to any depth Many methods in common with JFrame (since both are ultimately instances of Component) setSize(int width, int height); setBackground(Color c); setPreferredSize(Dimension d); Bit o' advice: Can't find the method you're looking for? Check the superclass.

Adding Components to Containers Swing containers have a “content pane” that manages the components in that container [Differs from original AWT containers, which managed their components directly] To add a component to a container, use its add method JFrame jf = new JFrame( ); JPanel panel = new JPanel( ); jf.add(panel);

Non-Component Classes Not all classes are GUI components AWT Color, Dimension, Font, layout managers Shape and subclasses like Rectangle, Point, etc. Graphics Swing Borders Further geometric classes Graphics2D Other (in java.awt.Image, javax.swing.Icon, etc…) Images, Icons

Layout Managers What happens if we add several components to a container? What are their relative positions? Answer: each container has a layout manager Several different kinds: FlowLayout (left to right, top to bottom); BorderLayout(“center”, “north”, “south”, “east”, “west”); GridLayout (2-D grid), GridBagLayout (makes HTML tables look simple); others Container state is “valid” or “invalid” depending on whether layout manager has arranged components since last change Default LayoutManager for JFrame is BorderLayout Default for JPanel is FlowLayout

pack and validate When a container is altered, either by adding components or changes to components (resized, contents change, etc.), the layout needs to be updated (i.e., the container state needs to be set to valid) Swing does this automatically more often than AWT, but not always Common methods after changing layout validate( ) – redo the layout to take into account new or changed (sub-)components pack( ) – redo the layout using the preferred size of each (sub-) component

Layout Example Create a JFrame with a button at the bottom and a panel in the center JFrame frame = new JFrame(“Trivial Window”); //default layout: Border JPanel panel = new JPanel( ); JLabel label = new JLabel(“Smile!”); label.setHorizontalAlighment(SwingConstants.CENTER); frame.add(panel, BorderLayout.CENTER); frame.add(label, BorderLayout.SOUTH);

Graphics and Drawing The windows, panes, and other components supplied with Swing are sufficient for predefined GUI components For more complex graphics, extend a suitable class and override the (empty) inherited method paintComponent that draws its contents public class Drawing extends JPanel { ... /** Repaint this Drawing whenever requested by the system */ public void paintComponent(Graphics g) { super.paintComponent(g); // to paint in the right order Graphics2D g2 = (Graphics2D) g; //good idea for Swing components g2.setColor(Color.green); g2.drawOval(40,30,100,100); g2.setColor(Color.red); g2.fillRect(60, 50, 60, 60); } …

paintComponent Method paintComponent is called by the underlying system whenever it needs the window to be repainted Triggered by window being move, resized, uncovered, expanded from icon, etc. Can happen anytime – you don’t control when In AWT days, you overrode paint( ). With Swing, it is best to leave paint alone and override paintComponent If your code does something that requires repainting, call method repaint( ) Requests that paintComponent be called sometime in the future, when convenient for underlying system window manager

Painter's Rules Always override paintComponent( ) of any component you will be drawing on Not necessary if you make simple changes, like changing background color, title, etc. that don't require a graphics object Never call paint( ) or paintComponent( ). Never means never! This is a hard rule to understand. Follow it anyway. Always paint the entire picture, from scratch Don't create a Graphics or Graphics2D object to draw with only use the one given to you as a parameter of paintComponent( ) and, don't save that object to reuse later! This rule is bent in advanced graphics applications

What Happens If You Don't Follow The Rules...

What Happens If You Don't Follow The Rules...

Classes Graphics and Graphics2D The parameter to paintComponent or paint is a graphics context where the drawing should be done Class Graphics2D is a subclass of Graphics, with better features In Swing components, the parameter has static type Graphics, but dynamic type Graphics2D so cast it to a 2D and use that.

Drawing Graphical Objects Many graphical objects implement the java.awt.Shape interface When possible, chose a Shape rather than a non-Shape Lots of methods available to draw various kinds of outline and solid shapes and control colors and fonts setColor, setFont, drawArc, drawLine, fillPolygon, drawOval, fillRect, many others