在 Java 中的 GridLayout 上覆盖图像

发布于 2024-11-13 22:58:51 字数 70 浏览 8 评论 0原文

是否可以将一组图像作为网格布局的“背景图像”,而其他图像作为网格布局的“内容”?

如果没有,最好的方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荭秂 2024-11-20 22:58:51

是的,这当然有可能。将其中之一作为保存 GridLayout 的 JPanel 的背景。这可以通过在 JPanel 的 PaintComponent 方法中绘制图像来完成。如果您希望网格的单元格显示背景图像,请务必将其不透明属性设置为 false。如果它们是 JLabels,则默认情况下已完成此操作。

编辑:例如:

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();
         }
      });
   }
}

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();
         }
      });
   }
}
小草泠泠 2024-11-20 22:58:51

是否可以让背景图像与其余图像一起调整大小?

是的,drawImage()可以缩放到容器的完整大小,如图此处

是否可以设置每个单元格的背景图像?

是的,getSubimage() 在这种情况下很有帮助,如图所示 这里

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文