You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by "G. Wade Johnson" <wa...@abbnm.com> on 2003/05/20 23:35:42 UTC

Zoom*Action classes

I needed to build a JSVGCanvas with a different zoom factor on the
keystroke events. This turned out not to be too difficult by replacing
the appropriate entries in the ActionMap.

However, I did find it a little annoying that I had to duplicate all of
the code in ZoomInAction/ZoomOutAction in order to do this.

I'd like to recommend that this code be refactored slightly. The above
two classes could be replaced with:

    protected class ZoomAction extends AbstractAction {
        double      scale_  = 1;
        ZoomAction( double scale ) {
            scale_  = scale;
        }
        public void actionPerformed(ActionEvent evt) {
            if (gvtRoot == null) {
                return;
            }
            AffineTransform at = getRenderingTransform();
            if (at != null) {
                Dimension dim = getSize();
                int x = dim.width / 2;
                int y = dim.height / 2;
                AffineTransform t =
AffineTransform.getTranslateInstance(x, y);
                t.scale(scale_, scale_);
                t.translate(-x, -y);
                t.concatenate(at);
                setRenderingTransform(t);
            }
        }
    }

This allows replacing "new ZoomInAction()" with "new ZoomAction(2)" and
"new ZoomOutAction()" with "new ZoomAction(0.5)".

It removes some redundant code and allows me to reuse library code
without using the cut-and-paste reuse anti-pattern.

Thanks,
G. Wade

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org