12.07.2013 Views

EPITA Première Année Cycle Ingénieur Atelier ... - wiki-prog - Epita

EPITA Première Année Cycle Ingénieur Atelier ... - wiki-prog - Epita

EPITA Première Année Cycle Ingénieur Atelier ... - wiki-prog - Epita

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>EPITA</strong> <strong>Première</strong> <strong>Année</strong> <strong>Cycle</strong> <strong>Ingénieur</strong><br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

marwan.burelle@lse.epita.fr<br />

http://www.lse.epita.fr<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Overview<br />

1 Going Graphics<br />

2 Swing And Concurrency<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Going Graphics<br />

Going Graphics<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Different toolkits<br />

• AWT: the good-old one, lakes some features and has<br />

a plateform specific look’n’feel (anyway, AWT is<br />

deprecated)<br />

• Swing: a better AWT with a unified Look’n’feel and<br />

better drawing algorithms.<br />

• SWT: the toolkit from Eclipse backed on native<br />

toolkits.<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Swing<br />

• Swing could have been a demonstration of classical<br />

design patterns in action, the whole concept heavily<br />

relies on:<br />

• Decorator<br />

• Composite<br />

• Facade<br />

• Chain of responsibility (internal)<br />

• A graphical application in Java can be seen as tree<br />

(Composite) rooted by a JFrame and where nodes are<br />

widget and layout.<br />

• The layout concept help you hierarchically describes<br />

your interface and provide a nive way to organize<br />

your widget<br />

• The whole API is event-driven meaning that all you<br />

have to do is to connect event and operations<br />

triggered by those events.<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


SwingSet2 demo<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Exploring The BorderLayout<br />

SimpleFrame<br />

import javax.swing.*;<br />

import java.awt.BorderLayout;<br />

import java.awt.Container;<br />

import java.awt.Dimension;<br />

public class SimpleFrame {<br />

public static void main(String[] args) {<br />

javax.swing.SwingUtilities.invokeLater(<br />

new Runnable() {<br />

public void run() { show(); }<br />

}<br />

);<br />

}<br />

}<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Exploring The BorderLayout<br />

SimpleFrame<br />

private static void show() {<br />

JFrame frame = new JFrame("BorderLayout");<br />

frame.setDefaultCloseOperation(<br />

JFrame.EXIT_ON_CLOSE);<br />

JButton labelT = new JButton("Top");<br />

JButton labelB = new JButton("Bottom");<br />

JButton labelC = new JButton("Center");<br />

JButton labelL = new JButton("Left");<br />

JButton labelR = new JButton("Right");<br />

Container pane = frame.getContentPane();<br />

pane.add(labelT, BorderLayout.PAGE_START);<br />

pane.add(labelB, BorderLayout.PAGE_END);<br />

pane.add(labelC, BorderLayout.CENTER);<br />

pane.add(labelL, BorderLayout.LINE_START);<br />

pane.add(labelR, BorderLayout.LINE_END);<br />

frame.pack();<br />

frame.setVisible(true);<br />

}<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Adding an Event Listener<br />

SimpleFrame<br />

static void addButtonEvent(final JButton b) {<br />

final String name = b.getText();<br />

ActionListener listener =<br />

new ActionListener() {<br />

int c = 0;<br />

public void actionPerformed(ActionEvent e) {<br />

out.println(name);<br />

c++;<br />

b.setText(name + " clicked (" + c + ")");<br />

}<br />

};<br />

b.addActionListener(listener);<br />

}<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Swing And Concurrency<br />

Swing And Concurrency<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


We Got Threads ?<br />

• A swing application always runs with 3 threads<br />

doing separate works in order to keep the<br />

application the most responsible as possible:<br />

• Initial Thread: the threads that execute initial<br />

application code.<br />

• Event Dispatch Thread: where all the stuff about event<br />

is done.<br />

• Worker Threads: where the background heavy work is<br />

done.<br />

• These threads are created directly by the framework,<br />

the only things you need to know is how to interract<br />

with them.<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Initial Threads<br />

• The only role of the initial thread is to launch the<br />

invoke the GUI creation that will be run by the Event<br />

Dispatch Thread.<br />

Syntax: invokeLater<br />

javax.swing.SwingUtilities.invokeLater(<br />

new Runnable() {<br />

public void run() { BuildGUI(); }<br />

}<br />

);<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Event Dispatch Thread<br />

• The place for all event and swing activities<br />

• Almost all swing action must be done in that thread:<br />

swing operations are not thread safe, so we must<br />

keep them bound to one thread.<br />

• Task in the Event Dispatch Thread must be short: if a<br />

task take too long, it will make the interface<br />

unresponsive or freezing<br />

• Globaly, if you start your GUI with the previous<br />

method, it will run on the wrigth thread.<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Worker Threads<br />

• Background taskes (often long one) are supposed to<br />

run asynchronously with the GUI and thus run on<br />

one or more Worker Threads.<br />

• javax.swing.SwingWorker is an abstract class used<br />

to build task to be scheduled on the worker threads.<br />

• You must define a method doInBackground that<br />

launch the work<br />

• You may define also a method called done that will<br />

be executed by the Event Dispatch Thread.<br />

• javax.swing.SwingWorker implements the<br />

Futur interface and thus we are able to wait for<br />

result using method get.<br />

• Beware, get is blocking and you’ll probably invoke it<br />

from the Event Dispatch Thread, it then can freeze your<br />

interface. There’s an overloaded variant of get<br />

taking a time-out value to avoid long waiting.<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Live Updates<br />

• We sometime need to push update to the interface<br />

before the end of the computation.<br />

• javax.swing.SwingWorker provides a method<br />

publish that let you make values available for the<br />

Event Dispatch Thread.<br />

• The Event Dispatch Thread will access these values<br />

through the method process that you thus need to<br />

override.<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Workers<br />

Syntax: SwingWorker<br />

class MyWorker extends SwingWorker {<br />

O doInBackground() {<br />

// do your job here<br />

// and public sometimes ...<br />

publish(new I(...));<br />

}<br />

void process(List values) {<br />

// display things here<br />

}<br />

}<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency


Other Tools<br />

• You can cancel a running job from the Event Dispatch<br />

Thread using the method worker.cancel(true)<br />

• The worker will be able to test the cancelation state<br />

using the method isCancelled()<br />

• You can use the <strong>prog</strong>ress property (an int ranging<br />

from 0 to 100) to indicate <strong>prog</strong>ression (using<br />

setProgress and getProgress)<br />

• You also have the state variable indicating the<br />

current state through the possible values: PENDING,<br />

STARTED, DONE<br />

<strong>Atelier</strong> Java - J5<br />

Marwan Burelle<br />

Going Graphics<br />

Swing And<br />

Concurrency

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!