| 12
 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
 
 | public class Panel extends JPanel {
 
   private Image image = new IconImage( "image1.png" );
   private final List<LocatedImage> images = new ArrayList<>();
 
   @Override
   protected void paintComponent( Graphics g ) { 
      if ( image!=null ) {
          g.drawImage( image, 0 , 0 , this.getWigth() , this.getHeight() , this )
      }
      for(LocatedImage locImage : images) {
          locImage.drawImage(g);
      }
   }
 
   // Pour changer le background
   public void setBackGround( Image image ) {
     this.image=image;
     repaint();
  }
 
  public void addImage(Image image, int x, int y, int width, int height) { 
       SwingUtilities.invokeLater(()-> addImage(new LocatedImage(image,x,y,width,height))); // on exécute l'ajout sur l'Event Dispatch Thread pour éviter les conflits de manipulation de la liste
  }
 
    // ne doit être exécutée que dans l'EDT
   private void addImage( LocatedImage image ) {
     this.images.add(image);
     repaint();
  }
 
 
  private class LocatedImage {
       private int x;
       private int y;
       private int width;
       private int height;
       private Image image;
       public LocatedImage(Image image, int x, int y, int width, int height) {
           this.image=Object.requiredNonNull(image);
           this.x=x;
           // etc..
       }
 
       public void drawImage(Graphics g) {
            g.drawImage(image,x,y,width,height,Panel.this);
       }
  }
 
} | 
Partager