title: custom shaped transparent buttons
body: <p>I found 2 different solutions that don't work for me. The first one deals with transparency. The second one I would prefer because it's cleaner looking. The goal is to create custom shaped buttons using images that have transparency. I want to ignore the transparent sections and only take action when I click on the non-transparent part of the image.</p>
  
  <p>First code comes from here <a href="http://java.macteki.com/2012/07/custom-shape-buttons.html" rel="nofollow noreferrer">http://java.macteki.com/2012/07/custom-shape-buttons.html</a>. By the way, what is the point of 0x00ffffff in the code below?</p>
  
  <pre><code>//********** BEGIN OF IMAGE BUTTON CODE ****************
        // The following is the actual code to create a image button
        // you may use any transparent image file, just change "a.png" below
        final BufferedImage image = ImageIO.read(new File("a.png"));
        ImageIcon icon = new ImageIcon(image);
  
        jLabel1 = new javax.swing.JLabel(icon);
        jLabel1.addMouseListener(new MouseAdapter()
        {
        public void mouseClicked(MouseEvent e)
        {
        boolean transparent = (image.getRGB(e.getX(),e.getY()) &amp; 0x00ffffff)!=0;
       if (!transparent) {
        // write your button handler here
         System.out.println("button pressed");
        } else {
          System.out.println("else");
        }
        }
        }
  
        );
        //********** END OF IMAGE BUTTON CODE ****************
        // add the button to the panel so that it becomes visible
  </code></pre>
  
  <p>It works with his test image, but does not work with any of mine. I even took his and just changed the color of the A in gimp, exported to a2.png and it does not work. It registers all areas as <code>else</code> and <code>if (!transparent)</code> never triggers.</p>
  
  <p>This image works:
  <img src="https://i.stack.imgur.com/oCY9t.png" alt="This image works">
  This image does not work:
  <img src="https://i.stack.imgur.com/7D547.png" alt="This image does not work"></p>
  
  <p>The second code that I would prefer comes from here <a href="https://stackoverflow.com/questions/5751311/creating-a-custom-button-in-java-with-jbutton">Creating a custom button in Java with JButton</a>. The image applies to the button, but a click anywhere in the box, including the transparent areas will register as a click. I want it to only work when you click on the non-transparent areas (or the A in this example).</p>
  
  <pre><code>BufferedImage startButton = ImageIO.read(new File("a2.png"));       
          jButton1 = new javax.swing.JButton(new ImageIcon(startButton));  
          jButton1.setBorder(BorderFactory.createEmptyBorder());
          jButton1.setContentAreaFilled(false);
          jButton1.setFocusable(false);
  </code></pre>
  
  <p>Here is the code I am trying to use based on the example from Guillame</p>
  
  <pre><code>private void initComponents() throws IOException {
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
          final BufferedImage image = ImageIO.read(getClass().getResource("/images/a.png"));
          jButton1 = new JButton(new ImageIcon(image)) {
               @Override
               public boolean contains(int x, int y) {
                   Rectangle viewRect = getBounds();
                   Insets insets = getInsets();
                   viewRect.x += insets.left;
                   viewRect.y += insets.top;
                   viewRect.width -= insets.left + insets.right;
                   viewRect.height -= insets.top + insets.bottom;
                   Rectangle iconR = new Rectangle();
                   SwingUtilities.layoutCompoundLabel(this, this.getFontMetrics(this.getFont()), this.getText(), this.getIcon(),
                           this.getVerticalAlignment(), this.getHorizontalAlignment(), this.getVerticalTextPosition(),
                           this.getHorizontalTextPosition(), viewRect, iconR, new Rectangle(), this.getIconTextGap());
                   if (!iconR.contains(x, y)) {
                       return false;
                   }
                   x -= iconR.x;
                   y -= iconR.y;
                   Color c = new Color(image.getRGB(x, y), true);
                   return c.getAlpha() != 0 &amp;&amp; (c.getRed() &lt; 255 || c.getGreen() &lt; 255 || c.getBlue() &lt; 255);
               }
           };
  
          jButton1.setContentAreaFilled(false); 
          jButton1.setFocusPainted(false);            
          jButton1.setRolloverEnabled(false);       
          jButton1.setBorderPainted(false);
  
          jButton1.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  jButton1ActionPerformed(evt);
              }
          });
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addGap(72, 72, 72)
                  .addComponent(jButton1)
                  .addContainerGap(255, Short.MAX_VALUE))
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                  .addContainerGap(193, Short.MAX_VALUE)
                  .addComponent(jButton1)
                  .addGap(84, 84, 84))
          );
  
          pack();
      }// &lt;/editor-fold&gt;//GEN-END:initComponents
  </code></pre>