Comment ajouter une image à l'interface java?
Fermé
marwen4ever
Messages postés
26
Date d'inscription
lundi 4 juillet 2011
Statut
Membre
Dernière intervention
2 octobre 2013
-
5 juil. 2011 à 13:08
demo - 15 août 2011 à 16:32
demo - 15 août 2011 à 16:32
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.
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.
A voir également:
- Comment ajouter une image à l'interface java?
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel football - Télécharger - Jeux vidéo
- Java apk - Télécharger - Langages
- Image iso - Guide
- Comment agrandir une image - Guide
5 réponses
KX
Messages postés
16760
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
12 février 2025
3 020
15 août 2011 à 14:41
15 août 2011 à 14:41
Voici une petite classe perso pour la manipulation simple d'image qui devrait répondre au problème de marwen4ever et amel.ammoula.
Exemple d'utilisation :
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(); } }
KX
Messages postés
16760
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
12 février 2025
3 020
Modifié par KX le 5/07/2011 à 13:31
Modifié par KX le 5/07/2011 à 13:31
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
Tu veux insérer ton image où dans tout ça ?
La confiance n'exclut pas le contrôle
marwen4ever
Messages postés
26
Date d'inscription
lundi 4 juillet 2011
Statut
Membre
Dernière intervention
2 octobre 2013
5 juil. 2011 à 13:56
5 juil. 2011 à 13:56
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
http://www.siteduzero.com/forum-83-271834-p1-inserer-une-image-en-fond-dans-un-programme.html
regarde sur ce lien ;)
regarde sur ce lien ;)
amel.ammoula
Messages postés
6
Date d'inscription
mercredi 10 août 2011
Statut
Membre
Dernière intervention
18 avril 2012
15 août 2011 à 10:21
15 août 2011 à 10:21
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();
}
}
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();
}
}
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question