PROJET DE LICENCE : INTERFACE GRAPHIQUE...

113
PROJET DE LICENCE : INTERFACE GRAPHIQUE (1) Intro – Développement Interface Graphique (Swing) Un premier tour d’horizon Composant Graphique, Layout, Event

Transcript of PROJET DE LICENCE : INTERFACE GRAPHIQUE...

Page 1: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

PROJET DE LICENCE :

INTERFACE GRAPHIQUE (1)

Intro – Développement Interface Graphique (Swing)

Un premier tour d’horizon Composant Graphique,

Layout, Event

Page 2: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 2 / 113 Université Nice Sophia Antipolis

JAVA.AWT ET JAVAX.SWING

awt =1ère boite à outil de java

Éléments de base

Component (et Graphics)

Container

Layout (LayoutManager)

swing = extension (d’abord JFC puis intégrer depuis jdk 1.2)

swing : faire que tout fonctionne de manière identique partout

Page 3: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 3 / 113 Université Nice Sophia Antipolis

JAVA.AWT.COMPONENT

Élément de base

Définit un élément graphique (qui sera affiché dans…) avec des propriétés (et des getters / setters) Une Size (class Dimension avec .width et .height)

Une Location (Position) (class Point avec .x et .y)

Coordonnées Origine au coin supérieur gauche

x (width) vers la droite et y (height) vers le bas

Visible (boolean) ; Opaque (boolean)

Une couleur de fond (background) et une couleur d’écriture (foreground)

Etc.

Méthode public void paint(Graphics g)

(0,0)

x

y

Page 4: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 4 / 113 Université Nice Sophia Antipolis

JAVA.AWT.GRAPHICS

Contexte graphique (« morceau d’écran »)

Permet de dessiner Changer de crayon : setColor

drawRect, drawOval, drawPolygon, drawString, fillRect, fillOval

drawImage(img, x, y, ImageObserver)

Obtenu automatiquement (repaint(), redimensionnement, etc.)

Grapgics2D : vue plus « complète » Transformation géométrique (translate, rotate)

Effets graphiques g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g.setStroke(new BasicStroke(epaisseur));

Page 5: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 5 / 113 Université Nice Sophia Antipolis

JAVA.AWT.COLOR

Couleur additive

4 octets (0-255)

Rouge (getRed)

Vert (getGreen)

Bleu (getBlue)

Transparence (getAlpha)

.darker() ; .brighter()

Page 6: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 6 / 113 Université Nice Sophia Antipolis

JAVA.AWT.CONTAINER

Hérite de Component

Pattern Composite

Porte d’autres Component (y compris des Container)

Organisation

LayoutManager

add / remove d’un Component

Différent type d’add, en fonction du Layout

getComponents retourne un tableau de tous ceux inclus

Unicité de lieu (un add ailleurs => déplacement)

Indice des components

Component

Containter

Page 7: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 7 / 113 Université Nice Sophia Antipolis

LAYOUTMANAGER

Définit l’organisation

Basé sur une des tailles (preferred, minum, maximum) ou une maximisation de l’élément

BorderLayout par défaut dans une fenêtre

ajout en précisant la zone

add("North" , comp)

FlowLayout : en ligne

GridLayout : en tableau

GridBagLayout : avec des contraintes

etc.

Page 8: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 8 / 113 Université Nice Sophia Antipolis

APERÇU DU BOXLAYOUT Organisation

Vertical : container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));

Horizontal : ligne. setLayout(new BoxLayout(ligne, BoxLayout.LINE_AXIS));

Généralement basé sur la taille maximum (dépend du component)

Alignement des bords :

setAlignmentX(Component.LEFT_ALIGNMENT); pour aligner les bords gauches

Insertion de « blancs » pour remplir (séparation)

container.add(Box.createVerticalGlue());

ligne.add(Box.createHorizontalGlue());

Insertion de “blancs” pour combler

zoneOutils.add(Box.createVerticalBox());

Insertion de « blancs » pour espacer…

container.add(Box.createRigidArea(new Dimension(5,5)));

Page 9: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 9 / 113 Université Nice Sophia Antipolis

SANS LAYOUT

setLayout(null)

Pas d’intelligence au placement /

dimensionnement

Chaque component doit

Avoir une taille (setSize)

Avoir une position (setLocation)

Positionnement au pixel…

Page 10: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 10 / 113 Université Nice Sophia Antipolis

EXEMPLE DE LAYOUT

Colonne

(Jpanel)

en

BoxLayout

Sous

élément

en

BoxLayout

Fenêtre en

BorderLayout

Un JPanel

sans

Layout

(Layout

null)

Page 11: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 11 / 113 Université Nice Sophia Antipolis

ARBRE DE COMPOSANTS GRAPHIQUES fenêtre

contentPane

...

zoneOutils

Bouton

« Aucune »

Bouton

« Rectangle »

Bouton

« Ellipse »

« West »

1

2

3

Ellipse

zoneDessin

Ellipse

Rectangle

Rectangle

Rectangle

« Center »

1

2

3

4

5

Page 12: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 12 / 113 Université Nice Sophia Antipolis

MÉTHODES À CONNAÎTRE

repaint() ! validate() !

setEnabled(true / false) : activé / désactivé

(Rectangle) getBounds / setBounds(x,y, w, h) : positionne et dimensionne

getWidth() : largeur / getHeight() : hauteur

getX() et getY() : obtenir une coordonnée

setVisible(true / false)

getBackground et setBackground [objet Color, définition RGB]

Certaines sont de JComponent…

Page 13: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 13 / 113 Université Nice Sophia Antipolis

FENÊTRE : CONTAINER DE HAUT NIVEAU

JFrame (Frame, Window Container) getRootPane () zone de la fenêtre sous le titre

getLayeredPane() zone où sont cachés les panneaux non visibles

getContentPane () zone où les éléments sont ajoutés

getGlassPane () zone transparente dessinée au-dessus du JRootPane utilisé pour afficher des pop-up menus

Forward de méthode sur le contentPane

