Here is a bit of sample code I have from a map-making GUI I’m working on.
The class containing it is called TileNode, which stores a Tile and other information.
/**
* Draws the TileNode
* @param g Graphics context to draw to
* @param x Location X
* @param y Location Y
*/
public void draw(Graphics2D g, int x, int y)
{
// Draw the tile itself
tile.draw(g, x, y);
// If selected, highlight area of tile
if(state == TileState.SELECTED)
{
Composite oldC = g.getComposite();
g.setColor(Color.WHITE);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g.fillRect(x, y, tile.getWidth(), tile.getHeight());
g.setComposite(oldC);
}
}
This code is supposed to highlight a tile when it is in the selected state.
When I run my program, and I continue calling this code, my memory climbs very very fast. The strange thing, when I don’t call g.fillRect(), or I set my alpha composite’s alpha to 1, this does not happen. The graphics component is eventually disposed of, but that does not seem to help. I am very confused…
Any insight?
Edit: It also makes no difference if I call g = (Graphics2D)g.create() at the top, and g.dispose() at the bottom of the function. Still memory hungry.