Chapter 18. Advanced graphics programming

Similar documents
GUI Event Handlers (Part II)

Graphical User Interfaces. Comp 152

Programming graphics

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Swing/GUI Cheat Sheet

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Summary Chapter 25 GUI Components: Part 2

Chapter 6: Graphical User Interfaces

EVENTS, EVENT SOURCES AND LISTENERS

Chapter 17 Creating User Interfaces

Points Missed on Page page 1 of 8

Graphical User Interface (GUI)

To gain experience using GUI components and listeners.

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Part I: Learn Common Graphics Components

Content Area: Mathematics Course: Computer Programming Grade Level: R14 The Seven Cs of Learning

Introduction to the JAVA UI classes Advanced HCI IAT351

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Java Event Handling -- 1

Packages: Putting Classes Together

To gain experience using arrays, manipulating image data, and using inheritance.

Graphical User Interface (GUI)

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Index SELF-STUDY. Symbols

Part 3: Graphical User Interface (GUI) & Java Applets

CS 251 Intermediate Programming GUIs: Components and Layout

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

JDirectoryChooser Documentation

BASICS OF GRAPHICAL APPS

To gain experience using arrays, manipulating image data, and using inheritance.

Unit 6: Graphical User Interface

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Computer Science 210: Data Structures. Intro to Java Graphics

CSC207 Week 4. Larry Zhang

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Final Exam CS 251, Intermediate Programming December 13, 2017

CS111: PROGRAMMING LANGUAGE II

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Chapter 6 Using Objects


IT101. Graphical User Interface

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

JAVA NOTES GRAPHICAL USER INTERFACES

Intro to Computer Science II. Exceptions

Agenda. Container and Component

Widgets. Widgets Widget Toolkits. User Interface Widget

11/27/2007 TOPICS CHAPTER TOPICS LISTS READ ONLY TEXT FIELDS. Advanced GUI Applications. This module discusses the following main topics:

Contents Chapter 1 Introduction to Programming and the Java Language

8/23/2014. Chapter Topics. Chapter Topics. Lists. Read Only Text Fields. Chapter 13: Advanced GUI Applications

Word Track Changes

Java IDE Programming-I

GUI Software Architecture

Chapter 12 Advanced GUIs and Graphics

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

Swing. By Iqtidar Ali

RAIK 183H Examination 2 Solution. November 10, 2014

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

We are on the GUI fast track path

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

CIS 120 Final Exam May 7, Name (printed): Pennkey (login id):

Here are the steps to get the files for this project after logging in on acad/bill.

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Exercises Lecture 3 Layouts and widgets

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

More About Objects and Methods

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Swing from A to Z Some Simple Components. Preface

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Chapter 7: A First Look at GUI Applications

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Graphical User Interface (GUI)

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

PHPRad. PHPRad At a Glance. This tutorial will show you basic functionalities in PHPRad and

CS11 Java. Fall Lecture 4

Supporting Materials

Java Help Files. by Peter Lavin. May 22, 2004

GUI Components Continued EECS 448

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Chapter 8: GUI Dialog & Table. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh

Graphical User Interface (Part-3) Supplementary Material for CPSC 233

Outline CSE 3461 F10. What is a Widget? Properties of Widgets. A self-contained screen object Also called a control Examples of widgets:

Data Structures. 03 Streams & File I/O

Table of Contents WINDOWS 95

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

Contents Introduction 1

Graphical User Interfaces (GUIs)

Object Oriented Programming

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

Transcription:

Chapter 18 Advanced graphics programming

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.

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 pulldown menus on the top bar.

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 constructor the initial colour is assigned to each stripe; the individual menu items are then added to the menus:

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 actionperformed method

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.

note the line of code that declares and creates a new popup 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.

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 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 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.

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:

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:

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.

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 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 choices are revealed when the down arrow is clicked:

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:

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 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:

Choosing the Select option from the File menu will cause a dialogue window to appear:

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.

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 actionperformed method This is the option associated with choosing the menu item that lets us select a file:

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 runchoice option This loads and runs the selected file.

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 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

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.