Hanna
Hanna

Reputation: 10763

Overlay images on a GridLayout in Java

Is it possible to have one set of images be the "background image" of a gridlayout and other images be the "content" of the gridlayout?

If not, what's the best way to do it?

Upvotes: 3

Views: 6094

Answers (2)

trashgod
trashgod

Reputation: 205805

Is it possible to have the background image resize with the rest?

Yes, drawImage() can scale to the full size of the container, as shown here.

Is it possible to set the background image of each cell?

Yes, getSubimage() is helpful in this context, as shown here.

Upvotes: 4

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Yes it's certainly possible. Make one the background of the JPanel that holds the GridLayout. This can be done by drawing the image in the JPanel's paintComponent method. If you want the cells of the grid to show the background image though, be sure to set their opaque property false. If they're JLabels this is already done by default though.

Edit: For example:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class OverLayImages extends JPanel {
   public static final String BACKGROUND_URL = "http://duke.kenai.com/misc/Bullfight.jpg";
   public static final String CELL_URL = "http://duke.kenai.com/iconSized/penduke-transparent.gif";
   private static final int ROWS = 3;
   private static final int COLS = 4;
   private BufferedImage backgroundImage;
   private BufferedImage cellImage;

   public OverLayImages() throws MalformedURLException, IOException {
      backgroundImage = ImageIO.read(new URL(BACKGROUND_URL));
      cellImage = ImageIO.read(new URL(CELL_URL));
      ImageIcon cellIcon = new ImageIcon(cellImage);
      setBackground(Color.white);

      setPreferredSize(new Dimension(backgroundImage.getWidth(), backgroundImage.getHeight()));

      setLayout(new GridLayout(ROWS, COLS));
      for (int i = 0; i < ROWS; i++) {
         for (int j = 0; j < COLS; j++) {
            JLabel label = new JLabel(cellIcon);
            add(label);
         }
      }
   }

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

   private static void createAndShowUI() {
      JFrame frame = new JFrame("OverLayImages");
      try {
         frame.getContentPane().add(new OverLayImages());
      } catch (MalformedURLException e) {
         e.printStackTrace();
         System.exit(1);
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(1);
      }
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

Upvotes: 6

Related Questions