title: Java Swing button disappear
body: <p>I am trying to make an invisible button where the painted button is, but when I try to add the button, it disappear almost every time I run the program and when its visible it goes invisible on resize. Please help me, whats wrong?</p>
  
  <pre><code>import javax.imageio.ImageIO;
  import javax.swing.*;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.FlowLayout;
  import java.awt.Font;
  import java.awt.FontMetrics;
  import java.awt.Graphics;
  import java.awt.Image;
  import java.io.IOException;
  
  public class Main extends JFrame
  {
      public static final int VERSION = 1;
      private static final long serialVersionUID = 1L;
      private Image img;
      private boolean hasMouseListener = false;
      private Image bgImage;
  
      public static int ii;
      public static int jj;
  
  
      public Main() throws IOException
      {
          super("Unigin Alpha 1.0.0"); //Set title
  
  
          ii = this.getWidth();
          jj = this.getHeight();
  
          setBackground(Color.WHITE);
          //loginForm1 = new LoginForm(this);
          JPanel localJPanel = new JPanel();
          //localJPanel.setLayout(null);
          setVisible(true); 
          setTitle("Unigin");
  
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          localJPanel.setPreferredSize(new Dimension(854, 480));
  
          setLayout(new FlowLayout());
          add(localJPanel, "Center");
  
          //-------------------------------------------------------------------------
          JButton bb = new JButton("HEEL");
          localJPanel.add(bb, "CENTER");
          //bb.setLocation(getWidth()/2-76-8, getHeight()/2); Doesn't work!
          //i / 2 - 76 - 8, j / 2, 76, 20
  
          pack();
          setLocationRelativeTo(null);
          try
          {
            setIconImage(ImageIO.read(Main.class.getResource("favicon.png")));
          } catch (IOException localIOException) {
            localIOException.printStackTrace();
          }
  
          super.paint(getGraphics());
      }
  
  
  
  
  
      public void paint(Graphics paramGraphics)
        {
          try {
              bgImage = ImageIO.read(Main.class.getResource("wool_red.png")).getScaledInstance(32, 32, 16);
            } catch (IOException localIOException) {
              localIOException.printStackTrace();
            }
  
          int i = getWidth() / 2;
          int j = getHeight() / 2;
          if ((img == null) || (img.getWidth(null) != i) || (img.getHeight(null) != j)) {
            img = createVolatileImage(i, j);
          }
  
          Graphics localGraphics = img.getGraphics();
          for (int k = 0; k &lt;= i / 32; k++)
            for (int m = 0; m &lt;= j / 32; m++)
              localGraphics.drawImage(bgImage, k * 32, m * 32, null);
          String str;
          FontMetrics localFontMetrics;
  
          if (true) {
            if (!hasMouseListener) {
              hasMouseListener = true;
              //addMouseListener(this);
            }
            localGraphics.setColor(Color.WHITE);
            str = "Bukkit Unigin";
            localGraphics.setFont(new Font(null, 1, 20));
            localFontMetrics = localGraphics.getFontMetrics();
            localGraphics.drawString(str, i / 2 - localFontMetrics.stringWidth(str) / 2, j / 2 - localFontMetrics.getHeight() * 2);
  
            localGraphics.setColor(Color.LIGHT_GRAY);
            localGraphics.setFont(new Font(null, 0, 12));
            localFontMetrics = localGraphics.getFontMetrics();
  
            localGraphics.setColor(Color.LIGHT_GRAY);
  
  
            localGraphics.fill3DRect(i / 2 - 76 - 8, j / 2, 76, 20, true);
            localGraphics.fill3DRect(i / 2 + 8, j / 2, 76, 20, true);
            //localGraphics.fill3DRect(x,y,width,height,true);
  
            localGraphics.setColor(Color.WHITE);
            str = "What do you want to do?";
            localGraphics.drawString(str, i / 2 - localFontMetrics.stringWidth(str) / 2, j / 2 - 8);
  
            localGraphics.setColor(Color.BLACK);
            str = "New plugin";
            localGraphics.drawString(str, i / 2 - 56 - 18 - localFontMetrics.stringWidth(str) / 2 + 28, j / 2 + 14);
            str = "Load plugin";
            localGraphics.drawString(str, i / 2 + 18 - localFontMetrics.stringWidth(str) / 2 + 28, j / 2 + 14);
          }
  
          localGraphics.dispose();
          paramGraphics.drawImage(img, 0, 0, i * 2, j * 2, null);
  
  
        }
  
      public static void main(String[] args) throws IOException {
  
          new Main();
  
  
  
      }
  }
  </code></pre>