View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    * 
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.view.swing.tree;
12  
13  import com.jsql.util.LogLevelUtil;
14  import org.apache.logging.log4j.LogManager;
15  import org.apache.logging.log4j.Logger;
16  
17  import javax.imageio.ImageIO;
18  import javax.swing.*;
19  import java.awt.*;
20  import java.awt.image.BufferedImage;
21  import java.io.IOException;
22  import java.util.Objects;
23  
24  /**
25   * An icon composed of a main icon and another one displayed in the bottom right corner.
26   */
27  public class ImageOverlap extends ImageIcon {
28      
29      /**
30       * Log4j logger sent to view.
31       */
32      private static final Logger LOGGER = LogManager.getRootLogger();
33  
34      /**
35       * The path of icon displayed in the bottom right corner.
36       */
37      private final String iconPathOverlap;
38  
39      /**
40       * Create icon with tiny icon on top layer.
41       * @param main Main icon to display
42       * @param iconPathOverlap Secondary icon to display on top of main icon
43       */
44      public ImageOverlap(ImageIcon main, String iconPathOverlap) {
45          super(main.getImage());
46          this.iconPathOverlap = iconPathOverlap;
47      }
48  
49      @Override
50      public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
51          super.paintIcon(c, g, x, y);
52          try {
53              BufferedImage bufferedImage = ImageIO.read(
54                  Objects.requireNonNull(ImageOverlap.class.getClassLoader().getResource(this.iconPathOverlap))
55              );
56              
57              g.drawImage(
58                  bufferedImage,
59                  (this.getIconWidth() - bufferedImage.getWidth()) / 2,
60                  (this.getIconHeight() - bufferedImage.getHeight()) / 2,
61                  null
62              );
63          } catch (IOException e) {
64              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
65          }
66      }
67  }