2010-11-20-0942Z


MrCreditRepair.biz will fix your credit rating!

I'd never really taken the time before to learn how to scale an image in Java. In fact, I didn't know what to put for the ImageObserver argument and always used this. But my scaled images would flicker.

So, since my customer wanted it done, I looked some things up, and found that you can pass null for the ImageObserver. And you can use an extended form of graphics.drawImage() and avoid having to specify a filter. In fact, to draw an image that fits the size you allocated to an applet, all you really need to do is to replace

 public void paint(Graphics graphics) {
  if (fingerprint != null) {
   graphics.drawImage(fingerprint, 0, 0, null);
  }
 }

with:

 public void paint(Graphics graphics) {
  if (fingerprint != null) {
   double ratio = (double)this.getWidth() / fingerprint.getWidth(null);
   int targetHeight = (int)(ratio * fingerprint.getHeight(null));
   graphics.drawImage(fingerprint, 0, 0, this.getWidth(), targetHeight, null);
  }
 }

This particular example might truncate the height, which doesn't matter to me. If you want to make sure you get the whole thing, you'll have to determine which applet dimension is smaller, use that as the basis for the ratio, and then center the image accordingly.

Back to blog or home page

last updated 2010-11-20 05:03:21. served from tektonic