gpt4 book ai didi

java - 将两个 Activity JPanel 层叠在一起

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:19 26 4
gpt4 key购买 nike

我正在尝试创建一个简单的动画,让游泳者沿着水下路径行走。我有两个 JPanel - 一个用于游泳者,一个用于背景。两者都是活跃的,游泳者正在移动其四肢,背景有需要更新的鱼/障碍物。

我试图将游泳者放在背景之上。我正在尝试使用 JLayeredPane,因此我创建了两个图层并添加它们:

JLayeredPane lp = frame.getLayeredPane();
lp.setPreferredSize(new Dimension(650, 550));

underwaterScene.setSize(lp.getPreferredSize());
underwaterScene.setLocation(0,0);

swimmer.setSize(lp.getPreferredSize());
swimmer.setLocation(0,0);
swimmer.setOpaque(false);

lp.add(underwaterScene, Integer.valueOf(1));
lp.add(swimmer, Integer.valueOf(2));

游泳者的paintComponent方法只是将游泳者绘制在正确的位置:

g.drawImage(swimmer.sprite, swimmer.x, swimmer.y, 150, 100, null);  

目前,我只看到游泳者层。如果我删除 lp.add(swimmer ... 调用,那么我可以看到背景层,所以我知道它正在被添加。

我的问题是:为什么游泳者没有完全遮挡背景而出现在背景之上?

预先感谢您的帮助!

最佳答案

这是一个工作示例。

此外,您可以在此处找到其上下文: https://github.com/fzoli/RemoteControlCar/blob/master/desktop/BrowserTest/src/ConstrainVisibility.java#L58

public class ConstrainVisibility extends JPanel {

protected static final String LS = System.getProperty("line.separator");
protected static final int OFFSET_X = 150;
protected static final int OFFSET_Y = 120;
protected static final int WIDTH = 200;
protected static final int HEIGHT = 200;

public ConstrainVisibility() {
super(new BorderLayout());
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setOpaque(true);
layeredPane.setBackground(new Color(200, 200, 255));
int layerIndex = 0;
// A flash player

// A swing panel
JPanel swingPanel = new JPanel();
swingPanel.setBorder(BorderFactory.createTitledBorder("Swing JPanel"));
swingPanel.setBackground(Color.GREEN);
swingPanel.setBounds(OFFSET_X * layerIndex, OFFSET_Y * layerIndex, WIDTH, HEIGHT);
layeredPane.setLayer(swingPanel, layerIndex++);
layeredPane.add(swingPanel);
// A web browser
JWebBrowser webBrowser = new JWebBrowser(JWebBrowser.constrainVisibility());
webBrowser.setBarsVisible(false);
webBrowser.setStatusBarVisible(true);
webBrowser.setHTMLContent(
"<html>" + LS +
" <body>" + LS +
" <h1>A web page</h1>" + LS +
" <p>A paragraph with a <a href=\"http://www.google.com\">link</a>.</p>" + LS +
" </body>" + LS +
"</html>");
webBrowser.setBounds(OFFSET_X * layerIndex, OFFSET_Y * layerIndex, WIDTH, HEIGHT);
layeredPane.setLayer(webBrowser, layerIndex++);
// A swing button
JButton swingButton = new JButton("Swing JButton");
swingButton.setBounds(OFFSET_X * layerIndex, OFFSET_Y * layerIndex, WIDTH, HEIGHT);
layeredPane.setLayer(swingButton, layerIndex++);
layeredPane.add(swingButton);
layeredPane.add(webBrowser);
layeredPane.setPreferredSize(new Dimension(WIDTH + OFFSET_X * (layerIndex - 1), HEIGHT + OFFSET_Y * (layerIndex - 1)));
add(new JScrollPane(layeredPane), BorderLayout.CENTER);
}

/* Standard main method to try that test as a standalone application. */
public static void main(String[] args) {
UIUtils.setPreferredLookAndFeel();
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("DJ Native Swing Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ConstrainVisibility(), BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
}

}

关于java - 将两个 Activity JPanel 层叠在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35896752/

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