简体   繁体   English

如何在Java中将油漆组件放置在背景的顶部?

[英]How to put paint component pieces on top of a background in Java?

We have a group project where we must create a turn based, 2 player, grid game for class, ie Chutes and Ladders. 我们有一个小组项目,在这个项目中,我们必须创建基于回合的2人网格上课游戏,即滑道和阶梯。 The whole project is done except for the fact that when we bring the background image up, the players pieces get painted behind the background, and are therefore not visible. 整个项目都完成了,除了以下事实:当我们调出背景图像时,玩家作品会在背景后面绘制,因此不可见。 Is there a way to get the pieces to show up on top of the background image? 有没有办法让片段显示在背景图像的顶部? We are using imageIO for the image, GridLayout for the Grid, and then using PaintComponent to place the pieces on the grid. 我们将imageIO用于图像,将GridLayout用于Grid,然后使用PaintComponent将片段放置在网格上。 When we do not have the background image, the pieces show up on the Grid flawlessly. 当我们没有背景图像时,这些片段会完美地显示在Grid上。

using PaintComponent to place the pieces on the grid 使用PaintComponent将零件放置在网格上

  • Make sure to call the super's paintComponent(...) method first thing in your paintComponent override. 确保在paintComponent覆盖中首先调用上级的paintComponent(...)方法。
  • Make sure that your paintComponent method has no program logic inside of it. 确保您的paintComponent方法内部没有程序逻辑。
  • Sometimes it's better to place your pieces in ImageIcons and the Icons in JLabels, and then place your JLabels on a Grid of JPanels. 有时,最好将作品放置在ImageIcons中,将图标放置在JLabels中,然后将JLabel放置在JPanels网格上。 For example . 例如

For more help, post code and give more details. 如需更多帮助,请提供邮政编码并提供更多详细信息。 To be honest, I'm a little surprised that you haven't even posted your paintComponent method code. 老实说,您甚至还没有发布paintComponent方法代码,我对此感到有些惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM