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
   |  
package display;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.lang.IllegalArgumentException;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
 
/** Fenêtre pour l'affichage d'objets de type Affichable et l'interaction souris avec ces objets. L'affichage peut se faire sur un fond coloré ou sur une image. */
public class GUIEpidemie extends JFrame implements MouseListener{
 
	private int offset = 12;
	private Graphic p;
	private Color fond = null;
	private BufferedImage img = null;
	private ArrayList<Affichable> elements;
	private ArrayList<MouseEvent> clics;
	private JTextField jtf;
 
	/** Crée une JFrame permettant d'afficher sur la couleur de fond spécifiée des Affichable ayant des coordonnées dans [0..largeur,0..hauteur]. */
	public GUIEpidemie(String titre, int largeur, int hauteur, Color fond){
		super(titre);
		this.elements = new ArrayList<Affichable>();
		this.clics = new ArrayList<MouseEvent>();
		this.fond = fond;
		this.setLayout(new BorderLayout());
		this.p = new Graphic();
		this.p.addMouseListener(this);
		this.getContentPane().add(this.p,BorderLayout.CENTER);
		jtf = new JTextField(20);
		this.getContentPane().add(jtf,BorderLayout.SOUTH);
		this.setSize(largeur,hauteur);
		this.setResizable(false);
		this.setLocation((java.awt.Toolkit.getDefaultToolkit().getScreenSize().width-this.getWidth())/2,(java.awt.Toolkit.getDefaultToolkit().getScreenSize().height-this.getHeight())/2);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
 
	/** Crée une JFrame permettant d'afficher des Affichable sur l'image de fond spécifiée. 
          * @throws IOException si le fichier image spécifié n'existe pas ou n'est pas une image lisible. */
	public GUIEpidemie(String titre, String fichierImage) throws IOException{
		super(titre);
		this.elements = new ArrayList<Affichable>();
		this.clics = new ArrayList<MouseEvent>();
		this.fond = fond;
		this.img = ImageIO.read(new File(fichierImage));
		this.setLayout(new BorderLayout());
		this.p = new Graphic();
		this.p.addMouseListener(this);
		this.getContentPane().add(this.p,BorderLayout.CENTER);
		jtf = new JTextField(20);
		this.getContentPane().add(jtf,BorderLayout.SOUTH);
		this.setSize(img.getWidth(),img.getHeight());
		this.setResizable(false);
		this.setLocation((java.awt.Toolkit.getDefaultToolkit().getScreenSize().width-this.getWidth())/2,(java.awt.Toolkit.getDefaultToolkit().getScreenSize().height-this.getHeight())/2);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
 
	/** Ajoute un Affichable à l'interface et rafraichit l'affichage. */
	public void addAffichable(Affichable a){
		this.elements.add(a);
		this.p.repaint();
	}
 
	/** Retire un Affichable de l'interface et rafraichit l'affichage. */
	public boolean removeAffichable(Affichable a){
		if(this.elements.remove(a)){
			this.p.repaint();
			return true;
		}
		else return false;
	}
 
	public void mouseClicked(MouseEvent e){
		this.clics.add(e);
	}
 
	public void mouseEntered(MouseEvent e){}
 
	public void mouseExited(MouseEvent e){}
 
	public void mousePressed(MouseEvent e){}
 
	public void mouseReleased(MouseEvent e){}
 
	/** Retourne un MouseEvent correspondant au dernier clic de l'utilisateur, ou null s'il n'y a plus de clic à renvoyer. */
	public MouseEvent getClic(){
		if(this.clics.isEmpty()) return null;
		else return this.clics.remove(0);
	}
 
	private class Graphic extends JPanel{
		public void paint(Graphics gr){
			if(GUIEpidemie.this.fond != null){
				gr.setColor(GUIEpidemie.this.fond);
				gr.fillRect(0,0,this.getWidth(),this.getHeight());
			}
			if(GUIEpidemie.this.img != null){
				gr.drawImage(GUIEpidemie.this.img,0,0,this);
			}
			for(Affichable a:GUIEpidemie.this.elements){
				gr.setColor(a.getColor());
				((Graphics2D) gr).fill(a.getShape());
				gr.setColor(Color.BLACK);
				gr.setFont(gr.getFont().deriveFont(Font.BOLD));
				Point p = a.getStringPosition();
				String[] s = a.getString().split("\n");
				for(int i=0;i<s.length;i++) gr.drawString(s[i],p.x,p.y+(i+1)*GUIEpidemie.this.offset);
			}
		}
	}
 
	/** Affiche le message m dans une boite de dialogue. */
	public void displayMessage(String m){
		JOptionPane.showMessageDialog(this,m);
	}
 
	/** Affiche le texte s dans la zone de texte en bas de la fenêtre. */
	public void setBottomFieldText(String s){
		this.jtf.setText(s);
	}	
 
} | 
Partager