Java GUIs and Graphics CNS 3250. Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
Corresponds with Chapter 12
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Event Handling Events and Listeners Timers and Animation.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
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.
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.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
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.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
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.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Layout Managers Arranges and lays out the GUI components on a container.
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.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Object-Oriented Software Engineering
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Basics of GUI Programming Chapter 11 and Chapter 22.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
Developing GUIs With the Eclipse Visual Editor, Swing/AWT Edition David Gallardo.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
A Quick Java Swing Tutorial
Graphical User Interfaces
Welcome To java
Advanced User Interfaces
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Event-driven programming for GUI
Chapter 7-3 (Book Chapter 14)
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Graphical User Interface
Presentation transcript:

Java GUIs and Graphics CNS 3250

Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout managers  Drawing

GUIs and graphics in Java  Portable!  Listeners and events  Components  Layout managers  Portable!  Listeners and events  Components  Layout managers

A design consideration  Data components should exist independently from GUI components  They should have no knowledge of GUI  GUI components call upon data objects for information Why is this a good approach to use?  Data components should exist independently from GUI components  They should have no knowledge of GUI  GUI components call upon data objects for information Why is this a good approach to use?

History  AWT / Event Model (JDK 1.1)  Heavyweight components  Use OS “peers”  Event Dispatch Thread  Listeners to handle events  Swing (Java 2)  Lightweight components  Painted via graphics (not OS peers)  Preserves look and feel across platforms  AWT / Event Model (JDK 1.1)  Heavyweight components  Use OS “peers”  Event Dispatch Thread  Listeners to handle events  Swing (Java 2)  Lightweight components  Painted via graphics (not OS peers)  Preserves look and feel across platforms

Events How does your program know if the user clicks something with the mouse? Register a listener object. When the user clicks with the mouse, the system passes an event object to the listener. How does your program know if the user clicks something with the mouse? Register a listener object. When the user clicks with the mouse, the system passes an event object to the listener.

Listeners Implements one of the listener interfaces:  ActionListener  MouseListener  MouseMotionListener  WindowListener  etc. Listener interfaces are defined in java.awt.event Implements one of the listener interfaces:  ActionListener  MouseListener  MouseMotionListener  WindowListener  etc. Listener interfaces are defined in java.awt.event

ActionListener interface  Used with buttons, menu items, text fields  Only one method, actionPerformed  Takes an ActionEvent object as parameter  ActionEvent has a method that tells the command that triggered the name of the event.  For a button, the text of the button will be the command by default.  Used with buttons, menu items, text fields  Only one method, actionPerformed  Takes an ActionEvent object as parameter  ActionEvent has a method that tells the command that triggered the name of the event.  For a button, the text of the button will be the command by default.

