gpt4 book ai didi

java - 多个 JPanel 完全重叠

转载 作者:搜寻专家 更新时间:2023-11-01 01:43:55 26 4
gpt4 key购买 nike

我正在创建一个程序,我可以在其中绘制 map 并向其中添加不同的道路等。我计划在一个 jpanel 上添加 map 地形,在另一个 jpanel 上添加道路等,彼此叠加。但我不能让他们工作。我不知道如何将多个 jpanel 完全叠加在一起并使它们都能够绘制。

最佳答案

解决这个问题的基本方法是使用 JLayeredPane 和类似 GridBagLayout 的东西。

JLayeredPane 可以让您更好地控制各个层的 z 深度,GridBagLayout 可以让您根据需要将组件相互叠加。

你也可以看看 OverlayoutLayout ,但从未使用过它,我无法发表评论。

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MapLayers {

public static void main(String[] args) {
new MapLayers();
}

public MapLayers() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

MapPane mapPane = new MapPane();
try {
mapPane.addLayer(new ImageLayer(ImageIO.read(new File("Ponie.png")), 360, 10));
mapPane.addLayer(new ImageLayer(ImageIO.read(new File("Layer01.png")), 0, 0));
} catch (IOException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(mapPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class ImageLayer extends JComponent {

private Image bg;
private int xOffset;
private int yOffset;

public ImageLayer(Image image, int x, int y) {
bg = image;
xOffset = x;
yOffset = y;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bg != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(bg, xOffset, yOffset, this);
g2d.dispose();
}
}

}

public class MapPane extends JLayeredPane {

private BufferedImage bg;

public MapPane() {
try {
bg = ImageIO.read(new File("PirateMap.jpg"));
} catch (IOException exp) {
exp.printStackTrace();
}
setLayout(new GridBagLayout());
}

public void addLayer(JComponent layer) {

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;

add(layer, gbc);

}

@Override
public Dimension getPreferredSize() {
return bg == null ? new Dimension(200, 200) : new Dimension(bg.getWidth(), bg.getHeight());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bg != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - bg.getWidth()) / 2;
int y = (getHeight()- bg.getHeight()) / 2;
g2d.drawImage(bg, x, y, this);
g2d.dispose();
}
}
}
}

看看How to use LayeredPanes了解更多详情

关于java - 多个 JPanel 完全重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17962171/

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