contentPane avec un BorderLayout par défaut

basé sur un transparent d’Audrey Occello

Page 14: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 14 / 113 Université Nice Sophia Antipolis

JFRAME : QUELQUES MÉTHODES

setVisible( boolean )

Une position sur l’écran

Besoin d’une taille

Méthode pack( ) : calcul de la taille optimale (basée

sur les layouts)

WindowListener ou setDefaultCloseOperation

Par défaut, fermer la fenêtre revient à la cacher

Page 15: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

APERÇU DE JAVAX.SWING

basé sur un transparent d’Audrey Occello

Page 16: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 16 / 113 Université Nice Sophia Antipolis

JCOMPONENT

Hérite de Container

Méthodes de commodité

setPreferredSize, setMaximumSize, setMinimumSize

setDoubleBuffered(true/false) / isDoubleBuffered()

setOpaque(true / false)

Dessin à l’écran : paint appel

paintComponent

paintBorder

paintChildren

Surcharge en général de paintComponent, appel au super

Page 18: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 18 / 113 Université Nice Sophia Antipolis

PETITES LISTES DES JCOMPONENTS

les boutons

JButton /JToggleButton / JCheckBox / JRadioButton

java.awt.ButtonGroup (méthode add)

Les îcones : javax.swing.ImageIcon créer avec le nom d’un fichier image par exemple

Les champs textuels

JTextField/ JTextArea

Menus : les JMenuBar, JMenu, JMenuItem

Etc…

http://java.sun.com/docs/books/tutorial/uiswing/

Page 20: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 20 / 113 Université Nice Sophia Antipolis

HÉRITAGE OU UTILISATION

Choix à faire

Cas de la JFrame par exemple :

extends

Champs de classe

Extension de la boite à outils

« Petites » modifications

Graphiques

Ou comportementales

Objet qui n’existe pas

Page 21: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 21 / 113 Université Nice Sophia Antipolis

ÉVÉNEMENTS (1/2)

Chaque élément graphique subit des événements

Principe du CallBack

Interface pour l’écouteur

add et remove pour l’écouté

Appel aux écouteurs par l’écouté lorsque l’événement se

produit

Parcours des composants à l’écran du dessus vers le

dessous) jusqu’au premier « écoutable »

Page 22: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 22 / 113 Université Nice Sophia Antipolis

ÉVÉNEMENTS (2/2)

processEvent : transmettre un événement

Même processus que pour l’affichage

Les objets Event

package java.awt.event et javax.swing.event

getSource() : savoir d’où il vient

Spécialisés : MouseEvent, ActionEvent, etc.

Page 23: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 23 / 113 Université Nice Sophia Antipolis

EXEMPLE « LIVE »

Réalisation d’une JFrame

Ajout d’un bouton

Ajout d’un ActionListener

Page 24: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

PROJET DE LICENCE :

INTERFACE GRAPHIQUE (2)

Swing ; événement, classe interne

Page 25: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

Les événements et leurs utilisations en java

ÉVÉNEMENTS

Page 26: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 26 / 113 Université Nice Sophia Antipolis

ÉVÉNEMENTS

Actions non séquentielles

Besoin d’événement déclencheur

Chaque élément graphique subit des événements

Principe du CallBack

Interface pour l’écouteur

add et remove pour l’écouté

Appel aux écouteurs par l’écouté lorsque l’événement se produit

Parcours des composants à l’écran du dessus vers le dessous) jusqu’au premier « écoutable »

Les étapes :

Il se passe quelque chose sur un objet

Ceux qui observent l’objet s’en rendent compte

Ils réagissent

Pas vraiment de l’observation, mais de l’abonnement…

Page 27: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 27 / 113 Université Nice Sophia Antipolis

PRINCIPE DES ÉVÉNEMENTS

EN JAVA

L’objet qui est source de l’événement

méthode add<Truc>Listener (on peut s’abonner)

Ceux qui observent

implements <Truc>Listener

ils sont ajoutés à la liste des oberservateurs (appel

de add<Truc>Listener)

Quand l’événément Truc se produit

L’objet appelle tous ceux qui l’observe

Page 28: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 28 / 113 Université Nice Sophia Antipolis

1ER EXEMPLE :

JAVA.AWT.EVENT.ACTIONLISTENER

Pour réagir aux clics sur les boutons (par exemple)

addActionListener

java.awt.Button / java.awt.List / java.awt.MenuItem /

java.awt.TextField / java.awt.TrayIcon /

javax.swing.AbstractButton / javax.swing.ButtonModel /

javax.swing.ComboBoxEditor / javax.swing.DefaultButtonModel

/ javax.swing.JComboBox / javax.swing.JFileChooser /

javax.swing.JTextField /

javax.swing.plaf.basic.BasicComboBoxEditor / javax.swing.Timer

Page 29: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 29 / 113 Université Nice Sophia Antipolis

JAVA.AWT.EVENT.ACTIONEVENT

Constructeurs Object source, int id, String command

Object source, int id, String command, int modifiers

Object source, int id, String command, long when, int modifiers

Méthodes spécifiques String getActionCommand() nom de l’action

int getModifiers() savoir si une touche est pressée

long getWhen() quand ?

String paramString()

Méthodes générales (ObjectEvent)

Object getSource() objet à l’origine de l’événement

Page 30: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 30 / 113 Université Nice Sophia Antipolis

JAVA.AWT.EVENT.ACTIONLISTENER

void actionPerformed(ActionEvent e)

Appelé par l’objet source de l’action (sur lequel

l’utilisateur a cliqué)

Page 31: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 31 / 113 Université Nice Sophia Antipolis

UNE FENÊTRE ET 2 BOUTTONS :

FENETRE2BOUTONS (1/2)

// création d’une fenêtre

JFrame f = new JFrame("2 boutons");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// création d’un panel (FlowLayout) comme contenu

JPanel p = new JPanel();

// création de l’écouteur (c.f. plus loin)

MonEcouteurDAction ecouteur = new MonEcouteurDAction(f);

// un bouton

