CS12420 – Swing and threads Lynda Thomas

Slides:



Advertisements
Similar presentations
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Advertisements

Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Java Applets- Using SwingWorker Dave Price and Chris Loftus Computer Science Department University of Wales, Aberystwyth.
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
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.
13-Jun-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Thread Examples. Runnable Interface Runnable defines only one abstract method; Public void run(); Thread also implements Runnable interface. Why does.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
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.
GUI Tutorial Images. Useful Info – not on final exam.
Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress.
Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Consolidation. Code making (i.e. making ciphers) is undertaken by a cryptographer whereas cryptanalysts try to break ciphers in order to gain intelligence.
Week 3, Day 1: Processes & Threads Return Quiz Processes Threads Lab: Quiz Lab 3: Strategy & Factory Patterns! SE-2811 Slide design: Dr. Mark L. Hornick.
F27SB2 Software Development 2 Lecture 4: Java GUIs 3.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
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,
BI-SW Training day Outline What is the EDT? Why should I care about it? How do I treat GUI objects properly? SwingWorker and its advantages.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Week 3, Day 1: Processes & Threads Processes Threads SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
CS Lecture 01 Frames and Components and events Lynda Thomas
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Writing Graphic Applications.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
CS Lecture 00 Swing overview and introduction Lynda Thomas
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Concurrent Programming and Threads Threads Blocking a User Interface.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
UID – Event Handling and Listeners Boriana Koleva
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Practical Session 10: Animation. Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and.
6-Jan-16 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
Creating a GUI Class An example of class design using inheritance and interfaces.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
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 Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation.
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.
Threads and Swing Multithreading. Contents I. Simulation on Inserting and Removing Items in a Combo Box II. Event Dispatch Thread III. Rules for Running.
Lecture 15 Basic GUI programming
Multithreaded applets
Lecture 27 Creating Custom GUIs
Object-Orientated Analysis, Design and Programming
Lecture 28 Concurrent, Responsive GUIs
PC02 Consolidation %WITTY_QUOTE%. PC02 Consolidation %WITTY_QUOTE%
Android Topics UI Thread and Limited processing resources
Steps to Creating a GUI Interface
Events, Event Handlers, and Threads
Using threads for long running tasks.
Threads.
Presentation transcript:

CS12420 – Swing and threads Lynda Thomas

Thread Thread of control –The idea was covered by Chris Can have multiple ones in one program See example Bouncer in this directory

In fact, We have already been using multiple threads, because When you use event handling code, this is handled by a different thread

Concurrency and Swing tutorial uiswing/concurrency/index.html A well-written Swing program uses concurrency to create a user interface that never "freezes" — the program is always responsive to user interaction, no matter what it's doing

A Swing programmer deals with the following kinds of threads: Initial threads, the threads that execute initial application code. The event dispatch thread, where all event- handling code is executed. Most code that interacts with the Swing framework must also execute on this thread. Worker threads, also known as background threads, where time-consuming background tasks are executed. The programmer does not need to provide code that explicitly creates these threads: they are provided by the runtime or the Swing framework. But the programmer works with them!

BUT what have we been doing? Initial threads, the threads that execute initial application code. The event dispatch thread, where all event- handling code is executed. Most code that interacts with the Swing framework must also execute on this thread. Worker threads, also known as background threads, where time-consuming background tasks are executed. We have created our GUIs here! We would have done complex stuff here – since all we had Should have done it here

…what you ?need? to know (cookbook style) 1.invokeLater() 2.SwingWorker And a couple of other things 1.Threads for animation 2.Double Buffering

1. invokeLater() Swing components should be created, queried, and manipulated on the event-dispatching thread We have not been doing this. public static void invokeLater(Runnable runnable) –Causes runnable to have its run method called in the dispatch thread of the EventQueue. This will happen after all pending events are processed.

So… Look at the new Bank in this directory It calls invokeLater() to be sure that the GUI is built in the right thread - we should ask the event dispatch thread to build our user interface rather than doing it ourself Code next slide

