IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Agents de placement/Fenêtres Java Discussion :

charger une image avec Jframe


Sujet :

Agents de placement/Fenêtres Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Battosaiii
    Invité(e)
    Par défaut charger une image avec Jframe
    J'ai consulte l'API mais je ne trouve pas de methode pour charger une image avec Jframe un truc tout con. En revanche j'arrive tres bien avec Applet mais Jframe c'est mieux !!






    Car on donnera à celui qui a, mais à celui qui n’a pas on ôtera même ce qu’il a

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 904
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 904
    Billets dans le blog
    54
    Par défaut
    Je t'ai repondu plus tot dans ton autre post http://www.developpez.net/forums/viewtopic.php?t=429590
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Battosaiii
    Invité(e)
    Par défaut
    J;ai loade une image dans un Jframe :

    this.tempImage = this.getToolkit().getImage("portable.jpg");
    ensuite j'ai tenete :
    this.getContentPane().add("Center",tempImage);

    mais j'obtiens l'erreur :

    SwingImageButton.java:62: cannot find symbol
    symbol : method add(java.lang.String,java.awt.Image)
    location: class java.awt.Container
    this.getContentPane().add("Center",tempImage);
    ^

    J'ai essaye aussi :

    public void paintComponent(Graphics g)
    {
    g.drawImage( this.tempImage, 10, 10, this);
    }

    mais il n'affiche rien !!!

    C'ets la croix et la banniere pour afficher une image dans Jframe ...
    Quelqu'un connait il le moyen d'afficher une image avec JFrame ???

  4. #4
    Battosaiii
    Invité(e)
    Par défaut
    Comment charger une image dans Jframe ? existe il une methode simple pour le faire ?

  5. #5
    Battosaiii
    Invité(e)
    Par défaut
    pour charger une image j'ai utilise la methode paintComponent.

    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
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    private  Image tempImage;
     
    	public SwingImageButton() 
    	{
    	  // call the parent class constructor - sets the frame title
    	  super("Swing Button Example");
     
    	  this.tempImage = this.getToolkit().getImage("portable.jpg");
    	  JPanel p2=new JPanel();
    	  p2.add(this.tempImage);
    	  //ImageIcon icon = new ImageIcon(tempImage);
    	   //Image img = Toolkit.getDefaultToolkit().getImage(java.net.URLClassLoader.getSystemResource("portable.JPG"));
    //	  setIconImage( img );
    	  // controls panel
    	//this.theImage= new read(portable.JPG);
     
    	  JPanel controls = new JPanel(new FlowLayout());
    	  this.button = new JButton("Clear Text");
    	  this.button.addActionListener(this);	
    	  controls.add(this.button);
     
    	  editable = new JCheckBox("Read-Only");
    	  editable.addActionListener(this);
    	  controls.add(this.editable);
     
    	  controls.add(new JLabel("  Text Color:"));
    	  String[] items = {"Black", "Blue", "Green", "Red"};
    	  colorBox = new JComboBox(items);
    	  colorBox.addActionListener(this);
    	  controls.add(colorBox);
     
    	  // status TextField
    	  this.status = new JTextField();
    	  this.updateStatus("Application Started.");
     
    	  // main area, with scrolling and wrapping
     
    	  this.textArea = new JTextArea(20,40);
    	  JScrollPane p = new JScrollPane(this.textArea,
    			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    			JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    	  this.textArea.setWrapStyleWord(true);
     
    	  // default layout is border layout for frame/jframe
    	//this.getContentPane().add("Center",tempImage);
    	  this.getContentPane().add("North",controls);
    	  //this.getContentPane().add("South",this.status);
    	  this.getContentPane().add("Center",p);
    	  	  this.getContentPane().add("South",p2);
     
    	  this.pack();  //set the size of the frame according 
    			//to the component's preferred sizes
    	  this.show();	//make the frame visible
    	}
    	private void updateStatus(String theStatus)
    	{
    	  this.status.setText("Status: " + theStatus);
    	}
    	public void actionPerformed(ActionEvent e)
    	{
     
    	}
     
    	public void paintComponent(Graphics g)
    	{
    		g.drawImage( this.tempImage, 10, 10, this);
    	}

    malheureusement j'arrive pas a charger l'image je tente de l'afficher en la mettamt dans un Jpanel mais ca ne marche pas non plus

    SwingImageButton.java:29: cannot find symbol
    symbol : method add(java.awt.Image)
    location: class javax.swing.JPanel
    p2.add(this.tempImage);
    Je suis sur que afficher une image est tres simple mais je ne vois pqs ce qu'il faut faire .

  6. #6
    Battosaiii
    Invité(e)
    Par défaut
    ouf j'ai reussi ah charger une image dans Jframe mais bon sang C'est fastidieux et long :

    Voia cette methode marche Il doit y en avoir d'autres mais j'ai pas trouve plus simple ...


    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
    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
     
    private JButton button;
    	private JTextArea textArea;
    	private JTextField status;
    	private JCheckBox editable;
    	private JComboBox colorBox;
    	private boolean isTextEditable = true;
    	//private JComboBo
    	   private BufferedImage image; 
     
    	public SwingImageButton() 
    	{
    		  super("Swing Button Example");
     
     
    	/*Load an image */
    	String filename="portable.JPG";
    	  image = new BufferedImage(290, 470, BufferedImage.TYPE_INT_RGB);
        // create an image by reading in the PNG, GIF, or JPEG from a filename
            try { image = ImageIO.read(new File(filename)); }
            catch(IOException e) {
                e.printStackTrace();
                throw new RuntimeException("Could not open file: " + filename);
            }
            if (image == null)
                throw new RuntimeException("Invalid image file: " + filename);
    	ImageIcon icon = new ImageIcon(image);
    			JLabel j = new JLabel(icon);
    			this.setContentPane(j);
     
    	  // call the parent class constructor - sets the frame title
     
     
    	 // this.tempImage = this.getToolkit().getImage("portable.jpg");
    	  //JPanel p2=new JPanel();
    	  //p2.add(this.tempImage);
    	  //ImageIcon icon = new ImageIcon(tempImage);
    	   //Image img = Toolkit.getDefaultToolkit().getImage(java.net.URLClassLoader.getSystemResource("portable.JPG"));
    //	  setIconImage( img );
    	  // controls panel
    	//this.theImage= new read(portable.JPG);
     
    	  JPanel controls = new JPanel(new FlowLayout());
    	  this.button = new JButton("Clear Text");
    	  this.button.addActionListener(this);	
    	  controls.add(this.button);
     
    	  editable = new JCheckBox("Read-Only");
    	  editable.addActionListener(this);
    	  controls.add(this.editable);
     
    	  controls.add(new JLabel("  Text Color:"));
    	  String[] items = {"Black", "Blue", "Green", "Red"};
    	  colorBox = new JComboBox(items);
    	  colorBox.addActionListener(this);
    	  controls.add(colorBox);
     
    	  // status TextField
    	  this.status = new JTextField();
    	  this.updateStatus("Application Started.");
     
    	  // main area, with scrolling and wrapping
     
    	  this.textArea = new JTextArea(20,40);
    	  JScrollPane p = new JScrollPane(this.textArea,
    			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    			JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    	  this.textArea.setWrapStyleWord(true);
     
    	  // default layout is border layout for frame/jframe
    	//this.getContentPane().add("Center",tempImage);
    	  this.getContentPane().add("North",controls);
    	  //this.getContentPane().add("South",this.status);
    	  this.getContentPane().add("Center",p);
    	//  	  this.getContentPane().add("South",p2);
     
    	  this.pack();  //set the size of the frame according 
    			//to the component's preferred sizes
    	  this.show();	//make the frame visible
     
    	  this.repaint();
    	}

  7. #7
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 904
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 904
    Billets dans le blog
    54
    Par défaut
    Mais quelle complexite pour pas grand chose... d'autant plus qu'il y a pas mal de tests inutiles... ex un if (image == null) au debut qui n'arrive jamais car tu crees une image vide au depart (ce qui n'est probablement pas une bonne idee d'ailleur puisque juste apres tu lance une exception si tu n'arrives pas a charger l'image).

    Ensuite l'erreur precedente n'etait pas liee au chargement de l'image mais au fait qu'il ne trouvait pas de methode add(Image) dans ta classe SwingImageButton.

    Apres en fouillant un peu tu aurai trouve des trucs :

    Citation Envoyé par gfx
    Le plus simple est encore de faire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    Image image = ImageIO.read(new File("chemin/vers/image")); 
    JLabel viewer = new JLabel(new ImageIcon(image)); 
     
    JPanel panel = new JPanel(new BorderLayout()); 
    panel.add(viewer);
    Citation Envoyé par bouye
    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
    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
     
    package test; 
     
    import java.awt.*; 
    import java.beans.*; 
    import javax.swing.*; 
     
    /** 
     * <p>Title: </p> 
     * <p>Description: </p> 
     * <p>Copyright: Copyright (c) 2005</p> 
     * <p>Company: </p> 
     * @author not attributable 
     * @version 1.0 
     */ 
    public final class ImagePanel extends JPanel { 
      public static final String IMAGE_PROPERTY = "ImagePanel.image"; 
      private InnerListener innerListener = new InnerListener(); 
     
      public ImagePanel(Image image) { 
        super(); 
        addPropertyChangeListener(IMAGE_PROPERTY, innerListener); 
        setImage(image); 
      } 
     
      public void dispose() { 
        removePropertyChangeListener(IMAGE_PROPERTY, innerListener); 
        innerListener = null; 
        setImage(null); 
      } 
     
      public void setImage(Image image) { 
        putClientProperty(IMAGE_PROPERTY, image); 
      } 
     
      public Image getImage() { 
        return (Image) getClientProperty(IMAGE_PROPERTY); 
      } 
     
      /** {@inheritDoc} 
       */ 
      @Override protected void paintComponent(Graphics graphics) { 
        super.paintComponent(graphics); 
        Image image = getImage(); 
        if (image != null) { 
          graphics.drawImage(image, 0, 0, null); 
        } 
      } 
     
      /** 
       * <p>Title: </p> 
       * <p>Description: </p> 
       * <p>Copyright: Copyright (c) 2005</p> 
       * <p>Company: </p> 
       * @author not attributable 
       * @version 1.0 
       */ 
      private class InnerListener implements PropertyChangeListener { 
        /** {@inheritDoc} 
         */ 
        public void propertyChange(PropertyChangeEvent event) { 
          String property = event.getPropertyName(); 
          if (property.equals(IMAGE_PROPERTY)) { 
            Image image = getImage(); 
            int width = 0; 
            int height = 0; 
            if (image != null) { 
              width = image.getWidth(null); 
              height = image.getHeight(null); 
            } 
            Dimension size = new Dimension(width, height); 
            setPreferredSize(size); 
            setMinimumSize(size); 
            repaint(); 
          } 
        } 
      } 
    }
    Note : ici l'image s'affiche dans le coin superieur gauche (normalement le panel a la taille de l'image, mais certains layout peuvent malencontreusement l'aggrandir). Les changements a apporter sont minimaux pour afficher l'image au centre du panel ou pour tenir compte de la bordure optionnelle du panel.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  8. #8
    Battosaiii
    Invité(e)
    Par défaut
    J'ai encore des problemes a afficher l'image AVEC le textarea dans l'image ... .

    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
    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
     
     import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
    import javax.swing.JPanel;
    import javax.imageio.ImageIO;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.awt.Color;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
      public class SwingImageButton extends JFrame implements ActionListener
      {
     
     
    	//private Image theImage;
    	private JButton button;
    	private JTextArea textArea;
    	private JTextField status;
    	private JCheckBox editable;
    	private JComboBox colorBox;
    	private boolean isTextEditable = true;
    	//private JComboBo
    	   private Image image; 
     
    	public SwingImageButton() 
    	{
    		  super("Swing Button Example");
     
     
    		  ImageIcon monIcon=new ImageIcon(ClassLoader.getSystemResource("portable.JPG"));
     
    JLabel viewer = new JLabel(monIcon);
     
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(viewer); 
     
     
    	  // status TextField
    	  this.status = new JTextField();
    	  this.updateStatus("Application Started.");
     
     
     
    	  this.textArea = new JTextArea(5,10);
     
    	  	this.getContentPane().add(panel);
    			this.getContentPane().add(textArea);
     
    	  this.pack();  //set the size of the frame according 
    			//to the component's preferred sizes
    	  this.show();	//make the frame visible
     
     
    	}
    	private void updateStatus(String theStatus)
    	{
    	  this.status.setText("Status: " + theStatus);
    	}
    	public void actionPerformed(ActionEvent e)
    	{
     
    	}
     
    	public void paintComponent(Graphics g)
    	{
    	//super(g);
     
    Graphics2D g2D=(Graphics2D )g;
    Graphics2D g2d = (Graphics2D)this.image.getGraphics();
    //g2D.drawImage(this.image,20,40,this);
    //super.paintComponent(g);
    	//	g.drawImage( this.tempImage, 10, 10, this);
    	}
     
    	public static void main(String args[])
    	{
    	   new SwingImageButton();
    	}
      }
    J'affiche bien l'image si je vire this.getContentPane().add(textArea);
    si je laisse cette methode l'image est supprimee meme si la taille de l'image ets superieur au textarea.

    J'ai laisse la methode paincomponer qui marche pas , et il troupe la methode super.paintcomponer (g)

    SwingImageButton.java:83: cannot find symbol
    symbol : method paintComponent(java.awt.Graphics)
    location: class javax.swing.JFrame
    super.paintComponent(g);
    ^

  9. #9
    BiM
    BiM est déconnecté
    Expert confirmé
    Avatar de BiM
    Femme Profil pro
    Consultante/Formatrice BIRT & Ingénieur Java/J2EE/GWT
    Inscrit en
    Janvier 2005
    Messages
    7 796
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 39
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Consultante/Formatrice BIRT & Ingénieur Java/J2EE/GWT

    Informations forums :
    Inscription : Janvier 2005
    Messages : 7 796
    Par défaut
    Normal il faut la redéfinir.

  10. #10
    Battosaiii
    Invité(e)
    Par défaut
    je les refedini :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     public void paintComponent(Graphics g)
       {
       //super(g);
     
    Graphics2D g2D=(Graphics2D )g;
    Graphics2D g2d = (Graphics2D)this.image.getGraphics();
    //g2D.drawImage(this.image,20,40,this);
    //super.paintComponent(g);
       //   g.drawImage( this.tempImage, 10, 10, this);
       }

  11. #11
    BiM
    BiM est déconnecté
    Expert confirmé
    Avatar de BiM
    Femme Profil pro
    Consultante/Formatrice BIRT & Ingénieur Java/J2EE/GWT
    Inscrit en
    Janvier 2005
    Messages
    7 796
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 39
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Consultante/Formatrice BIRT & Ingénieur Java/J2EE/GWT

    Informations forums :
    Inscription : Janvier 2005
    Messages : 7 796
    Par défaut
    Oui mais si t'appeles super.paintComponent(g); c'est le paintComponent de ta superclasse et non de ta classe.

Discussions similaires

  1. Charger une image avec du XML
    Par Leuprochon dans le forum ActionScript 3
    Réponses: 1
    Dernier message: 16/04/2012, 18h09
  2. Charger une image avec uigetfile
    Par SmileSoft dans le forum Images
    Réponses: 3
    Dernier message: 12/04/2011, 13h49
  3. Charger une image avec Visual C #
    Par ines4251 dans le forum Windows Forms
    Réponses: 2
    Dernier message: 08/01/2010, 09h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo