Comment ajouter une image à l'interface java?

Bonjour,

j'ai un problème au niveau de l'insertion d'une image dans une interface java dans un projet que je suis entrain de le faire.

5 réponses

  1. Modérateur
    Voici une petite classe perso pour la manipulation simple d'image qui devrait répondre au problème de marwen4ever et amel.ammoula.

    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JComponent;
    
    /**
     * Un composant représentant une image.
     */
    public class Dessin extends JComponent
    {
    	private static final long serialVersionUID = 1;
    	
    	protected final Image img;
    	
    	/**
    	 * Construit un nouveau composant représentant une image.
    	 * @param image l'image à représenter
    	 */
    	public Dessin(Image image)
    	{
    		img=image;
    		setSize(img.getWidth(this),img.getHeight(this));
    	}
    	
    	/**
    	 * Construit un nouveau composant représentant une image.
    	 * @param file le fichier contenant l'image
    	 * @throws IOException si une erreur intervient pendant la lecture
    	 */
    	public Dessin(File file) throws IOException
    	{
    		this(ImageIO.read(file));
    	}
    	
    	/**
    	 * Construit un nouveau composant représentant une image.
    	 * @param fileName le nom du fichier contenant l'image
    	 * @throws IOException si une erreur intervient pendant la lecture
    	 */
    	public Dessin(String fileName) throws IOException
    	{
    		this(new File(fileName));
    	}
    	
    	/**
    	 * @return l'image représenté par le compasant
    	 */
    	public Image getImage()
    	{
    		return img;
    	}
    	
    	@Override
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		g.drawImage(img,0,0,this);
    	}
    }

    Exemple d'utilisation :

    public class Test extends JFrame
    {
    	private static final long serialVersionUID = 1;
    	
    	public Test()
    	{
    		setLayout(null);
    		setSize(500,500);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		try
    		{
    			Dessin d = new Dessin("mon_image.png");
    			d.setLocation(220,220);
    			add(d);
    		}
    		catch (IOException e)
    		{
    			System.err.println(e);
    		}
    		
    		setVisible(true);
    	}
    	
    	public static void main (String...args)
    	{
    		new Test();
    	}
    }
    1. Modérateur
      Qu'est-ce que c'est ton "interface" ? Comment l'as tu faite (awt, swing, applet, autre...) ?
      Tu veux insérer ton image où dans tout ça ?
      La confiance n'exclut pas le contrôle
      1. c'est au niveau de l'interface graphique du java et j'aime placer cette image en haut de cette interface. je travaille avec la bibliothèque swing
        1. http://www.siteduzero.com/forum-83-271834-p1-inserer-une-image-en-fond-dans-un-programme.html
          regarde sur ce lien ;)
          1. ce si permet d'ajouter une image au fond de l'interface moi je veux ajouté une image en haut a coté j'ais besoin d'une solution :'( est voila mon interface

            import java.awt.*;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;
            import java.awt.Image;
            import javax.swing.*;
            import java.awt.Graphics;

            public class Bienvenu extends JFrame implements ActionListener {
            JLabel l1;
            JButton b1,b2,b3,b4;

            public Bienvenu(){
            super("Tunisiana");
            setLayout(null);
            setSize(500,350);
            setBackground(Color.WHITE);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            l1 =new JLabel("MANIPULATION DES MESSAGES");
            add(l1);
            l1.setFont(new java.awt.Font("Monotype Corsiva",Font.BOLD,18));
            l1.setBounds(10, 10, 300, 100);
            l1.setForeground(Color.BLUE);
            l1.setLocation(80,2);

            b1=new JButton ("1111");
            add(b1);
            b1.setLocation(50,50);
            b1.setBounds(15,80,100,30);
            //b1.setBackground(Color.red);
            b1.setForeground(Color.BLUE);
            b1.setFont(new java.awt.Font("Monotype Corsiva",Font.BOLD,20));

            b2=new JButton ("1110");
            add(b2);
            b2.setLocation(40,40);
            b2.setBounds(15,150,100,30);
            //b2.setBackground(Color.red);
            b2.setForeground(Color.BLUE);
            b2.setFont(new java.awt.Font("Monotype Corsiva",Font.BOLD,20));

            b3=new JButton ("Vider repertoire");
            add(b3);
            b3.setLocation(30,30);
            b3.setBounds(15,250,200,30);
            //b3.setBackground(Color.red);
            b3.setForeground(Color.BLUE);
            b3.setFont(new java.awt.Font("Monotype Corsiva",Font.BOLD,20));

            b4=new JButton ("Creer Repertoire");
            add (b4);
            b4.setLocation(30,30);
            b4.setBounds(250,250,200,30);
            //b4.setBackground(Color.red);
            b4.setForeground(Color.BLUE);
            b4.setFont(new java.awt.Font("Monotype Corsiva",Font.BOLD,20));

            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            b4.addActionListener(this);

            setVisible(true);
            }

            public void actionPerformed(ActionEvent e) {
            if (e.getSource()==(b1) )
            { TunisianaOffre t = new TunisianaOffre ();
            setVisible(false);
            dispose ();}

            if(e.getSource ()== (b2) )
            { TunisianaOffre t = new TunisianaOffre ();
            setVisible(false);
            dispose ();}
            if(e.getSource ()== (b3) )
            { ViderRepertoire t = new ViderRepertoire ();
            setVisible(false);
            dispose ();}

            if(e.getSource ()== (b4) )
            { CreerRepertoire c = new CreerRepertoire ();
            setVisible(false);
            dispose ();}
            }

            public static void main (String[] args ){
            Bienvenu t = new Bienvenu();

            }
            }
        2. Essais ce code de quelque ligne.
          http://sakoba.byethost13.com/2011/03/09/java-panel-with-background-image/