ProgLearner
ProgLearner

Reputation: 181

Placing JButton on image

Aim: to place JButton on a top of an image that is loaded from a different class. Problem: only JButton or image can be displayed

//main class
JPanel mPanel.add(new JButton("ddd") );
mPanel.add(drawbg);


class DrawBg extends JPanel {   

    public Dimension getPreferredSize() {
            return new Dimension(1280, 720);
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            // background
            Image img = new ImageIcon(getClass().getResource("/images/test_bg.jpg")).getImage();
            int imgX = img.getWidth(null);
            int imgY = img.getHeight(null);
            g.drawImage(img, (getWidth() - imgX), (getHeight() - imgY), imgX, imgY, null);

    }

Upvotes: 1

Views: 49

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Edit Ah yes, as ControlAltDel points out, you're not adding your JButton to your drawing JPanel instance. Just do that, and your problem is solved.


I don't see the bug in your code, so it might lie in code not shown. I do see a problem in that you're reading the image in, inside of paintComponent, something that should never be done as it will slow down painting and thus slow down the perceived responsiveness of your program. Also, why re-read in the image with each paint. Instead read it in once.

For example:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class ButtonOnImg extends JPanel {
   public static final String IMG_PATH = "https://duke.kenai.com/gun/Gun.jpg";
   private BufferedImage img;

   public ButtonOnImg() throws IOException {
      URL url = new URL(IMG_PATH);
      img = ImageIO.read(url);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (img != null) {
         g.drawImage(img, 0, 0, null);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (img == null) {
         return super.getPreferredSize();
      } else {
         int w = img.getWidth();
         int h = img.getHeight();
         return new Dimension(w, h);
      }
   }

   private static void createAndShowGui() {
      try {
         ButtonOnImg mainPanel = new ButtonOnImg();

         mainPanel.add(new JButton("Button!"));

         JFrame frame = new JFrame("ButtonOnImg");
         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         frame.getContentPane().add(mainPanel);
         frame.pack();
         frame.setLocationByPlatform(true);
         frame.setVisible(true);
      } catch (IOException e) {
         e.printStackTrace();
      }

   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Which displays as:

enter image description here

Upvotes: 2

Related Questions