NEW: public static void main(String args[]) { new BankSwingMVC(); } public BankSwingMVC() { try { SwingUtilities.invokeLater( new Runnable() { public void run() { buildBankSwing(); } } ); } catch (Exception e) { S.o.p("invokeLater exception"+e); } } public void buildBankSwing() { setTitle("Bank"); setDefaultCloseOperation(…..); buttonPanel = new ButtonPanel(this); add ("North",buttonPanel); textPanel = new TextPanel(); add ("Center",textPanel); pack(); setVisible(true); } OLD: public static void main(String args[]) { new BankSwingMVC(); } //constructor builds a window public BankSwingMVC() { setTitle("Bank"); setDefaultCloseOperation(…..); buttonPanel = new ButtonPanel(this); add("North",buttonPanel); textPanel = new TextPanel(); add("Center",textPanel); pack(); setVisible(true); } ……

2. SwingWorker Time consuming code needs to be placed in a separate thread so that your GUI can still operate while it is running Since Java 1.6 this has been formalised in a SwingWorker thread First let’s run the code to see WHY THIS IS A PROBLEM Life-no-threads – doesn’t work properly No update Can’t stop Can’t even quit

How apply to Life There are 3 subdirectories here: –Life-no-threads – doesn’t work properly –Life-home-made-threads – an old solution –Life-properlythreads – PROPER WAY OF DOING IT –I’m actually going to go over the older solution, because it is easier to see the forest for the trees –see next slide

