title: Creating custom JButton from images containing transparent pixels
body: <p><b>Read edit 2 for what I'm actually missing to make it work</b></p>
  
  <p>I'm currently trying to create some custom JButtons using images created in photoshop that have an alpha parameter.</p>
  
  <p>So far, overriding the paint() method to draw the image has worked in the sense that the button is drawn showing the correct image. I'd like to improve it, though, by making its shape (clickable area) the same as the visible pixels on the image (right now if I draw the button's border, it's a square).</p>
  
  <p>Is there an easy way to do that or do I have to parse the image and find the alpha pixels to make a custom border?</p>
  
  <p>Which methods would I have to override to make it work the way I want?</p>
  
  <p>Also, another question I'm going to have later: would it be better to use some kind of algorithm to change the images' colors to make it seem like it is being clicked when people click on it or am I better off creating a second image and drawing that one while the button is active?</p>
  
  <p><b>Edit:</b> I just read on some other question that I should redefine paintComponent() instead of paint(), I'd like to know why since redefining paint() works fine?</p>
  
  <p><b>Edit 2:</b> I changed everything to make sure my JButtons are created using the default constructor with an icon. What I'm trying to do is get the X and Y position of where the click was registered and grab the icon's pixel at that position and check its alpha channel to see if it is 0 (if it is, do nothing, else do the action it is supposed to do).</p>
  
  <p>The thing is, the alpha channel always returns 255 (and blue, red and green are at 238 on transparent pixels). On other pixels, everything returns the value it should be returning.</p>
  
  <p>Here's an example (try it with another image if you want) that recreates my problem:</p>
  
  <pre><code>public class TestAlphaPixels extends JFrame
  {
    private final File FILECLOSEBUTTON = new File("img\\boutonrondX.png");  //My round button with transparent corners
    private JButton closeButton = new JButton(); //Creating it empty to be able to place it and resize the image after the button size is known
  
  
    public TestAlphaPixels() throws IOException
    {
      setLayout(null);
      setSize(150, 150);
  
      closeButton.setSize(100, 100);
      closeButton.setContentAreaFilled(false);
      closeButton.setBorderPainted(false);
  
      add(closeButton);
  
      closeButton.addMouseListener(new MouseListener()
        {
  
          public void mouseClicked(MouseEvent e)
          {
          }
  
          public void mousePressed(MouseEvent e)
          {
          }
  
          public void mouseReleased(MouseEvent e)
          {
            System.out.println("Alpha value of pixel (" + e.getX() + ", " + e.getY() + ") is: " + clickAlphaValue(closeButton.getIcon(), e.getX(), e.getY()));
          }
  
          public void mouseEntered(MouseEvent e)
          {
          }
  
          public void mouseExited(MouseEvent e)
          {
          }
        });
      Image imgCloseButton = ImageIO.read(FILECLOSEBUTTON);
  
      //Resize the image to fit the button
      Image newImg = imgCloseButton.getScaledInstance((int)closeButton.getSize().getWidth(), (int)closeButton.getSize().getHeight(), java.awt.Image.SCALE_SMOOTH);
      closeButton.setIcon(new ImageIcon(newImg));
  
  
    }
  
    private int clickAlphaValue(Icon icon, int posX, int posY) 
    {
      int width = icon.getIconWidth();
      int height = icon.getIconHeight();
  
      BufferedImage tempImage = (BufferedImage)createImage(width, height);
      Graphics2D g = tempImage.createGraphics();
  
      icon.paintIcon(null, g, 0, 0);
  
      g.dispose();
  
      int alpha = (tempImage.getRGB(posX, posY) &gt;&gt; 24) &amp; 0x000000FF;
  
      return alpha;
    } 
    public static void main(String[] args)
    {
      try
      {
        TestAlphaPixels testAlphaPixels = new TestAlphaPixels();
        testAlphaPixels.setVisible(true);
        testAlphaPixels.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
      catch(IOException ioe)
      {
        ioe.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/oTWP4.png" alt="What this sample actually displays"></p>
  
  <p>This is just a wild guess, but is it possible that when my image gets cast to an Icon, it loses its Alpha property and thus doesn't return the correct value? Anyway, I'd really appreciate it if someone could actually help me out and tell me what I should be changing to get the correct value.</p>
  
  <p>I'm guessing that because when I try it with the original image, the alpha channel's value is fine, but I can't actually use that BufferedImage because I resize it, so I actually get the channel values of the image with the original size...</p>