Red Alert! import java.awt.event.*; import javax.swing.*; class RedAlert extends JFrame implements ActionListener { private JButton redAlertButton; public RedAlert() { redAlertButton = new JButton("Red Alert"); add(redAlertButton); redAlertButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Red Alert")) { System.out.println("Shields up!"); } public static void main(String[] args) { RedAlert frame = new RedAlert(); frame.setSize(200, 100); frame.setVisible(true); }

Red Alert! import java.awt.event.*; import javax.swing.*; class RedAlert extends JFrame implements ActionListener { private JButton redAlertButton; public RedAlert() { redAlertButton = new JButton("Red Alert"); add(redAlertButton); redAlertButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Red Alert")) { System.out.println("Shields up!"); } public static void main(String[] args) { RedAlert frame = new RedAlert(); frame.setSize(200, 100); frame.setVisible(true); }

MouseListener Includes several methods:  mouseClicked  mouseEntered  mouseExited  mousePressed  mouseReleased Includes several methods:  mouseClicked  mouseEntered  mouseExited  mousePressed  mouseReleased  Each method gets a MouseEvent object that tells the coordinates of the mouse and other info.  All methods have to be defined, whether you use them or not...

MouseAdapter  Defines all required methods for the MouseListener interface  Methods are empty.  Override the methods you actually want to use.  Use by making your class extend MouseAdapter.  Defines all required methods for the MouseListener interface  Methods are empty.  Override the methods you actually want to use.  Use by making your class extend MouseAdapter.

Adapters  All methods for the corresponding listener interface are defined.  Only useful if your class doesn't need to inherit from any other class.  Commonly used for mouse listeners, mouse motion listeners, window listeners.  There is no adapter for action listeners. Why not?  All methods for the corresponding listener interface are defined.  Only useful if your class doesn't need to inherit from any other class.  Commonly used for mouse listeners, mouse motion listeners, window listeners.  There is no adapter for action listeners. Why not?

Using listeners and events  Decide what kind of listener you need  This is usually based on the kind of input and the type of component you will be using  Define a class for that kind of listener Make a class that implements the interface Make a class that extends an adapter  Register the listener with the component  add____Listener, e.g. addActionListener  Decide what kind of listener you need  This is usually based on the kind of input and the type of component you will be using  Define a class for that kind of listener Make a class that implements the interface Make a class that extends an adapter  Register the listener with the component  add____Listener, e.g. addActionListener or

Red Alert! import java.awt.event.*; import javax.swing.*; class RedAlert extends JFrame implements ActionListener { private JButton redAlertButton; public RedAlert() { redAlertButton = new JButton("Red Alert"); add(redAlertButton); redAlertButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Red Alert")) { System.out.println("Shields up!"); } public static void main(String[] args) { RedAlert frame = new RedAlert(); frame.setSize(200, 100); frame.setVisible(true); }

PolyDraw demo

Elf -- Java Ldraw display Uses OpenGL (JOGL)

POV-Ray -- Ray tracing

Components  Top-level containers  General-purpose containers  Displays  Controls  Text components  Top-level containers  General-purpose containers  Displays  Controls  Text components

Top-level containers  Every Java GUI program must have a top- level container.  Frame (AWT--java.awt)  JFrame (Swing--javax.swing)  Every Java GUI program must have a top- level container.  Frame (AWT--java.awt)  JFrame (Swing--javax.swing)

General-purpose containers  Panel (AWT)  JPanel (Swing)  Panel (AWT)  JPanel (Swing)

JPanel -- very commonly used Container for componentsDrawing

Displays JLabel  Can include an image in addition to (or instead of text)  Can use HTML formatting JLabel  Can include an image in addition to (or instead of text)  Can use HTML formatting

Controls  JButton  JComboBox  JList  JMenuItem  JButton  JComboBox  JList  JMenuItem What listeners and events?

Controls  JButton  JComboBox  JList  JMenuItem  JButton  JComboBox  JList  JMenuItem What listeners and events? ActionEvent PopupMenuEvent ActionEvent PopupMenuEvent ListSelectionEvent ActionEvent

Text components

JTextArea  Used for multi-line text display and editing  Does not handle scrolling internally (as TextArea does)  Add to JScrollPane to get scrolling  Implements Scrollable interface  Can specify whether or not editing is allowed  Can specify line wrapping or not  Used for multi-line text display and editing  Does not handle scrolling internally (as TextArea does)  Add to JScrollPane to get scrolling  Implements Scrollable interface  Can specify whether or not editing is allowed  Can specify line wrapping or not

JEditorPane  Can display HTML  Not a "speed demon" according to our text book  Can get very complicated  Can display HTML  Not a "speed demon" according to our text book  Can get very complicated ' Castle A castle stands on a hill here. It doesn\'t appear to be inhabited, but it\'s hard to say for sure. '

PolyDraw components JScrollPane JPanel JButton JTextField JLabel JFrame JMenuBar JMenu JMenuItem JPanel JComboBox

Layout managers Where should components go? Usually: x, y coordinates. In Java location, size, and shape of components depends on another question: How big will the user's display be?  Could be anything from a huge, high-res display to a cell phone  Can change Where should components go? Usually: x, y coordinates. In Java location, size, and shape of components depends on another question: How big will the user's display be?  Could be anything from a huge, high-res display to a cell phone  Can change

Good news, bad news Good news  Adjusts components to fit size/shape of container  Can write Java GUIs with no special tools  Works well for different fonts, languages Bad news  Can be difficult to get what you want  Unexpected results Good news  Adjusts components to fit size/shape of container  Can write Java GUIs with no special tools  Works well for different fonts, languages Bad news  Can be difficult to get what you want  Unexpected results

Types of layout managers  BorderLayout  BoxLayout  CardLayout  FlowLayout  GridBagLayout  GridLayout  SpringLayout  BorderLayout  BoxLayout  CardLayout  FlowLayout  GridBagLayout  GridLayout  SpringLayout Pictures on the following slides are from the Java Tutorial:

Using a layout manager  Create (or get access to) container  Set layout manager for container  Create component  Add component to container  Create (or get access to) container  Set layout manager for container  Create component  Add component to container Container cp = getContentPane(); cp.setLayout(new BorderLayout()); JButton button = new JButton("Button 1"); cp.add(button, BorderLayout.CENTER); These steps vary somewhat depending on the layout manager.

BorderLayout  Very commonly used  The center area grows/shrinks the most  Regions are (or used to be): Center, North, South, East, West  Very commonly used  The center area grows/shrinks the most  Regions are (or used to be): Center, North, South, East, West

BoxLayout  Relatively new  Fillers:  strut--1D fixed amount of space  rigid area--2D fixed amount of space  glue--variable amount of space, separates components as much as possible (Textbook says "spring" would be a better term than "glue".)  Relatively new  Fillers:  strut--1D fixed amount of space  rigid area--2D fixed amount of space  glue--variable amount of space, separates components as much as possible (Textbook says "spring" would be a better term than "glue".)

CardLayout  Used to switch back and forth between different layouts in the same space  Usually need to keep a reference to the layout manager object so that you can control which card is showing.  Used to switch back and forth between different layouts in the same space  Usually need to keep a reference to the layout manager object so that you can control which card is showing.

FlowLayout  The simplest layout manager  Can specify horizontal alignment  The simplest layout manager  Can specify horizontal alignment

GridBagLayout  Very flexible, but very complicated  See the p. 433 in Core Java Vol. I for a "Recipe for Making a Grid Bag Layout"  Note Step 7: "compile, run, and enjoy"  Must create GridBagConstraints objects  Book recommends a helper class GBC  NetBeans and other IDEs offer visual tools  Very flexible, but very complicated  See the p. 433 in Core Java Vol. I for a "Recipe for Making a Grid Bag Layout"  Note Step 7: "compile, run, and enjoy"  Must create GridBagConstraints objects  Book recommends a helper class GBC  NetBeans and other IDEs offer visual tools

GridLayout  Useful for a number of components that are the same size  Position is determined by order that components are added to the container  Useful for a number of components that are the same size  Position is determined by order that components are added to the container

SpringLayout  Meant to be as flexible as GridBagLayout but "more intuitive" It might be as flexible, but I think it's safe to say that it isn't more intuitive.  Meant to be as flexible as GridBagLayout but "more intuitive" It might be as flexible, but I think it's safe to say that it isn't more intuitive.