JButton b = new JButton("un bouton");

b.setActionCommand("vous avez cliquez sur un bouton");

b.addActionListener(ecouteur);

Page 32: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 32 / 113 Université Nice Sophia Antipolis

UNE FENÊTRE ET 2 BOUTTONS :

FENETRE2BOUTONS (2/2)

// un menu déroulant

JComboBox j = new JComboBox();

j.addItem("Bonjour");

j.addItem("Au revoir");

j.setActionCommand("vous avez choisi une option du menu

d\u00e9roulant");

j.addActionListener(ecouteur);

// on ajoute le tout dans la fenêtre qui devient visible

p.add(b);

p.add(j);

f.setContentPane(p);

f.pack();

f.setVisible(true);

Page 33: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 33 / 113 Université Nice Sophia Antipolis

UNE FENÊTRE ET 2 BOUTTONS :

MONECOUTEURDACTION (1/2)

implements ActionListener

// un fenêtre nécessaire pour la popup de dialogue

private JFrame parent ;

// le constructeur

public MonEcouteurDAction(JFrame fen)

{

parent = fen;

}

33

Page 34: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 34 / 113 Université Nice Sophia Antipolis

UNE FENÊTRE ET 2 BOUTTONS :

MONECOUTEURDACTION (2/2)

// l’action est affichée (console ou popup)

public void actionPerformed(ActionEvent e)

{

if (parent == null)

{

System.out.println("Action sur "+e.getSource()+" : "+e.getActionCommand());

}

else

{

JOptionPane.showMessageDialog(parent, e.getActionCommand(), "Action", JOptionPane.INFORMATION_MESSAGE);

}

}

Page 35: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 35 / 113 Université Nice Sophia Antipolis

2IÈME EXEMPLE :

JAVAX.SWING.EVENT.MOUSEINPUTLISTENER

Pour écouter les événements liés à la souris

Clic (mousePressed / mouseReleased / mouseClicked)

Survol (mouseEntered / mouseExited)

Déplacement (mouseMoved)

Glisser/Drag (mouseDragged)

Quasiment tout composant graphique

Réunion de java.awt.event.MouseListener et de java.awt.event.MouseMotionListener

Page 36: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 36 / 113 Université Nice Sophia Antipolis

JAVA.AWT.EVENT.MOUSEEVENT

Constructeurs Object source, int id, long when, int modifiers, int x, int y, int clickCount, boolean popupTrigger, int button

Modifiers : masques qui permet de caractériser l’événement

+ 2 autres constructeurs

Méthodes spécifiques int getButton() sur quel bouton de la souris

int getClickCount() nombre de clic (double clic)

Point getLocationOnScreen() position à l’écran

Returns the absolute x, y position of the event.

static String getMouseModifiersText(int modifiers)

Point getPoint() position dans l’objet source

int getX() abscisse dans l’objet source

int getXOnScreen()

int getY()

int getYOnScreen()

boolean isPopupTrigger()

String paramString()

void translatePoint(int x, int

Méthodes générales (ObjectEvent)

Object getSource() objet à l’origine de l’événement

36

Page 37: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 37 / 113 Université Nice Sophia Antipolis

EXEMPLE : GLISSER DÉPOSER (1/2)

Point p;

/*

** MouseListener : on mémorise le début du glisser

*/

public void mousePressed(MouseEvent e)

{

p = e.getPoint();

}

public void mouseReleased(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

Page 38: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 38 / 113 Université Nice Sophia Antipolis

EXEMPLE : GLISSER DÉPOSER (2/2)

/*

** MouseMotionListener

*/

public void mouseDragged(MouseEvent e)

{

Point newP = e.getPoint();

source.setLocation(source.getX()+newP.x-p.x,

source.getY()+newP.y-p.y);

}

public void mouseMoved(MouseEvent e) {}public void

Page 39: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 39 / 113 Université Nice Sophia Antipolis

ÉVÉNEMENTS

processEvent : retransmettre un événement

Ou alors appeler directement les listeners

Même processus que pour l’affichage

Attention au blocage !!

Les objets Event

package java.awt.event et javax.swing.event

getSource() : savoir d’où il vient

Spécialisés : MouseEvent, ActionEvent, etc.

Page 40: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

INNER CLASS

Des classes internes et anonymes

Souvent utiliser avec les listeners

Page 41: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 41 / 113 Université Nice Sophia Antipolis

INNER CLASS

Définition

Dans une classe

Dans une méthode, entre { }

En paramètre (anonyme)

Observable :

ClassePrincipe.class

ClassePrincipe$1NomInterne.class

ClassePrincipe$2.class

Page 42: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 42 / 113 Université Nice Sophia Antipolis

INNER CLASS : POURQUOI ?

Outil supplémentaire

Héritage supplémentaire (sans être un objet du

type)

Masquage de l’implémentation (y compris au

package)

Définition de « callback » à la volée

Utile pour la programmation événementielle

Page 43: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 43 / 113 Université Nice Sophia Antipolis

LIEN ENTRE L’INNER DANS UNE CLASSE ET

L’OUTER

L’inner a accès aux éléments de la classe qui l’inclus

Accès à l’instance de la classe avec <NomDeClasse>.this

Création d’une instance d’une inner classe

<Instance de NomDeClasse>.new <Inner>( )

Ex : Bebete.Etat result = bebete.new Etat();

Bebete est une classe contenant une classe interne Etat ; bebete est une instance de la classe Bebete

Page 44: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 44 / 113 Université Nice Sophia Antipolis

INNER DANS UN BLOC D’INSTRUCTION Ne peuvent pas être utiliser en dehors de la méthode

Utile pour personnaliser des objets sans créer de classe (masquer l’implémentation).

import java.awt.Point;

public class InnerMeth {

Point getP(int x, int y) {

class MyPoint extends Point {

MyPoint(int x, int y) {

super(); this.x = x; this.y = y;

}

public String toString() { return "ceci est un point différent "+super.toString(); }

}

return new MyPoint(x,y);

}

public static void main(String [] args) {

InnerMeth i = new InnerMeth();

System.out.println(new Point(4, 10));

System.out.println(i.getP(4, 10));

}

}

Page 45: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 45 / 113 Université Nice Sophia Antipolis

ANONYMOUS INNER CLASS

Une classe sans nom, définit par un

new <NomDeClasse>(…) { <du code> }

On peut ainsi « étendre » n’importe classe

classe, interface ou classe abstraite

Si on a besoin d’une référence extérieure, il

faut que l’élément soit « final »

Page 46: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

PROJET DE LICENCE :

INTERFACE GRAPHIQUE (3)

Layout ; menu ; bouton

Ressource (images) :

http://docs.oracle.com/javase/tutorial/index.html

Page 47: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

Les différentes « mises en page » internes aux composants graphiques

LAYOUT

Page 48: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 48 / 113 Université Nice Sophia Antipolis

LAYOUTMANAGER

Définit l’organisation Proche du pattern strategy

Basé sur une des tailles (preferred, minum, maximum) ou une maximisation de l’élément

BorderLayout par défaut dans une fenêtre ajout en précisant la zone add("North" , comp) North ou South : hauteur garantie

(préférée) Center : redimensionne au reste East ou West : largeur garantie (préférée) 1 seul composant par emplacement

Page 49: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 49 / 113 Université Nice Sophia Antipolis

BOXLAYOUT Organisation

Vertical : container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));

Horizontal : ligne. setLayout(new BoxLayout(ligne, BoxLayout.LINE_AXIS));

Généralement basé sur la taille maximum (dépend du component)

Alignement des bords :

setAlignmentX(Component.LEFT_ALIGNMENT); pour aligner les bords gauches

Sur les composants ajoutés

Insertion de « blancs » pour remplir (séparation)

colonne.add(Box.createVerticalGlue());

ligne.add(Box.createHorizontalGlue());

Insertion de “blancs” pour combler

zoneOutils.add(Box.createVerticalBox());

Insertion de « blancs » pour espacer…

container.add(Box.createRigidArea(new Dimension(5,5)));

Page 50: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 50 / 113 Université Nice Sophia Antipolis

SANS LAYOUT

setLayout(null)

Pas d’intelligence au placement /

dimensionnement

Chaque component doit

Avoir une taille (setSize)

Avoir une position (setLocation)

Positionnement au pixel…

Page 51: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 51 / 113 Université Nice Sophia Antipolis

FLOWLAYOUT

Composants « alignés » sur une ligne

Si cela ne tient pas on commence une nouvelle ligne

Possibilité de spécifier l’alignement (centré par défaut) sur le layout :

Paramètre du constructeur ou setAlignement(int)

FlowLayout.LEFT ; RIGHT ; CENTER ; LEADING ; TRAILING

Gestionnaire d’affichage par défaut des JPanel

51

basé sur un transparent d’Audrey Occello

Page 52: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 52 / 113 Université Nice Sophia Antipolis

CARDLAYOUT

Permet d’empiler des panneaux (= composants )

Association clef (String) / panneau

Ajout dans le container : Méthode add( Component, "Une clef – chaine de caractère")

Montrer un « panneau » (méthode de CardLayout) : Navigation : first(Container parent) ; last(Container parent) ; next(Container parent) ;

previous(Container parent)

Désignation : show(Container parent, String name)

52

Page 53: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 53 / 113 Université Nice Sophia Antipolis

JTABBEDPANE

JPanel avec CardLaout et intégration des boutons de navigation

Méthodes : addTab (Nom, (Icône,) Component (, Tips) )

insertTab / removeTab

setSelectedIndex / setSelectedComponent / get-

Apparences setTabPlacement(int) avec JTabbedPane.TOP ; BOTTOM ; LEFT ou

RIGHT

Modification des boutons (setIconAt ; setBackgroundAt ; setEnabledAt ; etc.)

setTabComponentAt : personnalisation des boutons indexOfTabComponent : retrouver l’indice d’un bouton

Page 54: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

GRIDLAYOUT

Grille (tableau)

Ordre : équilibrage des lignes

Taille : tous les composants ont la même taille

On déclare le layout de la manière suivante :

new GridLayout(int rows, int cols)

(0 , 1) : une colonne

(1, 0) : une ligne

Modification de la grille setColumns(int cols) / getColumns()

setRows(int rows) / getRows()

54

Page 55: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 55 / 113 Université Nice Sophia Antipolis

DES LAYOUTS POUR LES ÉDITEURS

GRAPHIQUES

GroupLayout

Groupage

{parallèle ; séquentiel} x {vertical, horizontal}

Allignement vertical, horizontal, les deux

GridBagLayout

Expression de contrainte

Grille avec des poids pour chaque case

Page 56: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 56 / 113 Université Nice Sophia Antipolis

MISE EN PAGE

Choix des containers

Choix des layouts

Arbre

Structure hierachique Contenant / Contenu

(Graphe de scène)

Page 57: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

BOUTONS, GROUPE, ACTION

Page 58: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 58 / 113 Université Nice Sophia Antipolis

LES BOUTONS

AbstractButton

JButton

JComponent

JMenuItem JToogleButton

JCheckBox

MenuItem

JMenu

JRadioButton

MenuItem

JCheckBox JRadioButton

Page 59: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 59 / 113 Université Nice Sophia Antipolis

ABSTRACTBUTTON

Plusieurs icônes setIcon ; setPressedIcon ; setRolloverIcon ; setSelectedIcon ;

setRolloverSelectedIcon ; setDisabledIcon ; setDisabledSelectedIcon

ImageIcon (String fileName) ou ImageIcon(URL imageLocation)

Méthode de positionnement du texte (et donc le l’îcone) setVerticalTextPosition(SwingConstants.RIGHT ou

SwingConstants.LEFT ou SwingConstants.CENTER ou SwingConstants.LEADING ou SwingConstants.TRAILING (the default) );

setHorizontalTextPosition(SwingConstants.CENTER (the default) ou SwingConstants.TOP ou SwingConstants.BOTTOM);

addActionListener

doClic( ) : fait comme si on avait cliqué (event)

Page 60: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 60 / 113 Université Nice Sophia Antipolis

JCOMPONENT ET HTML

Un « Bouton » peut contenir du texte en html

"<html>ligne1<br />ligne2</html>"

JButton, JLabel, JMenuItem, JMenu,

JRadioButtonMenuItem, JCheckBoxMenuItem,

JTabbedPane, JToolTip, JToggleButton,

JCheckBox and JRadioButton

Page 61: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 61 / 113 Université Nice Sophia Antipolis

JRADIOBUTTON / JCHECKBUTTON

Bouton à état

isSelected( )

setSelected( boolean) : ne provoque pas d’event

ActionEvent : en sélection uniquement

ItemEvent : en changement (sélection et sélection)

addItemListener

ItemListener : public void itemStateChanged(ItemEvent e)

Page 62: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 62 / 113 Université Nice Sophia Antipolis

BUTTONGROUP Pour en sélectionner un seul parmi n : ButtonGroup

Ce n’est pas un objet graphique

new ButtonGroup()

add(<boutton avec état>) : avec JRadioButton ou JToggleButton ou JRadioButtonMenuItem

getSelected

ButtonModel (ce n’est pas le bouton lui-même mais un objet encapsulé) - fournit getActionCommand

getElements

Retourne une Enumeration<AbstractButton>

pattern iterator

Changement de comportement

Les boutons ne peuvent plus être désélectionnés autrement que pas la sélection d’un autre

Pour reset :

Enumeration<AbstractButton> enumeration = group.getElements(); // liste des éléments du

// ButtonGroup

while (enumeration.hasMoreElements( ) ) // tant qu’il y a des élts

{

AbstractButton ab =enumeration.nextElement(); // on prend le premier disponible

group.remove(ab); // on l’enlève du ButtonGroup

ab.setSelected(false); // on le déselectionne

group.add(ab); // on le remet dans le groupe

}

Page 63: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 63 / 113 Université Nice Sophia Antipolis

JTOOLBAR

Container détâchable

setFloatable( boolean ) // true par défaut

Ajout de component par simple add

Éléments collés

Méthode addSeparator()

Activation des « infobulles »

setRollover( boolean ) // false par défaut

Page 64: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 64 / 113 Université Nice Sophia Antipolis

ACTION : REGROUPEMENT D’ACTIONLISTENER

Interface Action

Classe Abstraite AbstractAction

Version « améliorée » des ActionListener Partage plus facile

Communication par message : méthode putValue(String key , Object value)

Action. NAME : un texte (qui s’affiche sur les boutons ou les menus)

Action. LARGE_ICON_KEY ou Action. SMALL_ICON : une icône

Action. ACTION_COMMAND_KEY : un texte « actionCommand »

Action.MNEMONIC_KEY : un mnémonique

Action.ACCELERATOR_KEY un raccourci clavier

Action. SHORT_DESCRIPTION ou Action. LONG_DESCRIPTION : un texte qui décrit l’action (version longue ou courte utilisée par les bulles d’aide)

Partage : setEnabled qui impactera tout les éléments gérés

Partage : réaction via la méthode actionPerformed

Page 65: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 65 / 113 Université Nice Sophia Antipolis

ACTION : ASSOCIATION

A passer en paramètre au constructeur ou avec

la méthode setAction

Utilisable par AbstractButton

Différents constructeurs de AbstractAction (à

étendre)

http://docs.oracle.com/javase/tutorial/uiswing

/examples/misc/ActionDemoProject/src/misc/

ActionDemo.java

Page 66: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

MENU

Page 67: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 67 / 113 Université Nice Sophia Antipolis

MENU / JMENU

JMenuBar qui porte

les JMenu

Les JMenu portent :

JMenuItem

JRadioButtonMenuItem, etc. (ButtonGroup)

Jseparator (pour séparer)

jframe.setJMenuBar

Page 68: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 68 / 113 Université Nice Sophia Antipolis

JMENUITEM

C’est un AbstractButton Texte, icône…

Ajout de raccourci (Alt+Q) Quand le menu est ouvert :

item.setMnemonic(KeyEvent.VK_Q); (souligne la lettre)

Quand le menu n’est pas forcément ouvert item.setAccelerator(KeyStroke.getKeyStroke(

KeyEvent.VK_Q, InputEvent.ALT_DOWN_MASK));

JMenu porte des JMenuItem Héritage… donc même fonctonnalités

Page 69: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 69 / 113 Université Nice Sophia Antipolis

MENU ET EVENT

JMenuItem

C’est un AbstractButton

ActionEvent

JRadioButtonMenuItem / JCheckBoxMenuItem

ItemEvent possible

Page 70: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 70 / 113 Université Nice Sophia Antipolis

MENU CONTEXTUEL

Généralement par clic droit (Windows)

Un JPopupMenu porte des

JMenuItem

JRadioButtonMenuItem, etc. (ButtonGroup)

JSeparator

MouseListener sur les élèments avec le menu contextuel (mousePressed et mouseReleased) avec le code suivant :

if (e.isPopupTrigger()) {

popup.show(e.getComponent(), e.getX(), e.getY());

}

Page 71: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

POO SEMESTRE 6 :

INTERFACE GRAPHIQUE (4)

Image, Graphics 2D

Ressource (images) :

http://docs.oracle.com/javase/tutorial/index.html

Page 72: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

MANIPULATION D’IMAGE

Page 73: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 73 / 113 Université Nice Sophia Antipolis

JAVA.AWT.IMAGE

Objet représentant les images

getSource : obtenir le « producer »

getHeight( imageObserver ) / getWidth ( imageObserver )

ImageObserver : pour être rappeler en cas de modification de l’image (chargement) Peut être null

Pour les obtenir :

javax.swing.ImageIcon Constructeur avec le nom du fichier

Méthode getImage

java.awt.Toolkit Pas de constructeur, mais une méthode statique (singleton)

java.awt.Toolkit.getDefaultToolkit()

méthode getImage("nomdufichier");

Méthode de Component public Image createImage(ImageProducer producer)

Page 74: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 74 / 113 Université Nice Sophia Antipolis

CLASSE JAVA.AWT.TOOLKIT

Les sous-classes de la classe abstraite Toolkit implantent la partie de AWT qui est en contact avec le système d’exploitation hôte

Quelques méthodes publiques : getScreenSize, getScreenResolution,

getImage,

createImage

getDefaultToolkit fournit une instance de la classe qui implante Toolkit

basé sur un transparent de Richard Grin

Page 75: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 75 / 113 Université Nice Sophia Antipolis

IMAGEICON

Plusieurs constructeurs : un nom de fichier absolu ou relatif

URL (adresse Internet ; objet de type URL)

une image (Image)

un tableau de byte (byte[])

Diverses méthodes getImageLoadStatus() : image chargée ?

MediaTracker.ABORTED, MediaTracker.ERRORED, MediaTracker.COMPLETE, MediaTracker.LOADING

getIconWidth() et getIconHeight() : les dimensions

getImage( ) : la « source »

Page 76: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 76 / 113 Université Nice Sophia Antipolis

IMAGE COMME DES RESSOURCES

Si on indique un chemin : depuis là où est exécuter le programme java

Sous eclipse : racine du projet

Problème en cas de distribution sous forme de jar

getResource de la classe Class

nomFichier : chemin par rapport au .class

URL url = getClass().getResource(nomFichier);

ImageIcon icone = new ImageIcon(url);

Page 77: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 77 / 113 Université Nice Sophia Antipolis

MEDIATRAKER

Intégré dans ImageIcon

Pour attendre la « construction d’une image »

Utilise pour le bon foncionnement // ajout d’image à « synchroniser »

tracker.addImage(changedImg, 1); // choix du “canal”

try

{

tracker.waitForID(1); // attente de toutes les images du canal

}

catch(Exception e)

{

System.out.println("errors while loading images" + e.getMessage());

}

Page 78: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 78 / 113 Université Nice Sophia Antipolis

AFFICHER UNE IMAGE

Avec un Jcomponent

JLabel

JButton, etc.

Par un dessin « explicite »

Méthodes drawImage de Graphics

Page 79: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 79 / 113 Université Nice Sophia Antipolis

JAVA.AWT.GRAPHICS (BIS)

Contexte graphique (« morceau d’écran »)

Permet de dessiner

Changer de crayon : setColor

drawRect, drawOval, drawPolygon, drawString,

fillRect, fillOval

Obtenu automatiquement (repaint(),

redimensionnement, etc.)

Page 80: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 80 / 113 Université Nice Sophia Antipolis

JAVA.AWT.GRAPHICS (BIS) - DRAWIMAGE

Dessin telle quelle (avec une éventuelle couleur de fond)

drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)

