Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Introduction to Java 2 Programming
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
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
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.
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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
Java Programming Chapter 10 Graphical User Interfaces.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Welcome to CIS 083 ! Events CIS 068.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
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, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 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.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
CS Lecture 00 Swing overview and introduction Lynda Thomas
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
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.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Review_6 AWT, Swing, ActionListener, and Graphics.
Graphical User Interfaces Tonga Institute of Higher Education.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
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.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
A First Look at GUI Applications
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Graphical User Interface
Presentation transcript:

Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh

Many intro. to programming examples use  Input from the keyboard (or file)  Output to the console (or file) This is effective but in today’s world is not so user-friendly Users want to use the mouse Users want windows with dialog boxes and buttons Users need maximum guidance with minimum room for error Graphical Interfaces

Java contains tools for design and implement with graphical interfaces  Graphical output and use of a mouse and other graphical components for input o Ex: Windows with buttons, textfields, pulldown menus, radiobuttons, labels, and more To use these tools, need Java classes and some programming theory  Typically prefer it over console applications Graphical Interfaces

The AWT (Abstract Windowing Toolkit) was developed for the first versions of Java  Created components such as Frame, Panel, Button, TextField, Label However, the look and feel of the AWT varied on different windowing systems  The same AWT Java program looks different when run on MS Windows machines, MACs and Sun Workstations o This is because the underlying windowing systems on those machines differ AWT and Swing

Since a goal of Java is to be platform independent, its look and feel should also be platform independent  Swing was developed from Java v. 1.2 to be more consistent in its look and feel across all platforms  Also adds extra features that did not exist in the AWT  Many Swing components are similar to AWT in name, but with a “J” in front o Ex: JFrame, JPanel, JButton, JTextField, JLabel AWT and Swing

In addition to Swing, since JDK 1.7 programmers also have the option of using JavaFX  This allows for graphics and multimedia across multiple platforms  It is actually a set of packages that work together  We will not be discussing this today, but see: JavaFX

Focus on JFrames  To use: o Create a JFrame object o Size it as desired o Show it on the display  Once we have a JFrame we can do a LOT o Draw graphics within it o Store and organize other components o React to events such as mouse movement and clicking JFrames

JLabels are simple components to show formatted text on the display  Change font type, size and color  Set and change the text itself as desired throughout program execution  Very simple example: o Create a JFrame, then put a JLabel in it and display it  See ex1a.java See ex1a.java  Note how the various objects are created and set up properly JLabels

This example does not really do much  No interaction with the user But it does show us some of the basic setup for graphical applications To Add a bit more functionality  Add a button that user can click to change the color of the label text Simple Example

JButtons are simple components that show text on the display, like a JLabel  In addition to showing text, it responds to clicks of the mouse o If a user clicks the mouse within a JButton, an ActionEvent object is generated in response o This object is passed automatically to an ActionListener object  The ActionListener must be registered to “listen” to the JButton  ActionListener is actually an interface with the single method actionPerformed() JButtons

 Any class that implements actionPerformed() can be an ActionListener This causes the actionPerformed method within the ActionListener to execute  The actionPerformed method is doing the actual response to the button click This idea is called event-driven programming  As program executes, user generates events in various ways Ex: click a button, move the mouse, edit text  Programmer writes code to respond to the various events that may occur Event-Driven Programming

 There are many different types of events in Java programs, but the basic idea for all of them is similar: Some object generates an event object The event object is passed to some event listener object A method in the event listener executes to handle the event  It is important that event handlers are linked to the appropriate event generators Otherwise event will still be generated but will not be responded to  See ex1b.javaex1b.java Event-Driven Programming

 Let’s look at another simple example:  Toggle Button  Click it once and it does an action  Click it again and it does a different action Each click it alternates between the two actions  The setup of this program is very similar to ex1b.java  Only difference is what the listener is doing  See ex1c.javaex1c.java Another Example

 If we want to have multiple components, we need to determine how to lay them out Use a layout manager  These determine how components appear in a window and how much space is allocated for them There are many layout managers in Java  Two simple ones are: 1.FlowLayout Places components as we read a book – left to right top to bottom 2.GridLayout Places components in an equally sized 2-dimensional rectangular grid Multiple Components

 Multiple components may also need to interact with each other  Listener for one component may need to access the other component In this case we must allow the listener access to all components involved -- so it must be different from how we did it in ex1b.java and ex1c.java  Ex: Consider a JTextField This component allows the user to enter text Once user hits “Enter”, the component generates an ActionEvent Same event generated by a JButton Multiple Components

 use to process input from a user  For example to change the contents of a JLabel or a JButton  Let’s look at another example Our JFrame will have a JButton and a JTextField The JButton will behave as in ex1b – clicking it will change the color of the text The JTextField will allow us to enter new text for the JButton Need to set things up differently to allow for more convenient access  See ex1d.javaex1d.java Multiple Components

 What if we want different parts of our window laid out differently?  There is a GridBagLayout that allows for arbitrary configurations, but it is quite complicated to use  A simpler solution is to subdivide our window  We can do this with JPanels Have most of the functionality of JFrames, except without the title/menu bar Can store other components and lay them out using a layout manager More on GUIS

 So now we can use the layout manager of our JFrame to store our JPanels  We can then use our JPanels to store our other components  When doing this, a common way of laying out our JFrame is BorderLayout  BorderLayout subdivides our window into 5 areas NORTH, SOUTH, EAST, WEST, CENTER  We can put a component in each area or just some of them  If the component is a JPanel, we can then put our other components within that  See ex2.javaex2.java More on GUIS

 What if we want to encapsulate data within a JPanel?  The JPanel class contains instance variables and methods, but these are geared toward its graphical function  We can attach components to it but this is solely for display purposes  The variables are still outside the JPanel  What if we want it to also store and manipulate our own data in the JPanel?  We need to extend it using inheritance More on JPanels

 Drawing Shapes and Figures  Java Graphics allow many predefined figures to be drawn drawRect, fillRect, draw3DRect, fill3DRect drawOval, fillOval drawLine drawArc, fillArc drawPolygon, fillPolygon  The Graphics2D class extends the functionality to allow objects implementing the Shape interface to be drawn  MouseEvents  These can be used to recognize and respond to arbitrary actions by the mouse Still More

 See ex3.javaex3.java  More complex example  Shows a more "moderate" graphical program  Uses JPanel subclass, MouseListener and Shapes Another Example