This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexte.java
366 lines (299 loc) · 11.7 KB
/
Texte.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*********************************************************************/
/* */
/* Copyright 2012-2017 Rémi Synave, Anthony Desitter, */
/* Nicolas Dubrunfaut, Maxime Langa, */
/* Guillaume Langa */
/* */
/* This file is part of MG2D. */
/* This library uses javazoom library piece of code */
/* http://www.javazoom.net */
/* and can be found with this library (file jlayer1.0.1.tar.gz) */
/* */
/* MG2D is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published */
/* by the Free Software Foundation, either version 3 of the License, */
/* or (at your option) any later version. */
/* */
/* MG2D is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with MG2D. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*********************************************************************/
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import MG2D.geometrie.*;
/**
* Cette classe permet la création de textes.<br />
* Un Texte est définie par une chaîne de caractères, d'une police de texte et d'un Point.
* @author Equipe 2D, Rémi Synave
* @version 2.8
*/
public class Texte extends Dessin {
// Attributs //
//constantes utiilisée pour centrer le texte
public static final int CENTRE=0;
public static final int GAUCHE=1;
private String texte;
private Font police;
private Point a; // Il s'agit du point central du texte /!\ //
private int largeur, hauteur;
private int typeAlign;
// Constructeur //
/**
* Construit le texte noir "Ceci est un texte" centré en (100,100).
*/
public Texte(){
super();
texte = new String("Ceci est un texte");
police = new Font("Calibri", Font.TYPE1_FONT, 10);
a = new Point(100,100);
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics();
FontMetrics metr = g.getFontMetrics ( police );
largeur = metr.stringWidth ( texte );
hauteur = police.getSize();
typeAlign = CENTRE;
}
/**
* Construit un texte sur le modèle d'un texte.
* @param t Le texte à copier.
*/
public Texte (Texte t){
super(t.getCouleur());
texte=new String(t.texte);
police = new Font(t.police.getFontName(), t.police.getStyle(), t.police.getSize());
a = new Point(t.a);
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics();
FontMetrics metr = g.getFontMetrics ( police );
largeur = metr.stringWidth ( texte );
hauteur = police.getSize();
typeAlign=t.typeAlign;
}
// Couleur par défaut //
/**
* Construit un texte centré à partir d'une chaîne de caractères, d'une police et d'un Point.<br />
* Il s'agit du constructeur sans couleur, ainsi l'objet sera noir.<br />
* /!\ Attention ! Le Point correspondant au centre du Texte.
* @param texte Chaîne de caractères à afficher.
* @param police Police de texte.
* @param a Point central du texte.
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/awt/Font.html" target="_blank">Font</a>
* @see Point
*/
public Texte ( String texte, Font police, Point a ) {
super ( Couleur.NOIR );
this.texte = new String(texte);
this.police = police;
this.a = new Point ( a );
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics();
FontMetrics metr = g.getFontMetrics ( police );
largeur = metr.stringWidth ( texte );
hauteur = police.getSize();
typeAlign = CENTRE;
}
/**
* Construit un texte à partir d'une chaîne de caractères, d'une police et d'un Point et d'un type d'alignement.<br />
* Il s'agit du constructeur sans couleur, ainsi l'objet sera noir.<br />
* /!\ Attention ! Le Point correspondant au centre du Texte.
* @param texte Chaîne de caractères à afficher.
* @param police Police de texte.
* @param a Point central du texte.
* @param typeAlign type d'alignement (gauche ou centre).
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/awt/Font.html" target="_blank">Font</a>
* @see Point
*/
public Texte ( String texte, Font police, Point a, int typeAlign ) {
super ( Couleur.NOIR );
this.texte = new String(texte);
this.police = police;
this.a = new Point ( a );
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics();
FontMetrics metr = g.getFontMetrics ( police );
largeur = metr.stringWidth ( texte );
hauteur = police.getSize();
this.typeAlign = typeAlign;
}
// Couleur définie par l'utilisateur //
/**
* Construit un Texte centré à partir d'une chaîne de caractères, d'une police et d'un Point.
* @param couleur Couleur du Texte
* @param texte Chaîne de caractères à afficher.
* @param police Police de texte.
* @param a Point central du texte.
* @see Couleur
* @see Point
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/awt/Font.html" target="_blank">Font</a>
*/
public Texte ( Couleur couleur, String texte, Font police, Point a ) {
super ( couleur );
this.texte = new String(texte);
this.police = police;
this.a = new Point ( a );
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics();
FontMetrics metr = g.getFontMetrics ( police );
largeur = metr.stringWidth ( texte );
hauteur = police.getSize();
typeAlign = CENTRE;
}
/**
* Construit un Texte à partir d'une chaîne de caractères, d'une police et d'un Point et d'un type d'alignement.
* @param couleur Couleur du Texte
* @param texte Chaîne de caractères à afficher.
* @param police Police de texte.
* @param a Point central du texte.
* @param typeAlign type d'alignement (gauche ou centre).
* @see Couleur
* @see Point
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/awt/Font.html" target="_blank">Font</a>
*/
public Texte ( Couleur couleur, String texte, Font police, Point a, int typeAlign ) {
super ( couleur );
this.texte = new String(texte);
this.police = police;
this.a = new Point ( a );
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics();
FontMetrics metr = g.getFontMetrics ( police );
largeur = metr.stringWidth ( texte );
hauteur = police.getSize();
this.typeAlign = typeAlign;
}
// Accesseurs //
// Getter //
/**
* Retourne la chaîne de caractères à afficher.
* @return Le texte à afficher.
*/
public String getTexte () {
return new String(texte);
}
/**
* Retourne la police de texte.
* @return Police de caractères utilisée.
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/awt/Font.html" target="_blank">Font</a>
*/
public Font getPolice () {
return police;
}
/**
* Retourne le point central.
* @return a Point central du Texte.
* @see Point
*/
public Point getA () {
return new Point(a);
}
/**
* Retourne la largeur du texte.
* @return Largeur du texte.
*/
public int getLargeur () {
return largeur;
}
/**
* Retourne la largeur du texte.
* @return Largeur du texte.
*/
public int getHauteur () {
return hauteur;
}
/**
* Implémentation de la méthode getBoiteEnglobante() de la classe abstraite Dessin.<br />
* Elle retourne une BoiteEnglobante entourant le texte.
* @return BoiteEnglobante BoiteEnglobante entourant le texte.
*/
public BoiteEnglobante getBoiteEnglobante() {
if(typeAlign==CENTRE){
Point bg = new Point(a.getX()-largeur/2,a.getY()-(hauteur/2));
Point hd = new Point(a.getX()+largeur/2,a.getY()+(hauteur/2));
return new BoiteEnglobante(bg,hd);
}
Point bg = new Point(a.getX(),a.getY()-(hauteur/2));
Point hd = new Point(a.getX()+largeur,a.getY()+(hauteur/2));
return new BoiteEnglobante(bg,hd);
}
/**
* Implémentation de la méthode translater() de la classe abstraite Dessin.<br />
* @param dx pas de translation suivant X
* @param dy pas de translation suivant Y
*/
public void translater ( int dx, int dy ){
a.translater(dx,dy);
}
// Setter //
/**
* Permet d'attribuer une nouvelle chaîne de caractères à afficher.
* @param texte Chaîne de caractères
*/
public void setTexte ( String texte ) {
this.texte = new String(texte);
}
/**
* Permet d'attribuer une nouvelle police au texte.
* @param police Nouvelle police.
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/awt/Font.html" target="_blank">Font</a>
*/
public void setPolice ( Font police ) {
this.police = police;
}
/**
* Permet d'attribuer un nouveau point d'affichage.
* @param a Nouveau point central du texte.
* @see Point
*/
public void setA ( Point a ) {
this.a = new Point(a);
}
// Méthodes //
/**
* Implémentation de la méthode afficher() de la classe abstraite Dessin.<br />
* Elle permet d'afficher un Texte sur la zone d'affichage.
* <br /><br />
* On récupère d'abord la couleur et la police. Une fois la police récupérée, on crée un FontMetrics afin d'avoir la dimension en largeur du texte ce qui nous permets d'afficher correctement le Texte avec le Point central donné par l'utilisateur.
* @param g Graphics.
*/
public void afficher ( Graphics g ) {
g.setColor ( getCouleur() );
g.setFont ( police );
FontMetrics metr = g.getFontMetrics ( police );
int ox=0;
//Pourquoi ? je ne sais plus...
int oy = (int)g.getClipBounds().getHeight()-a.getY()+(metr.getHeight()/4) ;;
if(typeAlign==GAUCHE)
ox = a.getX();
else
ox = a.getX() - ( metr.stringWidth ( texte ) / 2 );
g.drawString ( texte, ox, oy );
}
/**
* Méthode toString retournant un description du texte.<br />
* La chaine de caractères retournée est de la forme "Message m centrée en (x,y)".
* @return Une chaîne de caractères décrivant le texte.
*/
public String toString(){
if(typeAlign==CENTRE)
return new String("Message \""+texte+"\" centré en "+getA());
else
return new String("Message \""+texte+"\" commencant en "+getA());
}
/**
* Méthode equals permettant de tester l'égalité entre un texte et un objet passé en paramètre.
* @return Vrai si l'objet passé en paramètre est un texte dont les caractéristiques sont les mêmes que le texte sur lequel la méthode est appelée.
*/
public boolean equals(Object obj){
if (obj==this) {
return true;
}
// Vérification du type du paramètre
if (obj instanceof Texte) {
// Vérification des valeurs des attributs
Texte other = (Texte) obj;
return super.equals(other) && a.equals(other.a) && texte.equals(other.texte) && police.equals(other.police);
}
return false;
}
}