Basic algorithm for when running public void runalot() { int i=0; s=“first iteration" ; while (true) { calc(); if (!change()) { s="stable in "+i+" moves"; repaint(); break;} if (i==100){ s="100 times still changing";repaint(); break;} copy(); i++; s=i+" iterations"; try{ Thread.sleep(20); } catch (Exception e) { System.out.println("exception "+e);} repaint(); } } forever{ calculate next generation copy into current generation slowdown paint screen }

The problem is that runalot() is resource intensive particularly calc() which looks at every square many times for every square (all 100 of them) look at ALL its neighbours and decide if it lives in next generation While it is running nothing else can happen So, instead, it needs to be in its own thread DOESN’T WORK: public void actionPerformed(ActionEvent e){ if (e.getSource()==b1) c.step(); if (e.getSource()==b2) c.clear(); if (e.getSource()==b3) c.runalot(); if (e.getSource()==b4) c.stopit(); } …. class NewCanvas extends JPanel implements MouseListener { //lots deleted public void runalot() { int i=0; s="1 iteration" ; while (true) { calc(); if (!change()) { s="stable in "+i+" moves"; repaint(); break;} if (i==100){ s="100 times still changing";repaint(); break;} copy(); i++; s=i+" iterations"; try{ Thread.sleep(20); } catch (Exception e) { System.out.println("exception "+e);} //tried it with the above and without still won't repaint or stop! repaint(); }

OLD (homemade solution WORKS): //lots deleted //in the actionPerformed() : if (e.getSource()==b3){ runningThread=new Thread(c); runningThread.start(); } class NewCanvas extends JPanel implements MouseListener, Runnable{ //lots deleted public void run(){ //calling start() calls run() int i=0; s=“first iteration" ; keepGoing=true; try{ while (keepGoing) { calc(); if (!change()) { s="stable in "+i+" moves"; repaint(); break;} if (i==100) { s="100 still changing"; repaint(); break;} copy(); i++; s=i+" iterations"; Thread.sleep(200); repaint(); } } catch (InterruptedException e) { } DOESN’T WORK: public void actionPerformed(ActionEvent e){ if (e.getSource()==b1) c.step(); if (e.getSource()==b2) c.clear(); if (e.getSource()==b3) c.runalot(); if (e.getSource()==b4) c.stopit(); } …. class NewCanvas extends JPanel implements MouseListener { //lots deleted public void runalot() { int i=0; s=first iteration" ; while (true) { calc(); if (!change()) { s="stable in "+i+" moves"; repaint(); break;} if (i==100){ s="100 times still changing";repaint(); break;} copy(); i++; s=i+" iterations"; try{ Thread.sleep(20); } catch (Exception e) { System.out.println("exception "+e);} //tried it with the above and without still won't repaint or stop! repaint(); }

CORRECT SOLUTION Java6 and later: class NewCanvas extends JPanel implements MouseListener {//no need for Runnable lots deleted public void startCalc(){ // This method has work to do and so properly creates a SwingWorker, that is, // gets another thread to do that work see ***** below for how kicked off class Calculator extends SwingWorker, String> { public List doInBackground() { int i=0; s=“starting" ; keepGoing=true; try{ while (keepGoing) { board.calc(); i++; if (!board.isChange()) {s="stable in "+i+" moves"; repaint(); break;} if (i>=100) //had to make this >= {s="100 times still changing";repaint();break;} board.copy(); s=“iteration “+ I; // Call the inherited publish method and publish an intermediate result. //This method prepares intermediate results ready for the event dispatch thread (EDT). // When ready the EDT will call the process method with the list of messages. publish(s); i++; Thread.sleep(200); } catch (InterruptedException e) { } return null; } // Overrides the inherited process method called by the EDT when it is ready with a list of intermediate data protected void process(List messagesSoFar) { for (String message : messagesSoFar) { System.out.println(message); repaint(); } // ***** //Create the worker object and then schedule to run it on a worker thread Calculator worker = new Calculator(); worker.execute(); }

Read more at : threads/threads2.html threads/threads2.html The example code is at: threads/src/Example1.java threads/src/Example1.java If you need this you will know (GUI will seize up) You should know about this idea, so you can look it up when needed And you should use the ‘invokeLater’ stuff in 1.

In this lecture (so far) 2 things How to properly set up GUI using invokeLater() How to use SwingWorker so that your GUI doesn’t freeze Here endeth the things about Swing that could be on a CS124 exam

A couple of other odds and ends

1. Animation You may also need to use a new thread for animation Animation is basically just drawing different images (think on paper) in a loop (draw, cover, redraw, etc) Needs thread because otherwise the calls to repaint() get stacked up and then you will not see the multiple images See code in sample-animation-code directory

//This is an example of how to run an animation //It uses a class called NewCanvas to display the animation public class Main extends JFrame implements ActionListener{ private NewCanvas c; //panel for animation private JPanel p; //panel for button private JButton b; //push button /////////////////////////////////////////////////////////// public static void main(String args[]) { new Main(); } //////////////////The next bit is actually separate from the animation //////////////////use the invokeLater() public Main() { try{ SwingUtilities.invokeLater(new Runnable() {public void run() { build(); }}); } catch (Exception e) { System.out.println("invokeLater exception"+e); } public void build(){ //deleted } ///////////////////////////////////////////////////////when button pressed public void actionPerformed(ActionEvent e){ if (e.getSource()==b){ c.move(); //run the animation } //This is the class that actually displays the animation //puts a picture of aber in background and runs 3 faces public class NewCanvas extends JPanel implements Runnable { private MediaTracker mTracker; private Image backgroundImage; //needed for background private Image animateImage[]=new Image[3]; //needed for the images private Thread animator; //need to run this in separate thread private int current=0; //which image for animation //set up public NewCanvas() { //all mediatracker stuff deleted repaint(); current=0; } //called when button pressed public void move(){ animator=new Thread(this); animator.start(); //CALLS run() } //do the animation public void run(){ for (int j=0;j<6;j++) { //LOOP!!!! try { Thread.sleep(200); } catch (InterruptedException e) { break; } current=(current+1)%3; repaint(); //calls paintComponent() } //draw the image public void paintComponent(Graphics g) {

2. Double Buffering Double buffering Change a picture all at once instead of redrawing a bit at a time Several years ago this was really important because of speed Still matters for games – these examples don’t really show benefit run BadUpdateFrame and GoodUpdateFrame

BadUpdateFrame.java import javax.swing.*; public class BadUpdateFrame extends SimpleFrame { private UpdatePanel uppane; private Drawing drawing; public BadUpdateFrame() { drawing = new Drawing(); uppane = new UpdatePanel(drawing); add(uppane); pack(); } public void badDemo() { uppane.repaint(); drawing.flipLines(); uppane.repaint(); drawing.flipLines(); uppane.repaint(); drawing.flipLines(); } …… //deleted } GoodUpdateFrame.java /* create 2 drawings: drawing1 and drawing2 /* public void goodDemo() { uppane.repaint(); drawing2.flipLines(); uppane.setDrawing(drawing2); uppane.repaint(); drawing1.copy(drawing2); drawing1.flipLines(); uppane.setDrawing(drawing1); uppane.repaint(); } */

There is more about this in later modules!

What have I taught that could be on an exam? What is a design pattern? What is the MVC design pattern? What is the Observer/Observable design pattern? Why do we need SwingWorker threads? What does invokeLater() do? What does a Listener class do and how do you use it? Suppose I want to put a button in a Swing application, what are the three things that I need to do?