Cosc 4755 Phone programming: GUI Concepts & Threads.

Slides:



Advertisements
Similar presentations
Pages and boxes Building quick user interfaces. learning objectives o Build a quick UI with pages and boxes o understand how pages and boxes work o click.
Advertisements

13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-Oriented Analysis and Design
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Graphical User Interfaces A Quick Outlook. Interface Many methods to create and “interface” with the user 2 most common interface methods: – Console –
Multithreading.
Io package as Java’s basic I/O system continue’d.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
Cosc 5/4730 Information, Resources, and basic GUI concepts.
Applets Java API.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
PROG Mobile Java Application Development PROG Mobile Java Application Development BlackBerry App Lifecycle Java ME API.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Threads CS 3250 Some of these slides contain material by Professor Chuck Allison.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 Web Based Programming Section 8 James King 12 August 2003.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of The Eclipse debugger.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
Concurrent Programming and Threads Threads Blocking a User Interface.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
UID – Event Handling and Listeners Boriana Koleva
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Object Oriented Programming.  Interface  Event Handling.
JavaScript, Fourth Edition
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threading in Java
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Creating a GUI Class An example of class design using inheritance and interfaces.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Multithreaded applets
Chapter 19 Java Never Ends
Ellen Walker Hiram College
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Multithreaded Programming in Java
Multithreading.
Multithreading.
Android Topics UI Thread and Limited processing resources
Android Topics Asynchronous Callsbacks
Event loops 17-Jan-19.
Constructors, GUI’s(Using Swing) and ActionListner
Cosc 4730 An Introduction.
Lecture 19 Threads CSE /6/2019.
CMSC 202 Threads.
Presentation transcript:

Cosc 4755 Phone programming: GUI Concepts & Threads

GUI Concepts general phone programming: – the primary class is a "screen". It has access to the screen and can display things on it. This is our main thread as well. We create and display objects on the screen and then wait for the user to interact with them. – Setup is done in the Constructor/OnCreate method. – Listeners have code that is called when the user interacts with an object.

GUI Concepts (2) The objects will have listeners. – For each object you set any number of listeners. A listener is the method the is called when the user "does something" – like presses a button or enters text in a text box. – This includes external events such as sensor information. If you don't set a listener, then nothing will happen when the user interacts with that object – This maybe what you want to happen.

Example class myScreen extends MainScreen{ // this is blackberry code ButtonField myButton; ButtonListener btnListener; myScreen() { myButton = new ButtonField("Click me"); btnListener = new ButtonListener(); myButton.setChangeListener(btnListener); add(myButton); } //Nothing happens until the user clicks the button class ButtonListener implements FieldChangeListener { public void fieldChanged(Field field, int context) { myButton.setLabel("You Did!"); } } //Once the user clicks the button, this is called } // and the button label is changed.

GUI Concepts (3) Objects have many listeners – You only set the ones you want to deal with – So the ButtonField in the previous example There is also FocusListener that is called whenever the button loses or gains focus. One listener can be used for many objects as well. – In previous example the field parameter allows you to determine which object "changed".

Event Programming In Summary. You code will break down into two main sections Not that the code has to be written that way. Setup – All the code needed display information on the screen – setup the listeners Response/listeners – That code that deals with whatever interaction with the user. This code may also add new objects to the screen as well, depending what is going on. – Some listeners will be separate threads. – It’s important to know when you are on a separate thread or the “main” thread.

Concurrent programming In it's simplest form – two or more programs, processes, or threads running at the same time to complete a task. This can be two completely separate programs running – may not even be coded in the same language This can be two running instances of the same program This can be one program running multiple threads. – Example Web server, every time a new connection is made, the web server spawns a new process to deal with that connection.

Concurrent programming (2) Example (2) – a program had two vectors to multiple together It spawns (new processes or threads) equal to the length of the vector. – Each new process/thread now multiples one row of the vectors together and returns the result. The process/threads new end, leaving the original program with the result. With the exception of overhead, the vector is multiplied together in a time of 1, instead a time of N (where n is the length of the vector)

Threading Similar in nature to launching new processes – Except it in the same program, same process, has access to the same "global" variables in the program. – A process can have many threads. for GUI interfaces: – main thread draws the screen. – A second thread to do calculations » This thread is used to avoid delays in redrawing the screen – More threads to deal with events, such as menu and buttons clicks.

Java Threading When a java program starts, it gets one thread (the main execute line of the program) By extending the thread class, you can add more threads or implements runnable in your class – the void run method must be implemented for either approach.

Java Threading (2) Class myThread extends Thread { //variables, whatever myThread() { //constructor class if needed } public void run() { //called when the thread is started } myThread t = new myThread(); t.start(); //new thread and calls run //this code continues to run //do something else, while thread is also running. Note when execution the run method is done, the thread also ends.

Java Threading (3) A second method exists, called implements, which creates a runnable object (ie a class with a threads), but may extend another class. class myClass implements Runnable { public int a; public void run() { //do something with a } myClass t = new myClass; new Thead(t).start(); //new thread starts and calls run(); System.out.println(t.a);

Quick example import java.io.*; import java.net.*; public class testThread extends Thread { public int count; public String name; testThread(int i, String n) { count = i; name = n; } public void run() { for (int i = 0; i <count; i++) { System.out.println(name + " " + i); } public static void main (String[] args) { // declare the threads to run testThread t1 = new testThread(20,"P1"); testThread t2 = new testThread(30,"P2"); testThread t3 = new testThread(15,"P3"); // start the threads t1.start(); t2.start(); t3.start(); for (int i = 0; i <10; i++) { System.out.println("main" + " " + i); } }

Quick Example 2 In the selfthread class, event() creates a new thread of itself and starts it. – the run method has access to all the same variables and methods in the class as the main thread and can change variables as well. – Note no variable is needed, until the main threads need to interact with the thread. public class selfthread implements runnable{ //variables and other methods void event() { //something happened new Thread(this).start(); //main thread continues and new thread has been created } void run() { //do something, while the main thread is also running. }

But… Android doesn't allow a thread other "main" thread to access the screen. – We use a handler method to send "messages" back to the main thread Blackberry allows access, but you may need to get a event lock for popup windows and to deal with race conditions

Q A &