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...