gpt4 book ai didi

java - JLayeredPane 图像未显示

转载 作者:行者123 更新时间:2023-11-30 06:10:01 32 4
gpt4 key购买 nike

我有一个图像,我想要的只是将此图像打包到 JLabel 中,并将其添加到我的 JLayeredPane 中,以便稍后对多个图像进行分层。它可以在没有 JLayeredPane 的情况下处理一张图像,但是当使用 JLayeredPane 时,我只能看到边框。我必须对多个图像进行分层,以便 ypu 可以配置披萨并查看您定制的披萨。

package view;

import javax.swing.*;
import java.awt.*;

/**
* ImagePanel used to scale and display an image
*/

public class ImagePanel extends JPanel {
private JLayeredPane layeredPane;

/**
* Setup an ImagePanel, get the image, scale it and set it as a label to display it
*/
public ImagePanel(){

layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 300));
layeredPane.setBorder(BorderFactory.createTitledBorder("Your Pizza"));
ImageIcon icon = new ImageIcon(getClass().getResource("/images/pizzaboden.png"));
JLabel imgLabel = new JLabel(icon);
layeredPane.add(imgLabel);
add(layeredPane);

}
}

读取图像有效,这是我之前读取的方式,效果很好:

我的数据字段

private ImageIcon imageIcon;
private ImageIcon scaledImageIcon;
private JLabel imageLabel;
private Image image;
private Image scaledImage;

我的构造函数

imageIcon = new ImageIcon(getClass().getResource("/images/pizzaboden.png"));
image = imageIcon.getImage();
scaledImage = image.getScaledInstance(280, 280, Image.SCALE_SMOOTH);
scaledImageIcon = new ImageIcon(scaledImage);
imageLabel = new JLabel(scaledImageIcon);
add(imageLabel);

What I get

最佳答案

当您向 JLayeredPane 添加某些内容时,您需要指定在 X/Y 维度中添加组件的位置以及在分层维度中的位置,如 JLayeredPane tutorial 所示。 ,而你不这样做。您还需要为 JLabel 指定一个大小,因为 JLayeredPane 不考虑它的首选大小。类似的东西

imgLabel.setSize(imgLabel.getPreferredSize());
imgLabel.setPosition(0, 0);
layeredPane.add(imgLabel, JLayeredPane.DEFAULT_LAYER);

可能就足够了。

此外,您是否独立于当前程序进行了测试,以确保正确读取图像?另外,您正在使用 JPanel 添加到 FlowLayout - 您确定要执行此操作吗?也许更好的办法是给你的ImagePanel一个BorderLayout。

<小时/>

请注意,如果我正在做这样的事情,在 Swing GUI 中显示多个重叠图像,我不会使用 JLayeredPane,而是会在 paintComponent(Graphics g) 中绘制图像 (BufferedImages) 单个绘图 JPanel 的方法,我会在需要时从 GUI 添加和删除图像,然后在绘图 JPanel 上调用 repaint()

例如,如果您为绘图 JPanel 提供一个图像的 ArrayList,如下所示:

private List<Image> images = new ArrayList<>();

您可以通过在paintComponent方法中迭代列表来绘制此列表保存的任何图像:

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Image image : images) {
g.drawImage(image, 0, 0, this);
}
}

您可以轻松添加和删除图像:

public void clearImages() {
images.clear();
repaint();
}

public void removeImage(Image image) {
images.remove(image);
repaint();
}

public void addImage(Image image) {
images.add(image);
repaint();
}

例如,您可以运行以下概念测试代码来演示我的意思:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;

public class MultipleImages extends JPanel {
private static final String BULLS_EYE_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/thumb/3/36/Roundel_of_the_Serbian_Air_Force_1915.svg/"
+ "300px-Roundel_of_the_Serbian_Air_Force_1915.svg.png";
private static final String CHINESE_CHAR_PATH = "https://upload.wikimedia.org/"
+ "wikipedia/commons/1/1a/%E9%9D%91-red.png";
private static final String HOCKY_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/thumb/e/eb/Ice_hockey_pictogram.svg/"
+ "300px-Ice_hockey_pictogram.svg.png";
private static final String[] LABELS = {"Bulls Eye", "Chinese Character", "Hockey"};
private static final String[] IMAGE_PATHS = {BULLS_EYE_PATH, CHINESE_CHAR_PATH, HOCKY_PATH};
private Map<String, Image> imageMap = new HashMap<>();
private List<JCheckBox> checkBoxes = new ArrayList<>();
private ImagePanel imagePanel = new ImagePanel();

public MultipleImages() throws IOException {
for (int i = 0; i < IMAGE_PATHS.length; i++) {
URL imgUrl = new URL(IMAGE_PATHS[i]);
BufferedImage img = ImageIO.read(imgUrl);
imageMap.put(LABELS[i], img);
}

ActionListener checkBoxListener = e -> {
imagePanel.clearImages();
for (JCheckBox checkBox : checkBoxes) {
if (checkBox.isSelected()) {
String label = checkBox.getActionCommand();
imagePanel.addImage(imageMap.get(label));
}
}
imagePanel.repaint();
};
JPanel checkBoxPanel = new JPanel(new GridLayout(0, 1));
for (String label : LABELS) {
JCheckBox checkBox = new JCheckBox(label);
checkBox.setActionCommand(label);
checkBoxes.add(checkBox);
checkBoxPanel.add(checkBox);
checkBox.addActionListener(checkBoxListener);
}
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.add(checkBoxPanel, BorderLayout.PAGE_START);

setLayout(new BorderLayout());
add(rightPanel, BorderLayout.LINE_START);
add(imagePanel);
}

private static void createAndShowGui() {
JFrame frame = new JFrame("Multiple Images");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
frame.getContentPane().add(new MultipleImages());
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

public class ImagePanel extends JPanel {
private static final int PREF_W = 300;
private static final int PREF_H = PREF_W;
private static final Color BACKGROUND = Color.WHITE;
private List<Image> images = new ArrayList<>();

public ImagePanel() {
setBackground(BACKGROUND);
setBorder(BorderFactory.createLineBorder(Color.BLACK));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Image image : images) {
g.drawImage(image, 0, 0, this);
}
}

public void clearImages() {
images.clear();
repaint();
}

public void removeImage(Image image) {
images.remove(image);
repaint();
}

public void addImage(Image image) {
images.add(image);
repaint();
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}

关于java - JLayeredPane 图像未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50456806/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com