drawImage(Image img, int x, int y, ImageObserver observer)

Dessin avec redimensionnement

drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)

drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)

Dessin d’une sous partie de l’image avec redimensionnement

drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)

drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)

Page 81: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 81 / 113 Université Nice Sophia Antipolis

OÙ DESSINER ?

Surface d’affichage du

Composant

(insets : pour laisser la place

pour les bordures)

(0,0) = ( getX(), getY() )

Insets insets = getInsets();

insets.top

insets.bottom

insets.left insets.right

getWidth( )

getHeight( )

x

y

basé sur un transparent de Richard Grin

Page 82: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 82 / 113 Université Nice Sophia Antipolis

GRAPHICS ET CHAINE DE CARACTÈRE : FONT

Class Font Méthodes d’accès aux caractéristiques

getSize( ) / getSize2D( )

isBold( ) / isItalic( ) / isPlain( )

Construction : Font(String name, int style, int size)

Méthode static Font.getFont(String name)

Méthode getFont( ) de Component

Méthodes de Font deriveFont Taille (float), style (Font.PLAIN ; Font.BOLD ; Font.ITALIC ,

Font.BOLD | Font.ITALIC ), (AffineTransformation)

Exemple pour grossir la taille d’un component : component.setFont( component.getFont().deriveFont(72f) ) ;

