使用Java图形API绘制图层

使用Java图形API绘制图层

本文介绍了使用Java图形API绘制图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个模拟器项目,该项目测试几种基于A *的算法,并展示它们如何工作以及它们的结果.这些算法都是多智能体,并且在网格地图环境中运行.

我为网格使用了一个JPanel,它包含单元的二维数组,其中每个单元是一个自定义类,扩展了Component类,并使用paint方法在每个单元内绘制我需要的东西.对于单元格内部的绘图,我使用Graphics.fillRect或Graphics.drawImage之类的方法为每个单元格填充特定的颜色或图标.

我在网格上为每个座席的开始位置和目标位置使用特殊的图标.我的问题是我希望能够在同一单元格上绘制多个项目.

例如,我希望能够通过以特殊颜色绘制路径上的单元格来显示其中一个代理的路径,并且该路径可能会经过其他代理的起始位置,因此我希望能够用颜色填充单元格并在顶部绘制一个图标.在另一个示例中,我希望能够使用alpha混合将两种颜色混合.

如果我使用带有alpha值的一种颜色的graphics.fillRect(),然后再次使用具有alpha值的另一种颜色,则它将不起作用,因为最后一次fillRect()将覆盖第一个调用.

有没有一种方法可以使用我创建的相同单元组件来实现所需的功能,还是应该以不同的方式实现它?也许对这个问题有更好的解决方案?我真的很感谢在此问题上的任何建议.

解决方案

这取决于模式.这种方便的实用程序显示了使用.可用的源代码可能会为您的项目提供一些见识. >

附录:

示例 引用就是这样做的,.如果 AlphaComposite 不符合您的要求根据要求,您可以随时更改色相,饱和度和/或值;此示例组成了一个基于饱和度的颜色表.

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.

解决方案

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.

这篇关于使用Java图形API绘制图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:24