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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Toolkit;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import fr.nico.ptiCerfal.controleur.ZoneDessin;
import fr.nico.ptiCerfal.model.*;
public class FenetrePrincipale extends JFrame { // DEBUT
private static final long serialVersionUID = 6198246847581812181L;
// **************************************
// LE MENU
// **************************************
public String Texte;
// //////////////////Barre de Menu ////////////////////
private JMenuBar menuBar = new JMenuBar(); // Menu Barre
// ///////////////////////////////////////////////////
// ////////////////////////////////// Menu
// ///////////////////////////////////////////
JMenu fichier = new JMenu("Fichier"), // Fichier du Menu
edition = new JMenu("Edition"), // Edition du Menu
forme = new JMenu("Forme du pointeur"), // Forme du Menu
couleur = new JMenu("Couleur du pointeur"), // Couleur du pointeur
// dans le Menu
fond = new JMenu("Couleur de fond"), // Fond du Panel dans le Menu
aide = new JMenu("Aide"); // Aide
// ///////////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////// Label
// //////////////////////////////////
JLabel formetool = new JLabel(" Forme: "); // Label pour les formes
JLabel tailletool = new JLabel(" Taille: "); // Label pour la taille du
// pointeur
JLabel couleurtool = new JLabel("Couleur: "); // Label Pour la couleur du
// point
JLabel decortool = new JLabel(" Décor: "); // Label pour les Décors
// //////////////////////////////////////////////////////////////////////////////
// /////////////////////////////// Composants du Menu
// ////////////////////////////////////
JMenuItem nouveau = new JMenuItem("Effacer"), // Item Effacer du Menu
quitter = new JMenuItem("Quitter"), // Item Quitter du Menu
onglet = new JMenuItem("Nouvel onglet"), // Item Onglet du Menu
crayon = new JMenuItem("Crayon"), // Item Forme ronde du Menu
pinceau = new JMenuItem("Pinceau"), // Item Forme Carré du Menu
trait = new JMenuItem("Trait"), // Item Tirer un Trait du Menu
bleu = new JMenuItem("Bleu"), // Item Pointeur Bleu du Menu
rouge = new JMenuItem("Rouge"), // Item Pointeur Rouge du Menu
vert = new JMenuItem("Vert"), // Item Pointeur Vert du Menu
bleufond = new JMenuItem("Bleu"), // Item Fond Bleu du Menu
rougefond = new JMenuItem("Rouge"), // Item Fond Rouge du Menu
whitefond = new JMenuItem("Blanc"), // Item Fond Blanc du Menu
multifond = new JMenuItem("Choix de la couleur"), // Item Choix de
// la Couleur
createur = new JMenuItem("?");
// //////////////////////////////////////////////////////////////////////////////////////
// ////////////////// Zone de texte ////////////////////
JTextArea texteZone;
// ////////////////////////////////////////////////////
// ****************************************
// LA BARRE D'OUTILS
// ****************************************
// //////////////////Barre d'outils ////////////////////
JToolBar toolBar = new JToolBar(); // Barre d'outils
// ////////////////////////////////////////////////////
// //////////////////////////////////// Boutons du menu
// ////////////////////////////////////
JButton butCrayon = new JButton(new ImageIcon("images/crayon.png")), // Bouton
// Forme
// Ronde
butPinceau = new JButton(new ImageIcon("images/pinceau.png")), // Bouton
// Forme
// Carré
butRectangle = new JButton(new ImageIcon("images/rectangle.png")),// Rectangle
line = new JButton(new ImageIcon("images/trait.png")), // Bouton
// Tirer un
// Trait
texte = new JButton(new ImageIcon("images/texte.png")), // Bouton
// Ajouter
// du Texte
gomme = new JButton(new ImageIcon("images/gommetool.png")), // Bouton
// Gommer
// Couleur pointeur
red = new JButton(new ImageIcon("images/rouge.jpg")), // Bouton
// Pointeur
// Rouge
green = new JButton(new ImageIcon("images/vert.jpg")), // Bouton
// Pointeur
// Vert
blue = new JButton(new ImageIcon("images/bleu.jpg")), // Bouton
// Pointeur
// Bleu
yellow = new JButton(new ImageIcon("images/jaune.jpg")), // Bouton
// Pointeur
// Jaune
black = new JButton(new ImageIcon("images/noir.jpg")), // Bouton
// Pointeur
// Noir
white = new JButton(new ImageIcon("images/blanc.jpg")), // Bouton
// Pointeur
// Blanc
multi = new JButton(new ImageIcon("images/multi.png")), // Bouton
// Choix
// Couleur
// Taille pointeur
un = new JButton(new ImageIcon("images/trait1.png")), // Bouton
// Taille 1
deux = new JButton(new ImageIcon("images/trait2.png")), // Bouton
// Taille 2
trois = new JButton(new ImageIcon("images/trait3.png")), // Bouton
// Taille
// 3
quatre = new JButton(new ImageIcon("images/trait4.png")), // Bouton
// Taille
// 4
cinq = new JButton(new ImageIcon("images/trait5.png")), // Bouton
// Taille 5
six = new JButton(new ImageIcon("images/trait6.png")), // Bouton
// Taille 6
sept = new JButton(new ImageIcon("images/trait7.png")), // Bouton
// Taille 7
// Décors
arbre = new JButton(new ImageIcon("images/arbretool.png")), // Bouton
// Décor
// Arbre
nuage = new JButton(new ImageIcon("images/nuagetool.png")), // Bouton
// Décor
// Nuage
soleil = new JButton(new ImageIcon("images/soleiltool.png")); // Bouton
// Décor
// Soleil
// ///////////////////////////////////////////////////////////////////////////////////
// ***************************************
// LES ÉCOUTEURS
// ***************************************
// //////////////////// Déclaration des Écouteurs /////////////////////
private FormeListener fListener = new FormeListener(); // Forme
private CouleurListener cListener = new CouleurListener(); // Couleur
private FondListener foListener = new FondListener(); // Fond
private TailleListener tailListener = new TailleListener(); // Taille
private DecorListener dListener = new DecorListener(); // Décor
// /////////////////////////////////////////////////////////////////////
// ////////////////////////// Déclaration de la zone de dessin
// ////////////////
private ZoneDessin zoneDeDessin = new ZoneDessin(); // Zone de Dessin
// ////////////////////////////////////////////////////////////////////
// ////////////////////////////// Constructeur /////////////////////////
public FenetrePrincipale() {
this.setIconImage(Toolkit.getDefaultToolkit().getImage(
"images/soleilicone.png")); // Icone
this.setTitle("PaintingWoods"); // Titre
this.setSize(900, 600); // Dimension
this.setLocationRelativeTo(null); // Position Centrale
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Fermeture
// On initialise le menu
this.initMenu();
// Idem pour la barre d'outils
this.initToolBar();
// On positionne notre zone de dessin
this.getContentPane().add(zoneDeDessin, BorderLayout.CENTER);
this.setVisible(true);
}
// ///////////////////////////////////////////////////////////////////////
/**
* Initialise le menu
*/
private void initMenu() {
// Menu Onglet
onglet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("Ajout d'un onglet");
}
});
onglet.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
KeyEvent.CTRL_DOWN_MASK));
onglet.setToolTipText("Ajouter un onglet");
// Menu Nouveau
nouveau.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
zoneDeDessin.erase();
}
});
nouveau.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
KeyEvent.CTRL_DOWN_MASK));
nouveau.setToolTipText("Effacer le dessin");
// Menu quitter
quitter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
quitter.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,
KeyEvent.ALT_DOWN_MASK));
quitter.setMnemonic(KeyEvent.VK_F4);
quitter.setToolTipText("Quittez l'application");
// Menu Aide
createur.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
APropos propos = new APropos();
propos.setVisible(true);
}
});
createur.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
KeyEvent.CTRL_DOWN_MASK));
createur.setToolTipText("Afficher le créateur");
// //////////////////AJOUT //////////////////////////
// Menu Fichier
fichier.add(onglet);
fichier.addSeparator();
fichier.add(nouveau);
fichier.addSeparator();
fichier.add(quitter);
fichier.setMnemonic('F');
// Menu Edition
edition.setMnemonic('E');
edition.add(forme);
edition.addSeparator();
edition.add(couleur);
edition.addSeparator();
edition.add(fond);
// Menu Aide
aide.add(createur);
// Forme Dans le Menu
crayon.addActionListener(fListener);
pinceau.addActionListener(fListener);
trait.addActionListener(fListener);
forme.add(crayon);
forme.add(pinceau);
forme.add(trait);
// Couleur Dans le Menu
rouge.addActionListener(cListener);
vert.addActionListener(cListener);
bleu.addActionListener(cListener);
couleur.add(rouge);
couleur.add(vert);
couleur.add(bleu);
// Couleur de Fond dans le Menu
bleufond.addActionListener(foListener);
rougefond.addActionListener(foListener);
whitefond.addActionListener(foListener);
multifond.addActionListener(foListener);
fond.add(bleufond);
fond.add(rougefond);
fond.add(whitefond);
fond.add(multifond);
// On Ajoute le Menu Fichier et Edition au Menu
menuBar.add(fichier);
menuBar.add(edition);
menuBar.add(aide);
// On Ajoute le Menu
this.setJMenuBar(menuBar);
}
// //////////////////////////////////////////////////////
// //////////////////////// Barre d'outils
// //////////////////////////////////
/**
* Initialise la barre d'outils
*/
private void initToolBar() {
// Forme Dans la Barre d'outils
butCrayon.addActionListener(fListener);
butPinceau.addActionListener(fListener);
butRectangle.addActionListener(fListener);
line.addActionListener(fListener);
texte.addActionListener(fListener);
toolBar.add(formetool);
toolBar.add(butCrayon);
toolBar.add(butPinceau);
toolBar.add(line);
toolBar.add(texte);
toolBar.add(butRectangle);
toolBar.add(gomme);
// Couleur pointeur dans la Barre d'outils
red.addActionListener(cListener);
green.addActionListener(cListener);
blue.addActionListener(cListener);
yellow.addActionListener(cListener);
black.addActionListener(cListener);
white.addActionListener(cListener);
multi.addActionListener(cListener);
gomme.addActionListener(fListener);// Autre Listener pour gommer
// InfoBulle
butCrayon.setToolTipText("Crayon");
butPinceau.setToolTipText("Pinceau");
butCrayon.setToolTipText("Rectangle");
line.setToolTipText("Tirer un trait");
texte.setToolTipText("Taper du texte");
red.setToolTipText("Couleur du pointeur rouge");
green.setToolTipText("Couleur du pointeur vert");
blue.setToolTipText("Couleur du pointeur bleu");
yellow.setToolTipText("Couleur du pointeur jaune");
black.setToolTipText("Couleur du pointeur noir");
white.setToolTipText("Couleur du pointeur blanc");
multi.setToolTipText("Définir une couleur personnalisée");
gomme.setToolTipText("Gomme");
arbre.setToolTipText("Ajouter un arbre");
nuage.setToolTipText("Ajouter un nuage");
soleil.setToolTipText("Ajouter un soleil");
// Taille du pointeur dans le Barre d'outils
un.addActionListener(tailListener);
deux.addActionListener(tailListener);
trois.addActionListener(tailListener);
quatre.addActionListener(tailListener);
cinq.addActionListener(tailListener);
six.addActionListener(tailListener);
sept.addActionListener(tailListener);
toolBar.addSeparator();
toolBar.add(tailletool);
toolBar.add(un);
toolBar.add(deux);
toolBar.add(trois);
toolBar.add(quatre);
toolBar.add(cinq);
toolBar.add(six);
toolBar.add(sept);
// Couleur du pointeur dans la Barre d'outils
toolBar.addSeparator();
toolBar.add(couleurtool);
toolBar.add(white);
toolBar.add(black);
toolBar.add(red);
toolBar.add(blue);
toolBar.add(green);
toolBar.add(yellow);
toolBar.add(multi);
// Décor dans la Barre d'outils
arbre.addActionListener(dListener);
nuage.addActionListener(dListener);
soleil.addActionListener(dListener);
toolBar.addSeparator();
toolBar.add(decortool);
toolBar.add(arbre);
toolBar.add(nuage);
toolBar.add(soleil);
// On ajoute la Barre d'outils
this.getContentPane().add(toolBar, BorderLayout.NORTH);
}
// ////////////////////////////////////////////////////////////////////
// //////////////////////////////// Action des Écouteurs
// ÉCOUTEUR POUR LE CHANGEMENT DE FORME
// ************************************
class FormeListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource().getClass().getName()
.equals("javax.swing.JButton")) {
System.out.println("###### Changement de forme ##### !");
if (e.getSource() == butCrayon) { // Test du bouton Crayon
System.out.println("Crayon");
} else if (e.getSource() == butPinceau) { // Test du bouton Pinceau
System.out.println("Pinceau");
zoneDeDessin.setPointerType("CIRCLE");
} else if (e.getSource() == line) { // Test du bouton Trait
System.out.println("Tirage de trait");
} else if (e.getSource() == texte) { // Test du bouton Rond
System.out.println("Ajout zone de texte");
String unTexte = JOptionPane
.showInputDialog("Entrez votre texte et cliquez pour l'afficher");
} else if (e.getSource() == butRectangle) { // Test du bouton
// Rectangle
System.out.println("Traçage de Rectangle");
} else if (e.getSource() == gomme) { // Test de la gomme
System.out.println("Gomme Activée");
}
}
// Pour le MenuItem
else if (e.getSource().getClass().getName().equals(
"javax.swing.JMenuItem")) {
System.out.println("###### Changement de forme Item ##### !");
if (e.getSource() == crayon) { // Test du bouton Carré
System.out.println("Crayon");
zoneDeDessin.setPointerType("SQUARE");
} else if (e.getSource() == pinceau) { // Test du bouton Rond
System.out.println("¨Pinceau");
zoneDeDessin.setPointerType("CIRCLE");
} else if (e.getSource() == trait) { // Test du bouton Rond
System.out.println("Tirage de trait");
}
} else {
System.out.println("Pas de forme choisie!!");
}
}
}
// ÉCOUTEUR POUR LE CHANGEMENT DE COULEUR
// **************************************
class CouleurListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource().getClass().getName());
if (e.getSource().getClass().getName()
.equals("javax.swing.JButton")) {
System.out
.println("##### Changement de couleur de pointeur #####");
if (e.getSource() == red) { // Test du bouton Couleur rouge
System.out.println("Couleur rouge");
zoneDeDessin.setPointerColor(Color.RED);
} else if (e.getSource() == green) { // Test du bouton Couleur
// verte
System.out.println("Couleur verte");
zoneDeDessin.setPointerColor(Color.GREEN);
} else if (e.getSource() == blue) { // Test du bouton Couleur
// bleue
System.out.println("Couleur bleue");
zoneDeDessin.setPointerColor(Color.BLUE);
} else if (e.getSource() == yellow) { // Test du bouton Couleur
// jaune
System.out.println("Couleur jaune");
zoneDeDessin.setPointerColor(Color.YELLOW);
} else if (e.getSource() == black) { // Test du bouton Couleur
// noire
System.out.println("Couleur noire");
zoneDeDessin.setPointerColor(Color.BLACK);
} else if (e.getSource() == white) { // Test du bouton Couleur
// blanche
System.out.println("Couleur blanche");
zoneDeDessin.setPointerColor(Color.WHITE);
} else if (e.getSource() == multi) { // Test du bouton Multi
Color coul = JColorChooser.showDialog(null,
"Couleur de fond", zoneDeDessin.getPointerColor());
zoneDeDessin.setPointerColor(coul);
System.out.println("Choix de la couleur " + coul);
}
}
// Pour le MenuItem
else if (e.getSource().getClass().getName().equals(
"javax.swing.JMenuItem")) {
System.out
.println("###### Changement de couleur de pointeur Item #####");
if (e.getSource() == bleu) { // Test du Item bleu
System.out.println("Couleur bleue");
zoneDeDessin.setPointerColor(Color.BLUE);
} else if (e.getSource() == rouge) { // Test du Item rouge
System.out.println("Couleur rouge");
zoneDeDessin.setPointerColor(Color.RED);
} else if (e.getSource() == vert) { // Test du Item vert
System.out.println("Couleur verte");
zoneDeDessin.setPointerColor(Color.GREEN);
}
} else {
System.out.println("Pas de couleur choisie!");
}
}
}
// ÉCOUTEUR POUR LE CHANGEMENT DE TAILLE
// ************************************
class TailleListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource().getClass().getName());
if (e.getSource().getClass().getName()
.equals("javax.swing.JButton")) {
System.out
.println("##### Changement de taille du pointeur #####");
if (e.getSource() == un) { // Test du bouton taille 1
System.out.println("Taille 1");
zoneDeDessin.setPointerSize(2);
} else if (e.getSource() == deux) { // Test du bouton taille 2
System.out.println("Taille 2");
zoneDeDessin.setPointerSize(4);
} else if (e.getSource() == trois) { // Test du bouton taille 3
System.out.println("Taille 3");
zoneDeDessin.setPointerSize(6);
} else if (e.getSource() == quatre) { // Test du bouton taille 4
System.out.println("Taille 4");
zoneDeDessin.setPointerSize(8);
} else if (e.getSource() == cinq) {
System.out.println("Taille 5");
zoneDeDessin.setPointerSize(10);
} else if (e.getSource() == six) {
System.out.println("Taille 6");
zoneDeDessin.setPointerSize(12);
} else if (e.getSource() == sept) {
System.out.println("Taille 7");
zoneDeDessin.setPointerSize(14);
}
} else {
System.out.println("Pas de taille choisie!");
}
}
}
// ÉCOUTEUR POUR LE CHANGEMENT DE COULEUR DE FOND
// ************************************
class FondListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource().getClass().getName());
if (e.getSource().getClass().getName().equals(
"javax.swing.JMenuItem")) {
System.out.println("##### Changement couleur de fond #####");
if (e.getSource() == bleufond) { // Test du MenuItem fond bleu
System.out.println("Couleur de fond bleue");
zoneDeDessin.setCouleurDeFond(Color.BLUE);
} else if (e.getSource() == rougefond) { // Test du MenuItem
// fond rouge
// taille 2
System.out.println("Couleur de fond rouge");
zoneDeDessin.setCouleurDeFond(Color.RED);
} else if (e.getSource() == whitefond) { // Test du MenuItem
// fond blanc
System.out.println("Couleur de fond blanche");
zoneDeDessin.setCouleurDeFond(Color.WHITE);
} else if (e.getSource() == multifond) { // Test du bouton
// MultiCouleur
Color coul = JColorChooser.showDialog(null,
"Couleur de fond", zoneDeDessin.getCouleurDeFond());
zoneDeDessin.setCouleurDeFond(coul);
System.out.println("Choix de la couleur " + coul);
}
} else {
System.out.println("Pas de couleur de fond choisie!");
}
}
}
// ÉCOUTEUR POUR L'AJOUT DE DECOR
// ************************************
class DecorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource().getClass().getName());
if (e.getSource().getClass().getName()
.equals("javax.swing.JButton")) {
System.out.println("##### AJout d'un décor #####");
if (e.getSource() == arbre) { // Test du bouton Arbre
System.out.println("Décor arbre");
} else if (e.getSource() == nuage) { // Test du bouton Nuage
System.out.println("Décor nuage");
} else if (e.getSource() == soleil) { // Test du bouton Soleil
System.out.println("Décor soleil");
}
} else {
System.out.println("Pas de Décor choisi!");
}
}
}
// ///////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
@SuppressWarnings("unused")
FenetrePrincipale fen = new FenetrePrincipale();
}
} // FIN |
Partager