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 ar开发者_C百科ray 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.
If you draw a rectangle with 50% alpha and then draw another one, the second one will override it instead of blending with it.
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 stuff I was trying to composite was on the same
Component
.
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.
精彩评论