Page 83: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 83 / 113 Université Nice Sophia Antipolis

CALCULEZ LA TAILLE D’UNE CHAINE

Connaitre en pixel la taille d’une chaine Placer dans le Graphics

Découper en « lignes »

Couper (…)

Méthode getFontMetrics De component : public FontMetrics getFontMetrics(Font font)

De Graphics : public FontMetrics getFontMetrics()

FontMetrics Permets de connaitre les dimensions de la font

Largeur (int) : charWidth(char ch) / stringWidth(String str)

Espace occupé dans un graphics (Rectangle2D) : getStringBounds(String str, Graphics context)

Hauteur d’une ligne (int) : getHeight( )

Page 84: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 84 / 113 Université Nice Sophia Antipolis

DESSINER UNE CHAINE DANS UN GRAPHICS

(Calculs avec les FontMetrics)

Peut être assez complexe pour gérer tous les cas

drawString(String str, int x, int y)

Page 85: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 85 / 113 Université Nice Sophia Antipolis

GRAPHICS2D

En fait, la méthode paintComponent reçoit une instance de la classe Graphics2D, sousclasse de Graphics

Graphics2D offre beaucoup plus de possibilités que Graphics rotations,

les mises à l’échelle,

le choix de la largeur de trait, g.setStroke(new BasicStroke(epaisseur)) ;

