© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
VBA Modules, Functions, Variables, and Constants
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng 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.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
1 L46 Advanced GUI Component (1). 2 OBJECTIVES  To create and manipulate sliders, and menus,
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Chapter 8: Writing Graphical User Interfaces
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Designing Interface Components. Components Navigation components - the user uses these components to give instructions. Input – Components that are used.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
Lecture 5(b), Slide 1 CP2030 Copyright © University of Wolverhampton CP2030 Visual Basic for C++ Programmers v Component 5(b) HCI aspects of VB programming.
Graphical User Interface Components Chapter What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Applications Development
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CHAPTER:07 JAVA IDE PROGRAMMING-II Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Topics Introduction Scene Graphs
Object Oriented Programming.  Interface  Event Handling.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
CMPF114 Computer Literacy Chapter 3 The Visual Basic Environment 1.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
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.
CSC 205 Programming II Lecture 5 AWT - I.
A First Look at GUI Applications Radio Buttons and Check Boxes
A First Look at GUI Applications
Java GUI.
Graphical User Interface (pronounced "gooey")
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming

© The McGraw-Hill Companies, 2006 Making choices With graphical applications, there are a number of ways of providing choices to the user: –pull-down menus; –pop-up menus –dialogue windows; –radio buttons; –combo boxes.

© The McGraw-Hill Companies, 2006 Pull-down menus the program below displays a flag consisting of three horizontal stripes; the colour of each stripe can be changed by means of the pull- down menus on the top bar.

© The McGraw-Hill Companies, 2006 There are three aspects to creating a menu: –the menu bar at the top; –the different menus; –the list of menu items associated with each menu.

© The McGraw-Hill Companies, 2006

The constructor the initial colour is assigned to each stripe; the individual menu items are then added to the menus:

© The McGraw-Hill Companies, 2006 these menus are then added to the menu bar: the setJMenuBar method of JFrame is used to add the menu bar to the frame: note that a grid layout has been used to get the stripes where they are wanted.

© The McGraw-Hill Companies, 2006 The actionPerformed method

© The McGraw-Hill Companies, 2006 Pop-up menus a pop-up menu is normally not available all the time, but pops up only when it is necessary, and then disappears; in the application below the menu is used simply to change the background colour of a frame, and is invoked by pressing a button.

© The McGraw-Hill Companies, 2006 note the line of code that declares and creates a new pop- up menu: the menu items are added to this menu in the constructor: in the actionPerformed method, the menu is made visible when the button is pressed, and then is hidden once the background colour has been selected.

© The McGraw-Hill Companies, 2006 An aside note the use of the setFocusPainted method; calling this method with a parameter of false removes the highlighting around the text of the button when it is in focus:

© The McGraw-Hill Companies, 2006 The JDialog class and the JRadioButton class a dialogue window is an alternative to a pop-up menu; the Swing class that we use to produce such a window is JDialog; it is useful for those occasions when we do not want a part of a frame or window permanently devoted to a particular communication because it is only needed at certain times; a JDialog object allows us to add any components we wish to it; below we use radio buttons to change the background colour of a frame, just as we did with the pop-up menu;

© The McGraw-Hill Companies, 2006 the JDialog class is very similar to JFrame, both being derived from the AWT Window class; we have created the JDialog object with the following constructor: as with a JFrame, the JDialog has a default border layout policy; we have changed to a flow layout.; the components are added to the dialogue box as follows.

© The McGraw-Hill Companies, 2006 Note we have used the container method pack; when there is a flow layout policy, this makes the container adjust its size in order to lay the components out in the most compact manner:

© The McGraw-Hill Companies, 2006 it is possible for radio buttons to act independently, or, alternatively, as a group; in this case we require them to behave as a group, because we want only one to be able to be selected at any one time; to do this, we have created an object ButtonGroup: we add each button to the group as follows, so that they act together:

© The McGraw-Hill Companies, 2006 Modal and non-modal dialogues an object of the JDialog class can be modal or non-modal; using the empty constructor creates a non-modal dialogue; this means that any listening components on the originating frame are still enabled and we can therefore interact with the frame even while the dialogue is visible. a modal dialogue works in such a way as to "freeze" any interaction with the parent frame until the dialogue is disposed of; to create a modal dialogue you can use a different constructor, which takes two parameters; the first is a reference to the originating frame; the second is a boolean parameter; if this parameter is true a modal dialogue will be created, if false a non-modal dialogue will be created.

