CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

Slides:



Advertisements
Similar presentations
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Outline Graphics Applets Drawing Shapes Components and Containers Images.
Corresponds with Chapter 12
Copyright © 2014 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design 8 th Edition John Lewis.
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”
Chapter Day 6. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 DUE Problem set 2 posted 
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Graphical User Interfaces
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ITEC220 GUI Lecture – Part 1 References  Java Software Solutions,” by Lewis & Loftus  Chapter 3  Chapter 4  Chapter 5  Chapter 6  Java Foundations-Introduction.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
GUI and event-driven programming An introduction.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Graphical User Interface (GUI) Design using Swing Course Lecture Slides.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
© 2007 Pearson Addison-Wesley. All rights reserved3-1 Using Classes and Objects We can create more interesting programs using predefined classes and related.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Dale Roberts GUI Programming using Java - Introduction Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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 Instructor: Dr. Essam H. Houssein.
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.
Applets Yong Choi School of Business CSU, Bakersfield.
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.
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.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Basics of GUI Programming Chapter 11 and Chapter 22.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Swing One of the most important features of Java is its ability to draw graphics.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
3-1 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: – object creation.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Copyright © 2014 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design 8 th Edition John Lewis.
Copyright © 2014 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design 8 th Edition John Lewis.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphical User Interfaces
A First Look at GUI Applications
CSC 1051 – Data Structures and Algorithms I
Processing Timer Events
Timer class and inner classes
Chapter 3 Using Classes and Objects
Chapter 3 Using Classes and Objects
Steps to Creating a GUI Interface
Chapter 12 GUI Basics.
TA: Nouf Al-Harbi NoufNaief.net :::
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: CSC 1051 M.A. Papalaskari, Villanova University Graphical User Interfaces

Graphical User Interface (GUI) A GUI component – an object that represents a screen element –examples: buttons, text fields, labels, panels, frames GUI-related classes from packages: – java.awt Abstract Windowing Toolkit (AWT): the original Java GUI package –javax.swing Swing provides additional and more versatile components –Both packages are needed to create a Java GUI-based program CSC 1051 M.A. Papalaskari, Villanova University

GUI Container A kind of component: hold and organize other components two main types: frame : a container displayed as a separate window It can be repositioned and resized on the screen as needed has its own title bar with close-minimize-resize buttons a heavyweight container: managed by the underlying operating system panel – a container that cannot be displayed on its own but is used to organize other components A panel must be added to another container (like a frame or another panel) to be displayed a lightweight container: managed by the Java program itself CSC 1051 M.A. Papalaskari, Villanova University

Other components: Labels A label is a GUI component that displays a line of text and/or an image Let's look at a program that organizes two labels in a panel and displays that panel in a frame This program is not interactive, but the frame can be repositioned and resized See Authority.java Authority.java CSC 1051 M.A. Papalaskari, Villanova University

//******************************************************************** // Authority.java Author: Lewis/Loftus // // Demonstrates the use of frames, panels, and labels. //******************************************************************** import java.awt.*; import javax.swing.*; public class Authority { // // Displays some words of wisdom. // public static void main (String[] args) { JFrame frame = new JFrame ("Authority"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); JPanel primary = new JPanel(); primary.setBackground (Color.yellow); primary.setPreferredSize (new Dimension(250, 75)); continued

CSC 1051 M.A. Papalaskari, Villanova University continued JLabel label1 = new JLabel ("Question authority,"); JLabel label2 = new JLabel ("but raise your hand first."); primary.add (label1); primary.add (label2); frame.getContentPane().add(primary); frame.pack(); frame.setVisible(true); }

CSC 1051 M.A. Papalaskari, Villanova University continued JLabel label1 = new JLabel ("Question authority,"); JLabel label2 = new JLabel ("but raise your hand first."); primary.add (label1); primary.add (label2); frame.getContentPane().add(primary); frame.pack(); frame.setVisible(true); }

Nested Panels Containers that contain other components make up the containment hierarchy of an interface This hierarchy can be as intricate as needed to create the visual effect desired The following example nests two panels inside a third panel – note the effect this has as the frame is resized See NestedPanels.java NestedPanels.java CSC 1051 M.A. Papalaskari, Villanova University

