gpt4 book ai didi

java - 将 BufferedImage 添加到 JLayerPane 上的 JPanel

转载 作者:行者123 更新时间:2023-11-30 09:24:32 26 4
gpt4 key购买 nike

所以,我有一个 JLayeredPane(从技术上讲,实际上是 JLayeredPane 的子类)。上面是一个 JPanel。我想将 BufferedImage 添加到 Jpanel。

public class BigMap extends JLayeredPane implements MouseListener
JPanel mapPanel;
BufferedImage theMap;
public BigMap (BufferedImage m){
theMap = m;
mapPanel = new JPanel();
add(mapPanel, 0);
mapPanel.setBounds(0, 0, 640, 640);
//other unimportant stuff
}

@Overrride
public void paintComponent (Graphics g){
super.paintComponent(g);
Graphics2D gmap = (Graphics2D) mapPanel.getGraphics();
gmap.drawImage(theMap, null, 0, 0);
//some other stuff which is working just fine
}

问题是 BufferedImage 没有显示。 JPanel 肯定存在,因为我可以设置它的 backgroundColour 并根据需要查看它。我意识到 JLayeredPane 没有布局管理器,必须为 JPanel 设置边界,但这对 JPanel 本身来说应该不是问题,对吧?鉴于 BufferedImage 缺乏直接控制其大小的方法,我不明白如果它是的话我将如何克服它。

感谢任何帮助。

最佳答案

这里的问题是您覆盖了分层面板的 paintComponent() 方法,而不是 JPanelJPanel 稍后将自己绘制,作为分层 Pane 的子项之一,这将清除您绘制的内容。

一般来说,paintComponent() 方法应该绘制到提供给它的 Graphics 中,而不是绘制到其他组件的图形中。

关于java - 将 BufferedImage 添加到 JLayerPane 上的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15644686/

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