2_awt

publicité
La hiérarchie d'héritage
Button
Checkbox
List
Abstract Window Toolkit
AWT
Choice
// saisie de texte
TextArea
// plusieurs lignes
TextField
// une seule ligne
TextComponent
Les méthodes importantes des objets graphiques
Component
(abstraite)
Label
// texte
// panneaux
Canvas
Panel
Container
(abstraite)
1
Dialog
Window
Frame
2
Component
Container
java.awt.Container :
java.awt.Component :
Cette classe abstraite est la racine des classes conteneur de l'AWT.
Seules les classes qui héritent de Container peuvent contenir des
composants.
Cette classe abstraite est la racine des classes visibles de l'AWT.
• public void repaint( )
• public void paint(Graphics g)
• public Graphics getGraphics()
• public Container getParent()
• public Dimension getSize()
Peint le composant. Elle n'est
jamais appelée explicitement
par le programme. On peut
par contre la redéfinir.
3
4
• ajout de composant : méthodes add
• suppression de composants :
méthodes remove, removeAll()
• accès aux sous-composants :
• Component getComponent(int n)
• int getComponentCount( )
• Component[] getComponents( )
• gestion du layout :
• void setLayout(LayoutManager layout)
• LayoutManager getLayout( )
Window
Frame
java.awt.Window :
java.awt.Frame :
Fenêtre d'application sans bordure, ni barre de menus; c'est un
conteneur, géré par défaut par un BorderLayout.
Fenêtre munie d'un bord, un bandeau de titre et,
éventuellement, une barre de menus.
Rarement instanciée directement, on utilise ses sous-classes
Dialog et Frame.
• affichage :
• void setVisible( boolean b) , boolean isVisible( )
• void toFront( ) , void toBack( )
• void isShowing( ), void show( )
• suppression :
• void dispose( )
5
Applet
6
• constructeurs (au départ le fenêtre est invisible):
• Frame( )
• Frame(String title)
• titre :
• void setTitle( String title) , String getTitle( )
• taille :
• boolean isResizable( ), void setResizable(boolean b)
• menu :
• void setMenuBar( MenuBar menu),
• MenuBar getMenuBar( )
Dialog
Frame
java.awt.Dialog :
java.awt.Frame :
Fenêtre munies d'un bord et d'une barre de titre mais,
contrairement
aux Frames, elles sont éphémères.
Trois appels sont obligatoires pour afficher la
fenêtre :
• new Frame(...) pour créer l'objet Frame.
• setSize(...) pour donner une taille au cadre, sinon il
sera réduit a quelques pixels.
• setVisible(true) pour faire apparaître le cadre et
pour initier le Thread chargé de détecter et traiter
les actions de l'utilisateur sur le cadre.
7
8
• constructeurs (au départ le fenêtre est invisible):
• Dialog(Frame parent)
• Dialog(Frame parent, String title)
• Dialog(Frame parent, String title, boolean modal)
• titre :
• void setTitle( String title) , String getTitle( )
• taille :
• boolean isResizable( ), void setResizable(boolean b)
• Dialog peut être modal (dépendante) ou non :
• boolean isModal( ), void setModal(boolean b)
non modale l'utilisateur
peut continuer d'interagir
avec le reste de l'application.
Dialog
Dialog
Exemple Dialog :
Dialog
Frame
9
10
import java.awt.*;
public class LancerFenetre extends Frame {
public LancerFenetre(){
this.setTitle("Fenetre avec Dialog");
this.setSize(300, 250);
this.setBackground(Color.GRAY);
this.setLayout(new FlowLayout());
Dialog d = new Dialog(this,"Données",false);
d.setSize(200, 100);
true : il faut fermer
d.setLayout(new FlowLayout());
dialogue avant de
d.add(new Label("Nom : "));
reprendre l'application.
d.add(new TextField(10));
d.setVisible(true);
this.add(new Label("Fenêtre pricipale"));
this.setVisible(true);
}
}
Panel et Applet
La hiérarchie d'héritage
Button
Checkbox
java.awt.Panel :
• constructeurs :
• Panel( )
• Panel (LayoutManager layout)
List
Choice
// saisie de texte
TextArea
// plusieurs lignes
TextField
// une seule ligne
TextComponent
Component
(abstraite)
Label
// texte
Canvas
Container
(abstraite)
11
java.applet.Applet :
• constructeurs :
• Applet( )
• méthodes du cycle de vie :
• void init( ), void start( ), void stop( ), void destroy( )
• récupération des paramètres
• String getParameter(String name)
// panneaux
Panel
Applet
Dialog
Window
Frame
12
Applet
Applet
Une Applet est un programme inséré dans un document HTML
qui est exécuté par un navigateur. Exemple :
<applet code="exempleApplet.Bonjour.class"
width="400" height="300">
</applet>
package exempleApplet;
import java.applet.Applet;
import java.awt.*;
public class Bonjour extends Applet {
public void init(){
this.setLayout(new FlowLayout());
this.setBackground(Color.YELLOW);
this.add(new Label("Hello tous"));
}
13 }
Applet
14
Graphics
Graphics
java.awt.Graphics:
Un contexte graphique est un instance de Graphics qui comporte
l'ensemble des informations et des outils nécessaires
pour effectuer des opérations graphiques.
Exemple
import java.applet.Applet;
import java.awt.*;
La méthode getGraphics et le paramètre de paint permettent
d'obtenir une instance de Graphics. Méthodes :
15
• void drawRect(int x, int y, int width, int height )
• void fillRect(int x, int y, int width, int height )
• void drawOval(int x, int y, int width, int height )
• void fillOval(int x, int y, int width, int height )
• void drawLine(int x1, int y2, int x2, int y2 )
• void drawString(String s, int x, int y )
• setColor(Color c), getColor(), setFont(Font f), getFont()
• etc…
16
public class DessinApplet extends Applet {
public void paint(Graphics g){
g.drawString("Hello Tous !!", 20, 20);
g.setColor(Color.RED);
g.fillOval(30,30,60,50);
g.setColor(Color.BLUE);
g.drawRect(30,30,60,50);
}
}
Canvas
Canvas
java.awt.Canvas :
Exemple
import java.awt.*;
Représente une zone graphique pour faire un dessin.
• constructeurs :
• Canvas( )
• affichage :
• void paint( Graphics g) // Doit être défini !!!
17
public class FenetreExCanvas
extends Frame {
final static int HAUTEUR = 350;
final static int LARGEUR = 450;
public FenetreExCanvas(){
this.setSize(LARGEUR, HAUTEUR);
this.setLayout(new BorderLayout());
this.add(new DessinCanvas(), "Center");
this.setVisible(true);
}
public static void main(String[] args) {
new FenetreExCanvas();
}
}
18
Canvas
La hiérarchie d'héritage
Button
Exemple
import java.awt.*;
public class DessinCanvas extends Canvas {
public void paint(Graphics g){
g.drawString("Autre Canvas",20,20);
g.fillOval(0,0,15,15);
g.fillRect(150,80,200,100);
g.setColor(Color.YELLOW);
g.fillRect(170,100,160,60); Exercice. Faire
g.setColor(Color.RED);
g.drawLine(10,10,200,100);
g.setColor(Color.BLUE);
int x = this.getWidth();
int y = this.getHeight();
g.fillOval(x-16,y-16,15,15);
}
}
Checkbox
List
Choice
// saisie de texte
ce dessin
19
Component
(abstraite)
Label
// texte
// plusieurs lignes
TextField
// une seule ligne
// panneaux
Canvas
Container
(abstraite)
Panel
Window
Frame
TextField et TextArea
java.awt.TextComponent :
java.awt.TextField :
• Constructeurs :
• TextField( ), TextField(int cols)
• TextField(String text), TextField(String text, int cols)
• Taille :
• int getColumns( ), void setColumns(int n)
Classe-mère de TextArea et TextField; rarement utilisée
directement.
• Edition :
• boolean isEditable( ), void setEditable(boolean b)
• String getText( ), void setText(String text)
• Sélection :
• String getSelectedText( )
• int getSelectionStart( ), void setSelectionStart( int n)
• int getSelectionEnd( ), void setSelectionEnd( int n)
• void select(int start, int end)
• void selectAll( )
• int getCaretPosition( ), void setCaretPosition(int pos)
21
Applet
Dialog
20
TextComponent
java.awt.TextArea :
• Constructeurs :
• TextArea( ), TextArea(int rows, int cols)
• TextArea(String text, int rows, int cols, int scrollbars)
• Taille :
• int getColumns( ), void setColumns(int n)
• int getRows( ), void setRows(int n)
22
Button et Label
Checkbox
java.awt.Button :
java.awt.Checkbox :
• Constructeurs :
• Button( )
• Button(String label)
• Constructeurs :
• Checkbox( ), Checkbox(String label),
• Checkbox(String label, boolean checked)
• Checkbox(String l, boolean chkd, CheckboxGroup grp)
• Méthodes
• String getLabel( ), void setLabel(String label)
• boolean getState( ), void setState(boolean checked)
• CheckboxGroup getCheckboxGroup( )
• void setCheckboxGroup(CheckboxGroup groupe)
java.awt.Label :
• String getLabel( ), void setLabel(String label)
23
TextArea
TextComponent
24
List
java.awt.List :
Liste déroulante. L'utilisateur peut choisir un ou plusieurs éléments.
Constructeurs :
• List( ), List(int rows), List(int rows, boolean multiple)
• Méthodes
• void addItem(String item), void addItem(String item, int n)
• void replaceItem(String item, int n), void delItem(int pos)
• int getItemCount(), String getItem(int n), String[] getItems()
• boolean isMultipleMode( )
• void setMultipleMode(boolean checked)
• void select (int pos), void deselect (int pos)
• String getSelectedItem( ), String[] getSelectedItems( )
• int getSelectedIndex( ), int[] getSelectedIndexes( )
• boolean isIndexSelected(int pos)
25
List
Exemple
26
import java.awt.*;
public class ListeDéroulante
extends Frame {
public ListeDéroulante(){
this.setSize(300, 250);
this.setBackground(Color.GRAY);
this.setLayout(new BorderLayout());
List lst = new List(3, false);
lst.add("Lundi");
lst.add("Mardi");
false, un seul item
lst.add("Mercredi");
peut être sélectionné
lst.add("Jeudi");
lst.add("Vendredi");
this.add(lst,"North");
this.setVisible(true);
}
public static void main(String[] args) {
new ListeDéroulante();
}
}
Choice
java.awt.Choice :
Répresente les pop-up menu.
• Constructeurs :
• Choice( )
List est similaire à Choice.
List permet éventuellement d'afficher et
sélectionner simultanément plusieurs items.
Choice
Exemple :
• Méthodes de gestion du menu
• void addItem(String item), void insert(String item, int n)
• void remove(String item), void remove(int pos)
• int getItemCount(), String getItem(int n)
• Méthodes de gestion de la séléction
• void select (int pos), void select (String item)
• String getSelectedItem( )
• int getSelectedIndex( )
• boolean isIndexSelected(int pos)
27
28
Exemple Insertion d'une image (1/3)
jarExecutable
Exemple Insertion d'une image (2/3)
Code Frame :
Résultat :
Hiérarchie de la composition:
C'est similaire
pour une Applet
public class ImageFrame extends Frame {
public ImageFrame() {
this.setSize(500,500);
this.setLayout(new BorderLayout());
Image lune = null;
try {
lune = ImageIO. read(
this.getClass().getResourceAsStream( "lune.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
ImageCanvas ic = new ImageCanvas(lune);
add(ic,"Center");
this.setVisible(true);
this.addWindowListener( new WindowCloser(this));
}
Frame ou Applet (BorderLayout)
CENTER
Canvas
29
import java.awt.*;
public class ListeChoix extends Frame {
public ListeChoix(){
this.setSize(300, 250);
this.setBackground(Color.GRAY);
this.setLayout(new BorderLayout());
Choice choixCouleur = new Choice();
choixCouleur.add( "Rouge");
choixCouleur.add( "Bleu");
choixCouleur.add( "Blanc");
this.add(choixCouleur, "Center");
this.setVisible(true);
}
public static void main(String[] args) {
new ListeChoix();
}
}
30
Exemple Insertion d'une image (3/3)
Code ImageCanvas :
public class ImageCanvas extends Canvas {
private Image image;
public ImageCanvas(Image image) {
this.image = image;
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0,
this.getWidth(),this.getHeight(),this);
}
}
31
Téléchargement