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
   |  
public class AfficheurImage extends JPanel
{
    protected BufferedImage image = null, image2 = null, image3 = null;
    private int width, height;
    private double imageHeight, imageHeightPosition, imageWidthPostion;
 
    public AfficheurImage (int width, int height)
    {
        this.width = width; this.height = height;
        setBackground(Color.white);
    }
    public void setImage(String imagePath)
    {   
        try
        {
          File a = new File(imagePath);
            image = ImageIO.read(a);
            image = createCompatibleImage(image);
            image2 = ImageIO.read(a);
            image2 = createCompatibleImage(image2);
            double i;
            for (i = 0.45 ; image.getWidth() > width  ; i -= 0.01)
            {
                System.out.println(i);
                image = scale(image2, i);
            }
            image3 = scale(image2, i);
 
            imageHeight = image3.getHeight() * (width / image3.getWidth());
            imageHeightPosition = (height - imageHeight) / 2;
            imageWidthPostion = (width - image3.getWidth()) / 2;
        }
        catch (IOException ex) {  Logger.getLogger(AfficheurImage.class.getName()).log(Level.SEVERE, null, ex);   }
    }
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image3, (int) imageWidthPostion, (int) imageHeightPosition, null);
    }
    public void stopImage()
    {
        image3 = null;
    }
    /** Effectue une homothétie de l'image.
    *
    * @param bi l'image.
    * @param scaleValue la valeur de l'homothétie.
    * @return une image réduite ou agrandie.
    */
    public static BufferedImage scale(BufferedImage bi, double scaleValue)
    {
        AffineTransform tx = new AffineTransform();
        tx.scale(scaleValue, scaleValue);
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        BufferedImage biNew = new BufferedImage( (int) (bi.getWidth() * scaleValue),
                (int) (bi.getHeight() * scaleValue), bi.getType());
        return op.filter(bi, biNew);
    }
    /**
    * Create a compatible image from an existing image.
    *
    * @param image The image to make compatible.
    * @return A compatible image filled with the base image.
    */
    public static BufferedImage createCompatibleImage(BufferedImage image)
    {
        GraphicsConfiguration gc = GraphicsEnvironment.
                getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
 
        if (image.getColorModel().equals(gc.getColorModel()))
        {
            return image;
        }
        else
        {
            BufferedImage compatibleImage = gc.createCompatibleImage(image.getWidth(), image.getHeight(),
                    image.getTransparency());
 
            Graphics g = compatibleImage.getGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();
 
            return compatibleImage;
        }
    }
} | 
Partager