Salut à tous ,

J'ai crée une classe qui permet d'ajouter une image dans une JFrame , mais là , je veux faire des translations ( gauche , droite , haut , bas ) , sur mon image mais je bloque , habituer a le faire sur visual basic , mais avec java , je suis un peu perdu .

Voila ma class qui me permet d'ajouter une image ( si vous avez une autre solution je suis preneur )

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
 
 
public class JImagePanel extends JPanel {
 
    private Image image = null;
    private Boolean stretch = true;
 
 
    public JImagePanel(Image image) {
        this.image = image;
    }
 
 
    public JImagePanel(String file) {
        this.image = getToolkit().getImage(file);
    }
 
 
    public void setStretch(Boolean stretch) {
        this.stretch = stretch;
    }
 
    /**
     * Surcharger le dessin du composant
     * @param g canvas
     */
    protected void paintComponent(Graphics g) {
        int x = 0;
        int y = 0;
        int width = 0;
        int height = 0;
 
        if (this.stretch) {
            width = this.getWidth();
            height = this.getHeight();
        } else {
            width = this.image.getWidth(this);
            height = this.image.getHeight(this);
            x=((this.getWidth()-width)/2);
            y=((this.getHeight()-height)/2);
        }  
        g.drawImage(this.image, x, y, width, height, this);
    }
et pour la testé :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 public static void main(String[] args) {
        JImagePanel imagePanel = new JImagePanel("c:\\VOTREIMAGE.EXTENSION");
        //Centrer l'image
        imagePanel.setStretch(false);
 
 
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(imagePanel);
        frame.pack();
        frame.setLocation(200, 200);
        frame.setVisible(true);
    }
}

Je vous remercie ! .