我正在尝试创建一个简单的动画,游泳者沿着水下路径行进。我有两个JPanels - 一个用于游泳者,一个用于背景。两者都很活跃,游泳者正在移动四肢,背景中有鱼/障碍需要更新。
我想把游泳运动员放在背景之上。我正在尝试使用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(游泳者......那么我可以看到背景图层,所以我知道它正在被添加。
我的问题是:为什么没有完全阻挡游泳者出现在背景之上?
提前感谢您的帮助!
答案 0 :(得分:0)
这是一个有效的例子。
另外,你可以在这里找到它的背景: 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();
}
}