//******************************************************************** // NestedPanels.java Author: Lewis/Loftus // // Demonstrates a basic componenet hierarchy. //******************************************************************** import java.awt.*; import javax.swing.*; public class NestedPanels { // // Presents two colored panels nested within a third. // public static void main (String[] args) { JFrame frame = new JFrame ("Nested Panels"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // Set up first subpanel JPanel subPanel1 = new JPanel(); subPanel1.setPreferredSize (new Dimension(150, 100)); subPanel1.setBackground (Color.green); JLabel label1 = new JLabel ("One"); subPanel1.add (label1); continued

CSC 1051 M.A. Papalaskari, Villanova University continued // Set up second subpanel JPanel subPanel2 = new JPanel(); subPanel2.setPreferredSize (new Dimension(150, 100)); subPanel2.setBackground (Color.red); JLabel label2 = new JLabel ("Two"); subPanel2.add (label2); // Set up primary panel JPanel primary = new JPanel(); primary.setBackground (Color.blue); primary.add (subPanel1); primary.add (subPanel2); frame.getContentPane().add(primary); frame.pack(); frame.setVisible(true); }

CSC 1051 M.A. Papalaskari, Villanova University continued // Set up second subpanel JPanel subPanel2 = new JPanel(); subPanel2.setPreferredSize (new Dimension(150, 100)); subPanel2.setBackground (Color.red); JLabel label2 = new JLabel ("Two"); subPanel2.add (label2); // Set up primary panel JPanel primary = new JPanel(); primary.setBackground (Color.blue); primary.add (subPanel1); primary.add (subPanel2); frame.getContentPane().add(primary); frame.pack(); frame.setVisible(true); }

Images Images can be displayed in a Java program in various ways As we've seen, a JLabel object can be used to display a line of text It can also be used to display an image That is, a label can be composed of text, an image, or both at the same time CSC 1051 M.A. Papalaskari, Villanova University

Images The ImageIcon class is used to represent the image that is stored in a label If text is also included, the position of the text relative to the image can be set explicitly The alignment of the text and image within the label can be set as well See LabelDemo.java LabelDemo.java CSC 1051 M.A. Papalaskari, Villanova University

//******************************************************************** // LabelDemo.java Author: Lewis/Loftus // // Demonstrates the use of image icons in labels. //******************************************************************** import java.awt.*; import javax.swing.*; public class LabelDemo { // // Creates and displays the primary application frame. // public static void main (String[] args) { JFrame frame = new JFrame ("Label Demo"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); ImageIcon icon = new ImageIcon ("devil.gif"); JLabel label1, label2, label3; label1 = new JLabel ("Devil Left", icon, SwingConstants.CENTER); continued

CSC 1051 M.A. Papalaskari, Villanova University continued label2 = new JLabel ("Devil Right", icon, SwingConstants.CENTER); label2.setHorizontalTextPosition (SwingConstants.LEFT); label2.setVerticalTextPosition (SwingConstants.BOTTOM); label3 = new JLabel ("Devil Above", icon, SwingConstants.CENTER); label3.setHorizontalTextPosition (SwingConstants.CENTER); label3.setVerticalTextPosition (SwingConstants.BOTTOM); JPanel panel = new JPanel(); panel.setBackground (Color.cyan); panel.setPreferredSize (new Dimension (200, 250)); panel.add (label1); panel.add (label2); panel.add (label3); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); }

CSC 1051 M.A. Papalaskari, Villanova University continued label2 = new JLabel ("Devil Right", icon, SwingConstants.CENTER); label2.setHorizontalTextPosition (SwingConstants.LEFT); label2.setVerticalTextPosition (SwingConstants.BOTTOM); label3 = new JLabel ("Devil Above", icon, SwingConstants.CENTER); label3.setHorizontalTextPosition (SwingConstants.CENTER); label3.setVerticalTextPosition (SwingConstants.BOTTOM); JPanel panel = new JPanel(); panel.setBackground (Color.cyan); panel.setPreferredSize (new Dimension (200, 250)); panel.add (label1); panel.add (label2); panel.add (label3); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); }