Antialiasing, g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

le tracé de rectangle 3D

Graphics2D g2 = (Graphics2D) g;

basé sur un transparent de Richard Grin

Page 86: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 86 / 113 Université Nice Sophia Antipolis

JAVA.AWT.IMAGE.BUFFEREDIMAGE

C’est une java.awt.Image

Création simple BufferedImage(int width, int height, int imageType)

Largeur, hauteur

TYPE_3BYTE_BGR, TYPE_4BYTE_ABGR...

Pour dessiner dedans public Graphics2D createGraphics()

Manipulation d’image getRGB(x,y) // coordonnées pixel

// retourne un int « rgb » // rgb : sur 3 ou 4 octet (alpha, red, green, blue)

setRGB(x, y, rgb) Traitement par zone

Page 87: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 87 / 113 Université Nice Sophia Antipolis

EXEMPLE : PHOTOCOPIER UN COMPONENT

public class Cloning extends JComponent {

BufferedImage img ;

public Cloning(JComponent cpt)

{

img = new BufferedImage(cpt.getWidth(), cpt.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);

Graphics2D g = img.createGraphics();

cpt.paint(g);

setSize(cpt.getSize());

setPreferredSize(getSize());

setMaximumSize(getSize());

setMinimumSize(getSize());

}

public void paintComponent(Graphics g)

{

if (img != null) g.drawImage(img, 0, 0, null);

}

}

