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

Composants Java Discussion :

Jlist Image et Panel


Sujet :

Composants Java

  1. #1
    Membre du Club
    Inscrit en
    Octobre 2008
    Messages
    57
    Détails du profil
    Informations forums :
    Inscription : Octobre 2008
    Messages : 57
    Points : 42
    Points
    42
    Par défaut Jlist Image et Panel
    salut,
    je n'arrive pas á utiliser Jlist. En fait j'ai une classe qui me permet de prendre des photos à partir d'une webcam. Les images sont affichées dans un Frame composé de trois panel: un panel pour la webcam un autre pour un bouton qui me permet de capturer l'image et enfin un autre pour afficher l'image capturée. En ce qui concerne le panel pour l'image capturer j'ai une classe ImagePanel qui fait ce travail:

    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
     import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.*;
    import javax.media.protocol.DataSource;
     
    import javax.media.Buffer;
     import javax.media.CaptureDeviceInfo;
     import javax.media.CaptureDeviceManager;
     import javax.media.Manager;
     import javax.media.MediaLocator;
    import javax.media.NoDataSourceException;
     import javax.media.Player;
     import javax.media.control.FrameGrabbingControl;
     import javax.media.format.VideoFormat;
     import javax.media.util.BufferToImage;
     import javax.swing.JButton;
     import javax.swing.JComponent;
    import javax.swing.text.html.HTMLDocument.Iterator;
     
     import com.sun.image.codec.jpeg.JPEGCodec;
     import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
     
     public class SwingCapture extends Panel implements ActionListener
     {
     public static Player player = null;
     public CaptureDeviceInfo di = null;
     public MediaLocator ml = null;
     public JButton capture = null;
     public Buffer buf = null;
    public Image img = null;
     public VideoFormat vf = null;
     public BufferToImage btoi = null;
     public ImagePanel imgpanel = null;
     public DataSource src = null;
     public ImagePanel im;
     public List<String> myList;
     int count =1;
     String s="";
     public HauptFenster h;
     public SwingCapture()
     {
     
    	 h = new HauptFenster();
     
    	 myList = new ArrayList<String>();
     
     setLayout(new BorderLayout());
     setSize(320,550);
     
     imgpanel = new ImagePanel();
     capture = new JButton("Capture");
     capture.addActionListener(this);
     
     im = new ImagePanel();
     
    // String str1 = "vfw:Logitech USB Video Camera:0";
    // String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
    // di = CaptureDeviceManager.getDevice(str2);
    // ml = new MediaLocator("vfw://0");
     
     VideoFormat vidformat = new VideoFormat(VideoFormat.YUV);
     Vector devices = CaptureDeviceManager.getDeviceList(vidformat);
     CaptureDeviceInfo di = (CaptureDeviceInfo) devices.firstElement();
     try {
    	 src = Manager.createDataSource(di.getLocator());
    } catch (NoDataSourceException e1) {
    	// TODO Auto-generated catch block
    	e1.printStackTrace();
    } catch (IOException e1) {
    	// TODO Auto-generated catch block
    	e1.printStackTrace();
    }
     
     try
     {
     player = Manager.createRealizedPlayer(src);
     player.start();
     Component comp;
     
     if ((comp = player.getVisualComponent()) != null)
     {
     add(comp,BorderLayout.NORTH);
     }
     add(capture,BorderLayout.CENTER);
     add(imgpanel,BorderLayout.SOUTH);
     
     }
     catch (Exception e)
     {
     e.printStackTrace();
     }
     Frame f = new Frame("SwingCapture");
     f.addWindowListener(new WindowAdapter() {
    	 public void windowClosing(WindowEvent e) {
    	 playerclose();
    	 System.exit(0);}});
    	 f.add("Center",this);
    	 f.pack();
    	 f.setSize(new Dimension(320,550));
    	 f.setVisible(true);
     
     }
     
     
    // public static void main(String[] args) {
    //Frame f = new Frame("SwingCapture");
    // SwingCapture cf = new SwingCapture();
    // f.addWindowListener(new WindowAdapter() {
    // public void windowClosing(WindowEvent e) {
    // playerclose();
    // System.exit(0);}});
    // f.add("Center",cf);
    // f.pack();
    // f.setSize(new Dimension(320,550));
    // f.setVisible(true);
    // 
    //}
     
     
     public static void playerclose()
     {
     player.close();
     player.deallocate();
     }
     
     
     public void actionPerformed(ActionEvent e)
     {
     JComponent c = (JComponent) e.getSource();
     
     if (c == capture)
     {
     
     
     
     // Grab a frame
     FrameGrabbingControl fgc = (FrameGrabbingControl)
     player.getControl("javax.media.control.FrameGrabbingControl");
     buf = fgc.grabFrame();
     
     // Convert it to an image
     btoi = new BufferToImage((VideoFormat)buf.getFormat());
     img = btoi.createImage(buf);
     
     
     // show the image
     imgpanel.setImage(img);
     
    // name of image
      s = "c:\\test"+count+".jpg";
     
     // save image
     im.saveJPG(img,s);
     myList.add(s);
    // listIterator();
     
     }
     h.combo.addItem(s);
    //t.jc.addItem(s);
     
     count++;
     }
     
     
     
    //public void addInListe(String a){
     
    //	myList.add(a);
    //}
     
    //public void listIterator(){
    //java.util.Iterator<String> it = myList.iterator();
    //while ( it.hasNext() ){
    //	
    //    String  tr =it.next() ;
    //    t.jc.addItem(tr);
    //  }
    //
    //}
     
     
     }

    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
    84
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Panel;
    import java.awt.image.BufferedImage;
    import java.io.FileOutputStream;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Vector;
     
    import javax.swing.DefaultListModel;
    import javax.swing.ImageIcon;
    import javax.swing.JList;
    import javax.swing.ListSelectionModel;
     
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
     
    class ImagePanel extends Panel
     {
     public Image myimg = null;
     public Vector vector;
    public JList list;
     public ImagePanel()
     {
     setLayout(null);
     setSize(320,240);
     
     
     }
     
     public void setImage(Image img)
     {
     
     
    	 this.myimg = img;
    	 repaint();
     }
     
     public void paint(Graphics g)
     {
     if (myimg != null)
     {
     g.drawImage(myimg, 0, 0, this);
     }
     }
     
     
     
     public static void saveJPG(Image img, String s)
     {
     
     BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
     Graphics2D g2 = bi.createGraphics();
     g2.drawImage(img, null, null);
     
     FileOutputStream out = null;
     try
     {
     out = new FileOutputStream(s);
     }
     catch (java.io.FileNotFoundException io)
     {
     System.out.println("File Not Found");
     }
     
     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
     JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
     param.setQuality(0.5f,false);
     encoder.setJPEGEncodeParam(param);
     
     try
     {
     encoder.encode(bi);
     out.close();
     }
     catch (java.io.IOException io)
     {
     System.out.println("IOException");
     }
     }
     
     }
    Mon problem est le suivant: lorsque je prends la première photo elle s'affiche dans le panel correspondant et ensuite si je prends la deuxième elle prend la place de la première. Pour que toutes les photos prisent soient visualiser par l'utilisateur j'ai pensé a un Jlist d'image et alors j'ai essayé ceci:

    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
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Panel;
    import java.awt.image.BufferedImage;
    import java.io.FileOutputStream;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Vector;
     
    import javax.swing.DefaultListModel;
    import javax.swing.ImageIcon;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.ListSelectionModel;
     
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
     
    class ImagePanel extends Panel
     {
     public Image myimg = null;
    private DefaultListModel dl;
    private JList mylist;
    private Vector myPictures;
    private JPanel l;
     private JScrollPane jscroll;
     
     public ImagePanel()
     {
     setLayout(null);
     setSize(320,240);
     
     
     
     }
     
    // public void setImage(Image img)
    // {
    //	 this.myimg = img;
    //	 repaint();
    // }
     
     public void paint(Graphics g)
     {
     if (myimg != null)
     {
     g.drawImage(myimg, 0, 0, this);
     }
     }
     
     
     
     public static void saveJPG(Image img, String s)
     {
     
     BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
     Graphics2D g2 = bi.createGraphics();
     g2.drawImage(img, null, null);
     
     FileOutputStream out = null;
     try
     {
     out = new FileOutputStream(s);
     }
     catch (java.io.FileNotFoundException io)
     {
     System.out.println("File Not Found");
     }
     
     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
     JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
     param.setQuality(0.5f,false);
     encoder.setJPEGEncodeParam(param);
     
     try
     {
     encoder.encode(bi);
     out.close();
     }
     catch (java.io.IOException io)
     {
     System.out.println("IOException");
     }
     }
     public void jlist(){
    	 l = new JPanel();
    	 jscroll = new JScrollPane();
    	 this.dl = new DefaultListModel();
    	 this.mylist = new JList(this.dl);
    		this.mylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		this.mylist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    		this.mylist.setVisibleRowCount(1);
     
    		 for(int i = 0; i<myPictures.size();i++){
    				Image myImg = (Image) myPictures.get(i);
    				int height = (int)(myImg.getHeight(null)*0.3);
    				int width = (int) (myImg.getWidth(null)*0.3);
    				Image test = myImg.getScaledInstance(width,height,1);
    				ImageIcon iI = new ImageIcon(test);
    				this.dl.addElement(iI);
     }
    		 jscroll.add(mylist);
    		 l.add(jscroll);
    		 repaint();
     }
     public void addImage(Image m){
    	 myPictures.addElement(m);
     }
     }
    en fait j'ai remplacé la classe setImage de ImagePanel dans SwingCapture par jlist(). ca ne fonctionne pas.
    Quelqu'un pourait il m'aider svp?
    Merci d'avance.

  2. #2
    Membre actif Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Points : 275
    Points
    275
    Par défaut
    Nous ne devons pas mélanger les composants d'interface AWT avec ceux de Swing:
    Panel --> JPanel, Frame --> JFrame !!!
    Enlève la méthode paint(Graphics g), elle n'a plus de raison d'être.

    Ceci devrait fonctionner (j'ai omis la méthode saveJPG qui ne concerne pas notre problème):

    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
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    public class ImagePanel extends JPanel {
        private DefaultListModel dl;
        private JList mylist;
        private Vector myPictures;
        public ImagePanel() {
            myPictures = new Vector();
            setLayout(new BorderLayout());
            setPreferredSize(new Dimension(320, 240));
            this.dl = new DefaultListModel();
            this.mylist = new JList(this.dl);
            this.mylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            this.mylist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
            this.mylist.setVisibleRowCount(1);
            add(new JScrollPane(mylist));
        }
        public void jlist() {
            dl.removeAllElements();
            for (int i = 0; i < myPictures.size(); i++) {
                Image myImg = (Image) myPictures.get(i);
                int height = (int) (myImg.getHeight(null) * 0.3);
                int width = (int) (myImg.getWidth(null) * 0.3);
                Image test = myImg.getScaledInstance(width, height, 1);
                ImageIcon iI = new ImageIcon(test);
                this.dl.addElement(iI);
            }
        }
        public void addImage(final Image m) {
            myPictures.addElement(m);
        }
    }

Discussions similaires

  1. Jlist image et texte
    Par Ceubex dans le forum Composants
    Réponses: 1
    Dernier message: 30/01/2011, 08h38
  2. Question sur Image et Panel
    Par Syrus dans le forum ActionScript 3
    Réponses: 0
    Dernier message: 16/07/2008, 11h12
  3. Problème d'image dans panel
    Par Morgoth818 dans le forum Agents de placement/Fenêtres
    Réponses: 5
    Dernier message: 09/07/2008, 09h01
  4. Réponses: 5
    Dernier message: 11/02/2007, 21h46
  5. Réponses: 8
    Dernier message: 09/03/2006, 22h06

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