Performance improvement for desklet container, using default background (GradientBackground)

4 views
Skip to first unread message

Augusto Sellhorn

unread,
Mar 14, 2007, 9:25:47 PM3/14/07
to Desklets Development
I don't know about your machines, but I have a pretty fast one with a
good video card and under WinXP, dragging desklets inside the
container was a bit slower than expected. Looking at the code, it
seems like it's doing all the drawing and painter stuff over and over,
so I just drew it to a back buffer and painted that instead. The whole
application seems a lot faster and more responsive, at least when I
drag desklets around.

The modification is simple, of course I'd like to see if anybody else
notices the perf. difference ...


src/java/ab5k/backgrounds/GradientBackgroud.java

+-------------------------------------------------------------------------
/**
*
* @author joshy
*/
public class GradientBackground extends DesktopBackground {
BufferedImage background;

+------------------------------------------------------------------------

public void paint(Graphics2D gc) {
if (getDesktopPane() == null) {
return;
}

if (background == null || background.getWidth() !=
desktopPane.getWidth() ||
background.getHeight() != desktopPane.getHeight()) {

background =
desktopPane.getGraphicsConfiguration().createCompatibleImage(
desktopPane.getWidth(),
desktopPane.getHeight());

Graphics2D g = background.createGraphics();

// the background
g.setPaint(new GradientPaint(new Point(0,0),
new Color(190,190,204),
new Point(desktopPane.getWidth(),
desktopPane.getHeight()),
new Color(50,50,204)));
g.fillRect(0, 0, desktopPane.getWidth(),
desktopPane.getHeight());


PinstripePainter pin = new PinstripePainter(new
Color(50,50,204,30), 45.0,8.0,8.0);
Graphics2D gfx = (Graphics2D) g.create();

if (gfx.getClip() == null) {
gfx.setClip(0, 0, desktopPane.getWidth(),
desktopPane.getHeight());
}

pin.paint(gfx, desktopPane, desktopPane.getWidth(),
desktopPane.getHeight());
gfx.dispose();

// the text
String text = "AB5k";
g.setColor(new Color(255,255,255,30));
g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,400));
FontMetrics metrics = g.getFontMetrics();
Rectangle2D bounds = metrics.getStringBounds(text,g);
g.translate(desktopPane.getWidth()/
2,desktopPane.getHeight()/2);
g.translate(-(bounds.getX()+bounds.getWidth())/2,
0-bounds.getY()-bounds.getHeight()/2);
g.drawString(text,0,0);
g.dispose();
}

gc.drawImage(background, 0, 0, desktopPane);
}

+-------------------------------------------------------------

Joshua Marinacci

unread,
Mar 15, 2007, 1:00:47 AM3/15/07
to desklets-c...@googlegroups.com
Thanks Augusto. Buffering does indeed help, though at a cost of memory.  I plan to rewrite the container complete to properly use buffered images for each of the desklets. That way they don't have to be constantly redrawn when the background is. That should be done in the next week or so. First I have to get the JavaOne presentation done. They are asking for it in the next few days.

- J
- Blasting forth in three part harmony!


Reply all
Reply to author
Forward
0 new messages