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

AWT/Swing Java Discussion :

Pas d'image malgré le drawImage()


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Expert confirmé
    Avatar de SheikYerbouti
    Profil pro
    Inscrit en
    Mai 2003
    Messages
    6 760
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2003
    Messages : 6 760
    Par défaut Pas d'image malgré le drawImage()
    Bonjour,

    j'ai une classe qui implémente JButton, munie d'une méthode paint().
    Toutes les instructions dans cette méthode donnent le résultat escompté à par drawImage().

    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
     
    public class CButton extends JButton
    {
      ...
     
      public void paint(Graphics g)
      {
         System.out.println("paint() CButton");
          super.paint(g);
          Graphics2D g2 = (Graphics2D) g ;
          int x,y,w,h ;
          int ix,iy ;
          x = (int)this.getBounds().getX() ;
          y = (int)this.getBounds().getY() ;
          w = (int)this.getBounds().getWidth() ;
          h = (int)this.getBounds().getHeight() ;
          GradientPaint redtowhite = new GradientPaint(0,0,cStart,w,h,cEnd);
         g2.setPaint(redtowhite);
          g2.fill(new Rectangle2D.Double(4,4,w-6,h-6)) ;
          // image to draw ?
          if (image != null)
          {
             System.out.println("draw image...");
             g2.drawImage(image,5,5,this);
          }
          // label
          g2.setColor(cText);
          g2.setFont(font);
          FontMetrics fm = g2.getFontMetrics(font);  // metrics for this object
          Rectangle2D rect = fm.getStringBounds(this.getText(), g2); // size of string
          int textHeight  = (int)(rect.getHeight());
          int textWidth   = (int)(rect.getWidth());
     
          //... Center text horizontally and vertically
          if(iTextPos == Left)
          {
            ix = 5;
            iy = (this.getHeight() - textHeight) / 2  + fm.getAscent();
          }
          else if(iTextPos == Center)
          {
            ix = (this.getWidth()  - textWidth)  / 2;
            iy = (this.getHeight() - textHeight) / 2  + fm.getAscent();
          }      
          else
          {
            ix = (this.getWidth()  - textWidth) - 5 ;
            iy = (this.getHeight() - textHeight) / 2  + fm.getAscent();
          }                              
          g2.drawString(this.getText(), ix, iy);                                  
     
      }
    J'avoue rester perplexe...

  2. #2
    Expert éminent
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Billets dans le blog
    1
    Par défaut
    Salut,


    En général pour les composants Swing, il est plus propre de redéfinir paintComponent() plutôt que paint().

    Sinon il faudrait savoir comment tu fais pour récupérer ton image...

    a++

  3. #3
    Expert confirmé
    Avatar de SheikYerbouti
    Profil pro
    Inscrit en
    Mai 2003
    Messages
    6 760
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2003
    Messages : 6 760
    Par défaut
    Comme ceci et qui ne m'a jamais posé de problème auparavant:


    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
     
    Image img = loadImage( 'd:/image.jpg' ) ;
    ...
          /*
         *  Load an image from JAR file, Client machine or Internet URL 
         */
        private Image loadImage(String p_imageName)
        {
          URL imageURL = null;
          boolean loadSuccess = false;
          Image img = null ;
          String imageName = p_imageName ;
     
          try {
          file_image = new File( imageName );
          fis = new FileInputStream( file_image );
          }
          catch( Exception e )
              {
                e.printStackTrace();
              }      
          iMageSize = ( int )file_image.length() ;
          imageName = "<a href="file:///" target="_blank">file:///</a>" + p_imageName ;
          //JAR
          log("Searching JAR for " + imageName);
          imageURL = getClass().getResource(imageName);
          if (imageURL != null)
          {
            log("URL: " + imageURL.toString());
            try
            {
              img = Toolkit.getDefaultToolkit().getImage(imageURL);
              loadSuccess = true;
              log("Image found in JAR: " + imageURL.toString());
              return img ;
            }
            catch (Exception ilex)
            {
              log("Error loading image from JAR: " + ilex.toString());
            }
          }
          else
          {
            log("Unable to find " + imageName + " in JAR");
          }
          //DOCBASE
          if (loadSuccess == false)
          {
            log("Searching docbase for " + imageName);
            try
            {
              if(imageName.toLowerCase().startsWith("<a href="http://" target="_blank">http://")||imageName.toLowerCase().startsWith("https://</a>"))
              {
                imageURL = new URL(imageName);
              }
              else if(imageName.toLowerCase().startsWith("file:"))
              {
                imageURL = new URL(imageName);
              }
              else
              {
                imageURL = new URL(m_codeBase.getProtocol() + "://" +m_codeBase.getHost() + ":" + m_codeBase.getPort() + imageName);
              }
              log("Constructed URL: " + imageURL.toString());
              try
              {
                img = createImage((java.awt.image.ImageProducer)imageURL.getContent());
                loadSuccess = true;
                log("Image found in DOCBASE: " + imageURL.toString());
                return img ;
              }
              catch (Exception ilex)
              {
                log("Error reading image - " + ilex.toString());
              }
            }
            catch (java.net.MalformedURLException urlex)
            {
              log("Error creating URL - " + urlex.toString());
            }
          }
          //CODEBASE
          if (loadSuccess == false)
          {
            log("Searching codebase for " + imageName);
            try
            {
              imageURL = new URL(m_codeBase, imageName);
              log("Constructed URL: " + imageURL.toString());
              try
              {
                img = createImage((java.awt.image.ImageProducer)imageURL.getContent());
                loadSuccess = true;
                log("Image found in CODEBASE: " + imageURL.toString());
                return img ;
              }
              catch (Exception ilex)
              {
                      log("Error reading image - " + ilex.toString());
              }
            }
            catch (java.net.MalformedURLException urlex)
            {
              log("Error creating URL - " + urlex.toString());
            }
          }
          if (loadSuccess == false)
          {
            log("Error image " + imageName + " could not be located");
          }
          return img ;
        }

  4. #4
    Expert éminent
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Billets dans le blog
    1
    Par défaut
    Oulà un peu compliqué tout ca !!!

    Ce que je voudrais savoir c'est dans quel cas tu te situe exactement (je n'ai pas envie de décortiquer l'algo) et quel méthode standard tu utilises pour lire l'image.

    Par exemple Toolkit.getImage() ne renverra pas d'erreur s'il n'arrive pas à lire l'image, et l'image retourné sera vide...

    a++

  5. #5
    Expert confirmé
    Avatar de SheikYerbouti
    Profil pro
    Inscrit en
    Mai 2003
    Messages
    6 760
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2003
    Messages : 6 760
    Par défaut
    Franchement, l'erreur de vient pas de là. Je me sers de cette routine depuis longtemps et je t'assure que l'image n'est pas vide. Tout simplement, je ne la vois pas sur le bouton.

  6. #6
    Expert éminent
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Billets dans le blog
    1
    Par défaut
    Essaye simplement d'appliquer l'image en tant qu'icone sur ton JButton ou sur un JLabel pour voir si elle s'affiche...

    a++

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 13/09/2007, 09h45
  2. Réponses: 0
    Dernier message: 12/09/2007, 11h07
  3. gtk stock item, pas d'image sur mon bouton ?
    Par titor dans le forum GTK+ avec C & C++
    Réponses: 5
    Dernier message: 25/02/2007, 20h21
  4. picture : pas d'image sur un dvi et un pdf
    Par ChipsterJulien dans le forum Tableaux - Graphiques - Images - Flottants
    Réponses: 6
    Dernier message: 10/03/2006, 14h23
  5. Le fichier xxx.xxx n’est pas une image Windows32 valide
    Par Furius dans le forum Windows XP
    Réponses: 7
    Dernier message: 12/12/2005, 21h25

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