On prend les dimensions du copié

On prend

une photo

du copié

On dessine la photo

Page 88: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 88 / 113 Université Nice Sophia Antipolis

DOUBLE-BUFFERING EN AWT

java.awt.image.BufferedImage buffImg = new BufferedImage(width, height, type);

type : BufferedImage.TYPE_4BYTE_ABGR par exemple

Dans paint(Graphics g) : Graphics grph = buffImg.createGraphics();

// on dessine dans grph

g.drawImage(buffImg, 0, 0, null)

setDoubleBuffered pour SWING

Optimisation toujours possible Dessin par tranche, pour ne pas « tout » redessiner

Page 89: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 89 / 113 Université Nice Sophia Antipolis

IMAGEIO

Paquetage javax.imageio ; class ImageIO

Ce paquetage permet de charger et sauvegarder facilement n’importe quel type d’image

Il utilise la notion de plugin pour supporter les différents types d’image

Méthodes static read pour lire depuis un fichier local (File), un flot ou un URL (lecture directe sans utilisation d’un thread en parallèle)

Méthodes write pour écrire un BufferedImage dans un fichier ou un flot, en choisissant le codage d’écriture

Format par défaut : gif, png, jpg

des paquetages sur le Web pour ajouter d’autres formats

public static void scanForPlugins() Scans for plug-ins on the application class path

String[] formatNames = ImageIO.getReaderFormatNames();

Page 90: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 90 / 113 Université Nice Sophia Antipolis

IMAGEIO

BufferedImage image ;

// après construction… Graphics g = image.getGraphics(); // on peut dessiner dans g, par exemple avec // component.paint(g) ; File imageFile = new File(filename); try { ImageIO.write(image, "png", imageFile); } catch (IOException e) { System.err.println("echec dans la sauvegarde"); }

Page 91: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

PROJET DE LICENCE :

INTERFACE GRAPHIQUE (5)

Input, KeyListener & Focus, Evénements liés aux

composants…

Ressource (images) :

http://docs.oracle.com/javase/tutorial/index.html

Page 92: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

Saisie de Texte / Comment entrez des valeurs

INPUT

Page 93: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 93 / 113 Université Nice Sophia Antipolis

LE TEXTE EN SWING

Jlabel : texte (et icone) non modifiable

JTextField : entrer du texte (sur une ligne) Touche « entrée » provoque un

actionListener

JTextArea : quelques lignes de texte Édition « texte brut »

JEditorPane : édition « formatée » StyledDocument (structure

hiérarchique) addDocumentListener /

addUndoableEditListener

undo / redo

JTextPane (découpage en paragraphe)

basé sur un transparent de Richard Grin

Page 94: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 94 / 113 Université Nice Sophia Antipolis

HÉRITAGE

JTextComponent

JTextField

JComponent

JEditorPane JTextArea

JTextPane

JLabel

JPasswordField JFormattedTextField

Manipule des

StyledDocument

Utilise des formats comme :

amountFormat = NumberFormat.getNumberInstance( );

Page 95: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 95 / 113 Université Nice Sophia Antipolis

JTEXTCOMPONENT

Modèle = Document / Vue / Contrôleur

Contient les méthodes de base pour traiter une zone de saisie ou/et d’affichage de texte : {get/set}Text pour obtenir ou mettre le texte (ou une partie

du texte) contenu dans le composant addInputMethodListener

setEditable() pour indiquer si l’utilisateur peut modifier le texte

copier/couper/coller avec le clipboard du système

utilisation et gestion du point d’insertion (caret) addCaretListener

etc.

basé sur un transparent de Richard Grin

Page 96: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

Interagir avec le clavier, savoir sur quel composant cela se passe,

EVENEMENT CLAVIER ET FOCUS

Page 97: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 97 / 113 Université Nice Sophia Antipolis

EVÉNEMENTS CLAVIER

KeyListener

Méthodes keyPressed puis keyTyped puis

keyReleased

KeyEvent

Constante VK_xxx pour le code de la touche

Hérite de InputEvent : donc getModifiers() et

isAltDown(), etc.

getKeyChar() et getKeyCode() (code de la touche)

Page 98: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 98 / 113 Université Nice Sophia Antipolis

FOCUS

Les événements claviers vont sur le composant qui a le focus Un seul composant peut « avoir le focus » à un moment

donné

La fenêtre qui « a le focus » est celle qui contient ce composant

2 autres façons pour obtenir le focus Le plus souvent un composant obtient le focus quand

l’utilisateur clique sur lui

Il peut aussi l’obtenir par programmation avec les méthodes requestFocus() ou requestFocusInWindow() de la classe Component

Attention au retour d’une fenêtre (de dialogue)

Page 99: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 99 / 113 Université Nice Sophia Antipolis

MÉTHODES ET FOCUS

Dans la classe Component la méthode boolean isFocusable() permet de savoir si

un composant peut l’avoir

void setFocusable(boolean) permet de modifier cette propriété

Dans la classe Component, 2 méthodes pour passer au composant suivant ou précédent : transfertFocus et transfertFocusBackward les fenêtres sont des composants

FocusListener : méthodes focusGained et focusLost

basé sur un transparent de Richard Grin