© The McGraw-Hill Companies, 2006 in the above example we could have created a modal dialogue with the following line: this would make it impossible to close the frame while the dialogue window is visible. Note there is a constructor of JDialog which takes just one parameter, a reference to the originating frame; this constructor is useful in the case where the code for the frame resides in a different class to that of the dialogue.

© The McGraw-Hill Companies, 2006 The JComboBox class a combo box provides a choice from a list of options; below is a simple class (ComboBoxDemo) in which a combo box provides the choice of background colour for the frame.

© The McGraw-Hill Companies, 2006 The choices are revealed when the down arrow is clicked:

© The McGraw-Hill Companies, 2006 the JComboBox is created with a constructor that accepts an array of objects - normally Strings - that defines the choices: the first option, "Select colour" is not actually an option, but is there simply to direct the user. an ActionListener is added to the combo box in the constructor:

© The McGraw-Hill Companies, 2006 in the actionPerformed method we use the getSelectedItem method of JComboBox to determine which item was selected; this method returns the selected item, an Object, which in this case must be type cast back to a String. the setSelectedIndex method of JComboBox sets the selected item to the one indicated; in this case we require the selected item to be "Select colour", so that after the selection is made the user sees this displayed again; this is the first item in the list - index 0 of the array.

© The McGraw-Hill Companies, 2006 The JFileChooser class a JFileChooser object interacts with the computer's operating system to enable you to search directories and select files; the class below produces a frame containing a menu bar consisting of a couple of menu options; as well as the menu bar, it provides a text area:

© The McGraw-Hill Companies, 2006 Choosing the Select option from the File menu will cause a dialogue window to appear:

© The McGraw-Hill Companies, 2006 below we have chosen the file called calc.exe; once we select this file a message appears in the text area, telling us the name of the file chosen. if the file chosen is an executable file, then selecting the Run option of the File menu will run the program, in this case the Windows calculator.

© The McGraw-Hill Companies, 2006 we have declared the following attribute: an object of the File class (which resides in the java.io package) will hold a representation of a file; once we have selected a file, its details will be held in this attribute.

© The McGraw-Hill Companies, 2006 The actionPerformed method This is the option associated with choosing the menu item that lets us select a file:

© The McGraw-Hill Companies, 2006 after creating the JFileChooser object, we call the showDialog method to make the dialogue window visible; this requires two parameters: –a reference to the originating component (in our case this frame); –the text required on the accept button, in this case "Select File". once the user has selected the file, the getSelectedFile method is called to return the file selected; this is then assigned to the chosenFile attribute; after this we use the getName method of the File class to display the name of the file in the text area; if we had wanted the full path name to be displayed we could have used getPath instead of getName.

© The McGraw-Hill Companies, 2006 The runChoice option This loads and runs the selected file.

© The McGraw-Hill Companies, 2006 we create an object of the Runtime class and then assign it the return value of the getRuntime method, which is a static method of that class; the object returned by this method contains information about the Java application that is currently running; the Runtime object is then able to execute a command - specific to the particular operating system - as a separate process in the computer's memory; it does this with its exec method, which executes the command that is sent in as a parameter; in the above example we are sending in the full path name of the selected file (by using the getPath method of File); if the file is an executable file then it will be loaded and run. two exceptions have been handled: –a NullPointerException, which would be thrown if the file name sent into the exec method were null - that is if no file had been selected; –an IOException - this would occur it the file were not an executable

© The McGraw-Hill Companies, 2006 The JSlider class a JSlider allows us to control the value of a variable by moving a sliding bar - a slider -which is used to vary a value of an integer within a particular range; the JSlider class has a method called getValue that returns an integer representing the distance that the bar has been moved; below we have placed a slider at the top of a frame, and added a label to show the current value returned by getValue; when you run the program you will see that the default value of the range is 0 to 100

© The McGraw-Hill Companies, 2006 a constructor of JSlider allows us to decide upon the orientation of the slider with a predefined integer parameter that can be: –JSlider.HORIZONTAL; –JSlider.VERTICAL; the interface that we need to implement is ChangeListener (provided in the javax.swing.event library). the method that handles the event is stateChanged, which receives a ChangeEvent object; we report on the movement of the bar by using the getValue method: the minimum and maximum values of the slider default to 0 to 100; if you require alternative limits there is an appropriate constructor provided - this takes two integer parameters, representing the minimum and maximum respectively.