I make a simple grid, each square of which is highlighted with a cursor: 
This is a pair of JPanels, mapgrid and overlay inside a JLayeredPane, with a mapgrid at the bottom. Mapgrid just draws when initializing the grid; its drawing method looks like this:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
g2d.setColor(new Color(128, 128, 128, 255));
g2d.drawRect(tileSize * j, i * tileSize, tileSize, tileSize);
}
}
In the JPanel overlay, a selection occurs, this is what redraws when you move the mouse:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(new Color(255, 255, 128, 255));
g2d.drawRect((pointerX/tileSize)*tileSize,(pointerY/ tileSize)*tileSize, tileSize, tileSize);
}
I noticed that although the base layer (mapgrid) is NOT repainted when moving the mouse, only a transparent overlay layer, performance is not enough. If I overlay the background of the JPanel overlay, it will be faster. If I remove anti-aliasing, anti-aliasing is also a little faster.
I do not know why setting the background to the overlay (and therefore hiding the grid) or disabling anti-aliasing in the grid leads to much better performance.
? ?