Page 100: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 100 / 113 Université Nice Sophia Antipolis

MNEMONICS ET ACCELERATORS

Raccourcit des boutons / menus Alt (mouseless modifier) + touche

Dans la class AbstractButton setMnemonic(code) avec un code de touche (KeyEvent.VK_xxx)

Pour les menus : il faut que le menu soit ouvert

Dans la class JTabbedPane setMnemonicAt(int tabIndex, int code) avec pour la tabIndex un

indice d’un onglet

Accelerateur pour les JMenuItem Raccourcit clavier

KeyStroke.getKeyStroke(int keyCode, int modifiers) C.f . KeyEvent et InputEvent pour les modifiers

basé sur un transparent de Richard Grin

Page 101: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 101 / 113 Université Nice Sophia Antipolis

TOOLTIPS : AIDE CONTEXTUELLE

On peut ajouter une bulle d’aide ou de

description à n’importe quel composant :

composant.setToolTipText("Texte");

Cette bulle s’affiche lorsque le pointeur de la

souris est positionné depuis un certain temps

sur le composant

basé sur un transparent de Richard Grin

Page 102: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

EVÉNEMENTS LIÉS AUX COMPOSANTS

Page 103: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 103 / 113 Université Nice Sophia Antipolis

COMPONENTLISTENER

Réagir à des changements sur le component

ComponentListener componentHidden(ComponentEvent)

Called after the listened-to component is hidden as the result of the setVisible method being called.

componentMoved(ComponentEvent) Called after the listened-to component moves, relative to its container. For example, if a

window is moved, the window fires a component-moved event, but the components it contains do not.

componentResized(ComponentEvent) Called after the listened-to component's size (rectangular bounds) changes.

componentShown(ComponentEvent) Called after the listened-to component becomes visible as the result of the setVisible

method being called.

All of these methods are also in the adapter class, ComponentAdapter.

Il existe aussi ContainerListener (add / remove)

Page 104: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 104 / 113 Université Nice Sophia Antipolis

COMPONENTEVENT

ComponentEvent

Component getComponent()

Returns the component that fired the event. You can use

this instead of the getSource method.

public String paramString()

Returns a parameter string identifying this event. This

method is useful for event-logging and for debugging.

Page 105: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

OUTILS COMPLÉMENTAIRES

Page 106: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 106 / 113 Université Nice Sophia Antipolis

FENÊTRAGE INTERNE

JInternalFrame : Jcomponent à placer à

l’intérieur d’une fenêtre et qui se comporte

comme une fenêtre

Iconifier, Agrandir, Déplacer, etc.

Va de paire avec JDesktopPane (ou un layout

null)

http://docs.oracle.com/javase/tutorial/uiswing

/components/internalframe.html

Page 107: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 107 / 113 Université Nice Sophia Antipolis

CLAQUE (JLAYER)

Design Pattern Decorator

Ajoute un « plus », ici graphique

http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html

class WaitLayerUI extends LayerUI<JPanel> implements ActionListener Avec une méthode

public void paint (Graphics g, JComponent c)

Création du panel à décorer

JLayer<JPanel> jlayer = new JLayer<JPanel>(panel, layerUI);

Ajout du jlayer à la fenêtre (et non pas du panel)

Le JLayer est le décorateur, le LayerUI est la « stratégie » de décoration

Délagation de la décoration à la classe LayerUI

on ne refait que le LayerUI, on redessine ce qu’on veut

Page 108: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 108 / 113 Université Nice Sophia Antipolis

COMPORTEMENT DE CERTAINS WIDGETS

Comportement des boutons (feedback)

JComboBox (menu déroulant)

Etc.

Modification avec ButtonGroup

Comportement interne

Gestion de la taille

Gestion de l’alignement

Page 109: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

Un bilan sous forme de questions

QUESTIONS DIVERSES

Page 110: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 110 / 113 Université Nice Sophia Antipolis

HÉRITAGE OU DÉLÉGATION ?

Une question : l’objet doit-il « être un » (un composant graphique) ?

Héritage quand :

Modification du comportement

Lourd

ou systématique

Délégation sinon

Décoration possible (pour ajout de fonctionnalité mineur)

C.f. JScrollPane, JLayer, etc.

Page 111: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 111 / 113 Université Nice Sophia Antipolis

LISTENER : UNE INSTANCE OU PLUSIEURS ?

Généralement plus simple de plusieurs instances

Autant que d’objet écoutés

Évite des tests sur la source (et donc de la dépendance

dans le code)

Cas particulier des Action

Eventuellement (rare) une seule instance si

Besoin de « synchroniser » des sources d’événement

Besoin d’économiser

Page 112: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 112 / 113 Université Nice Sophia Antipolis

LISTENER : CLASSE OU CLASSE INTERNE OU

CLASSE INTERNE ANONYME ?

À nouveau la question : l’objet doit-il « être un » (listener) ?

Variation dans la réutilisation, le dynamisme :

Classe : listener réutilisable, choix à faire lors des instanciation

Classe interne : rattachement à la classe « écoutée », réutilisation plus compliquée

Classe interne anonyme : comportement interne à la classe « principale », pas d’exportation de ce comportement

Page 113: PROJET DE LICENCE : INTERFACE GRAPHIQUE (1)deptinfo.unice.fr/twiki/pub/Linfo/ProjetDeLicence201314/...Université Nice Sophia Antipolis L3 Informatique – Projet de Licence – dev

L3 Informatique – Projet de Licence – dev UI – Philippe Renevier Gonin 113 / 113 Université Nice Sophia Antipolis

INTERFACE GRAPHIQUE : EDITION MANUELLE OU

ÉDITEUR (À LA NETBEANS) ?

Édition à la main

Parfois « rédhibitoire »

Maitrise totale

Editeur Graphique d’Interface

Facile et rapide

Code plutôt laid : nom des variables, listener qui appel des méthodes privées, etc.

code « non modifiable » dans l’éditeur

Utilisation de layout complexe (GroupLayout ou GridBagLayout) et de classe propre à l’éditeur