Chet
Chet

Reputation: 1269

Swing image display optimization

I am displaying an image in Swing. I am drawing on top of the image (a bunch of drawRect() calls) and refreshing the screen. The image is constant, but the objects drawn on top are not. Is there any way to avoid redrawing the image any time? Since the graphics card likely does the image display, is it safe to assume that the drawRect() calls are the bottleneck? I draw as many as 20,000 calls a frame (but usually no more than 3000).

Edit: It is indeed the rect calls that are slowing it down and it can be made considerably faster by removing the transparency channel. That being said, it would still be nice to speed it up and include the transparency. The code can't really get simpler, so I am hoping by doing something different it will help.

public void paintComponent(Graphics g) {
    super.paintComponent(g) ;
    //grid or walkers
    g.drawImage(image, 0, 0, null);

    for(Walker w : walkArray){
        g.setColor(new Color(255,255-w.data[3], 0, w.data[2]));
        g.drawRect(w.data[0], w.data[1], 1, 1);
    }       
}

Upvotes: 4

Views: 196

Answers (1)

dinox0r
dinox0r

Reputation: 16039

This is out of context, but can you lookup the colors in a precomputed palette instead of creating an instance of a Color in every cycle? Maybe that could improve performance a little.

Edit: For example, a List<Integer> is used as an RGB lookup table here, and a Queue<Color> is used here.

Upvotes: 2

Related Questions