使用java图形API绘制图层
我正在做一个模拟器项目,测试几种基于 A* 的算法并展示它们的工作原理和结果。 这些算法都是多智能体的,并且在网格地图环境上运行。
我使用 JPanel 作为网格,其中包含一个二维 Cells 数组,其中每个 Cell 都是一个自定义类,它扩展了 Component 类,并使用 Paint 方法在每个单元格内绘制我需要的内容。 对于单元格内的绘图,我使用 Graphics.fillRect 或 Graphics.drawImage 等方法用特定颜色或图标填充每个单元格。
我使用一个特殊的图标来表示网格上每个代理的起始位置和目标位置。 我的问题是我希望能够在同一单元格上绘制多个项目。
例如,我希望能够通过以特殊颜色沿着路径绘制单元格来显示其中一个代理的路径,并且该路径可能会经过不同代理的起始位置,所以我希望能够填充具有颜色的单元格,并在顶部绘制了一个图标。 在另一个示例中,我希望能够使用 alpha 混合来混合两种颜色。
如果我将 Graphics.fillRect() 与具有 alpha 的一种颜色一起使用,然后再次使用具有 alpha 值的不同颜色,则它将不起作用,因为最后一个 fillRect() 将覆盖第一个调用。
有没有一种方法可以使用我创建的相同单元组件来实现我所需要的,或者我应该以不同的方式实现它? 也许这个问题有更好的解决方案? 我非常感谢有关此事的任何建议。
I'm doing a simulator project that tests several A* based algorithms and show how they work and their results.
The algorithms are all multi-agent and run on a grid map environment.
I used a JPanel for the grid which contains a two dimensional array of Cells where each Cell is a custom class that extends the Component class and use the paint method to draw the stuff i need inside each cell.
For the drawing inside the cell I use method such as Graphics.fillRect or Graphics.drawImage to fill each cell with a certain color or icon).
I'm using a special Icon for the start position and goal position of every agent on the grid.
My problem is that I want to be able to draw more than one item on the same cell.
For example I want to be able to show the path of one of the agents by painting the cells along the path in a special color and the path might go through a start position of a different agent, so I want to be able to fill the cell with the color and have an icon drawn on top.
In another example I want to be able to mix two colors using alpha blending.
If I use graphics.fillRect() with one color that has alpha and then use it again with a different color with alpha value it won't work since the last fillRect() will override the first call.
Is there a way I can achieve what I need using the same Cell Component I created or should I implement it differently?
Perhaps there is a better solution to this problem?
I would really appreciate any advice on this matter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于模式。这个方便的实用程序显示了使用
AlphaComposite
。可用的源代码可能会为您的项目提供一些见解。附录:
示例 引用 正是这样做的,就像这个 示例。如果
AlphaComposite
不满足您的要求,您可以随时改变色调、饱和度和/或明度;这个示例组成了一个基于饱和度的颜色表。It depends on the mode. This convenient utility shows the result of blending different colors using the modes defined in
AlphaComposite
. The available source code may offer some insights for your project.Addendum:
The example cited does exactly this, as does this example. If
AlphaComposite
does not meet your requirements, you can always vary hue, saturation and/or value; this example composes a color table based on saturation.