| 12
 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
 
 | /**
 * Projet : LAM
 * Paquetage : fr.statlife.LAM.IHM
 * Fichier : ActionRogner.java
 *
 * @author Mathilde Pellerin
 * @date 3 sept. 2010
 */
package fr.statlife.LAM.IHM;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
 
import javax.swing.AbstractAction;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
 
/**
 * 
 * @author Mathilde Pellerin
 */
@SuppressWarnings("serial")
public class ActionRogner extends AbstractAction implements PropertyChangeListener
{
	private FenetreLAM fenetre;
	private JPanel panneau;
	private PanneauMammo panneauApercu;
	private NumberFormat formatValeur;
	private BufferedImage mammo;
 
	private JFormattedTextField	fieldHaut;
	private JFormattedTextField	fieldBas;
	private JFormattedTextField	fieldGauche;
	private JFormattedTextField	fieldDroite;
 
	private int nbPixelHaut;
	private int nbPixelBas;
	private int nbPixelGauche;
	private int nbPixelDroite;
	private Graphics2D	g2d;
 
	public ActionRogner(final FenetreLAM fenetre, String texte)
	{
		super(texte);
 
		this.fenetre = fenetre;
 
		formatValeur = NumberFormat.getNumberInstance();
		formatValeur.setMaximumIntegerDigits(3);
		this.panneau = construirePanneau();
	}
 
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		//(Re)mise a zero des champs
		fieldHaut.setValue(0);
		fieldBas.setValue(0);
		fieldGauche.setValue(0);
		fieldDroite.setValue(0);
 
		mammo = fenetre.getPanneauMammoNative().getImg();
		panneauApercu.setImg(mammo);
 
		JOptionPane.showConfirmDialog(
			this.fenetre,
			new Object[]{
				this.panneau
			},
			FenetreLAM.messages.getString("boutonRogner"),
			JOptionPane.OK_CANCEL_OPTION,
			JOptionPane.PLAIN_MESSAGE
		);
	}
 
	private JPanel construirePanneau()
	{
		//Construction du panneau principal
		JPanel panneau = new JPanel(new GridLayout(1, 3));
		panneau.setPreferredSize(new Dimension(350, 150));
 
		//Construction du panneau qui contient le reglage des dimensions
		JPanel panneauDimension = new JPanel(new GridLayout(4, 2));
		JLabel labelHaut = new JLabel(FenetreLAM.messages.getString("labelRognageHaut"));
		JLabel labelBas = new JLabel(FenetreLAM.messages.getString("labelRognageBas"));
		JLabel labelGauche = new JLabel(FenetreLAM.messages.getString("labelRognageGauche"));
		JLabel labelDroite = new JLabel(FenetreLAM.messages.getString("labelRognageDroite"));
		fieldHaut = new JFormattedTextField(formatValeur);
		fieldHaut.addPropertyChangeListener("value", this);
 
		fieldBas = new JFormattedTextField(formatValeur);
		fieldBas.addPropertyChangeListener("value", this);
 
		fieldGauche = new JFormattedTextField(formatValeur);
		fieldGauche.addPropertyChangeListener("value", this);
 
		fieldDroite = new JFormattedTextField(formatValeur);
		fieldDroite.addPropertyChangeListener("value", this);
 
		panneauDimension.add(labelHaut);
		panneauDimension.add(fieldHaut);
		panneauDimension.add(new JLabel());
		panneauDimension.add(labelBas);
		panneauDimension.add(fieldBas);
		panneauDimension.add(new JLabel());
		panneauDimension.add(labelGauche);
		panneauDimension.add(fieldGauche);
		panneauDimension.add(new JLabel());
		panneauDimension.add(labelDroite);
		panneauDimension.add(fieldDroite);
		panneauDimension.add(new JLabel());
 
		//Construction du panneau qui contient l'apercu miniature
		panneauApercu = new PanneauMammo();
 
		panneau.add(panneauDimension);
		panneau.add(panneauApercu);
		return panneau;
	}
 
	/* (non-Javadoc)
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	@Override
	public void propertyChange(PropertyChangeEvent evt)
	{
		if(mammo != null)
		{
			Object source = evt.getSource();
 
			g2d = mammo.createGraphics(); // on récupère le contexte graphique de la BufferedImage   
	    	g2d.setColor( Color.red ); // on met l'état de couleur rouge à la BufferedImage
 
		    if (source == fieldHaut) 
		    {
		    	nbPixelHaut = ((Number)fieldHaut.getValue()).intValue();
 
		    	//on trace la ligne en haut qui delimite la zone a rogner
		    	g2d.fillRect(0, nbPixelHaut, mammo.getWidth(), 2);
		    	g2d.dispose(); //on libère la mémoire utilisée pour le contexte graphique
		    	panneauApercu.setImg(mammo);
		    }
		    else if(source == fieldBas) 
		    {
		    	nbPixelBas = ((Number)fieldBas.getValue()).intValue();
 
		    	//on trace la ligne en bas qui delimite la zone a rogner
		    	g2d.fillRect(0, mammo.getHeight() - nbPixelBas, mammo.getWidth(), 2);
		    	g2d.dispose(); //on libère la mémoire utilisée pour le contexte graphique
		    	panneauApercu.setImg(mammo);
		    }
		    else if(source == fieldGauche) 
		    {
		    	nbPixelGauche = ((Number)fieldGauche.getValue()).intValue();
 
		    	//on trace la ligne a gauche qui delimite la zone a rogner
		    	g2d.fillRect(nbPixelGauche, 0, 2, mammo.getHeight());
		    	g2d.dispose(); //on libère la mémoire utilisée pour le contexte graphique
		    	panneauApercu.setImg(mammo);
		    }
		    else if(source == fieldDroite) 
		    {
		    	nbPixelDroite = ((Number)fieldDroite.getValue()).intValue();
 
		    	//on trace la ligne a droite qui delimite la zone a rogner
		    	g2d.fillRect(mammo.getWidth() - nbPixelDroite, 0, 2, mammo.getHeight());
		    	g2d.dispose(); //on libère la mémoire utilisée pour le contexte graphique
		    	panneauApercu.setImg(mammo);
		    }
		}
	}
} | 
Partager