| 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
 
 |  
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
public class ImagePanel extends JPanel
{
    private BufferedImage image;
    private int w;  	// largeur de l'image
    private int h;  	// hauteur de l'image
/**
* Le constructeur :
* @param filePath le fichier de l'image.
* @param x coordonnée x du coin supérieur gauche.
* @param y coordonnée y du coin supérieur gauche.
* @param width largeur de l'image.
* @param height hauteur de l'image.
*/
    public ImagePanel(String filePath, int x, int y, int width, int height)
    {
        super(null, true);
		File fileImg = new File(filePath);
        try {
            image = ImageIO.read(fileImg);
        } catch (IOException e) {
            image = null;
            System.err.println("Fichier invalide");
        }
 
		if( width<=0 && height<=0){
			w = image.getWidth(this);
			h = image.getHeight(this);
		} else {
        	w = width;
        	h = height;
		}
		setBounds(x, y, w, h);
		//setOpaque(false);
		setBackground(Color.red );
        setVisible(true);
    }
 
	/**
        * ImagePanel au dimensions de l'image originale
        * @param filePath Chemin l'image.
        * @param x coordonnée x du coin supérieur gauche.
        * @param y coordonnée y du coin supérieur gauche.
        */
	public ImagePanel(String filePath, int x, int y)
	{
		this( filePath, x, y, 0, 0);
	}
 
	/**
         * Applique une rotation de <code>scale</code> radians à la propriété <code>image</code>
         * @param scale Angle de rotation (max: 2*pi)
         */
	public void rotate(double scale)
	{
		double sinA = Math.sin(scale);
 
		AffineTransform tx = new AffineTransform();
		tx.rotate(scale, this.image.getWidth() / 2, this.image.getHeight() / 2);
		AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
 
		Rectangle rect = op.getBounds2D(this.image).getBounds();
		tx.translate(sinA * rect.getX(), -sinA * rect.getY());
		op = new AffineTransformOp(tx,AffineTransformOp.TYPE_BILINEAR);
 
		this.image = op.filter(this.image, op.createCompatibleDestImage(this.image, null) );
		this.setBounds( new Rectangle(this.image.getWidth(), this.image.getHeight()) );
		this.w = this.image.getWidth();
		this.w = this.image.getWidth();
		this.h = this.image.getHeight();
	}
 
/**
* Gere l'affichage graphique du JPanel, ainsi que le refraichissement.
*/
    public void paint(Graphics g)
    {
		g.drawImage(image, 0, 0, w, h, null);
		g.drawRect(0,0,w+1,h+1);
    }
} | 
Partager