The Source for Java Technology Collaboration
User: Password:



David Walend

David Walend's Blog

Affine Frustration Transformed - New! Fewer Bugs!

Posted by dwalend on January 26, 2006 at 06:05 PM | Comments (6)

I put together a generic ZoomPane that holds other Swing components. Hand ZomePane's transformChanged() method a new AffineTransform to show a new portion of the underlying view. ZoomPane uses a little linear animation to glide into the new view gracefully.

I set up ZoomButtonPane as a simple interface. (I want to play with something more complex and transparent. It's been a while since I've worked with Swing, so I wanted to warm up with some easy buttons.) ZoomButtonPane hands off AffineTransforms to a ZoomPane.

Panning worked great. Zooming in and out was a pain to get right. I found similar old questions in some forums, with no answers. I asked for help in other forums, and got a few responses that pointed to example code. Unfortunately, none of the examples solved the whole problem; in each case, some controller held on to some of the state information used by ZoomPane. I figured out how to get those details from just the old transform and size of the window.


    private void zoom(double newScale)
    {
        AffineTransform oldDestinationTransform = zoomPane.getCopyOfDestionationTransform();

//Unwind the centering from the old transform.
        double centerX = zoomPane.getWidth()/2;
        double centerY = zoomPane.getHeight()/2;

        double oldScale = oldDestinationTransform.getScaleX();

        oldDestinationTransform.translate(centerX,centerY);
        oldDestinationTransform.scale(1.0/oldScale,1.0/oldScale);
        oldDestinationTransform.translate(-centerX,-centerY);
        
        oldDestinationTransform.translate((oldDestinationTransform.getTranslateX()/oldScale)
                                            -oldDestinationTransform.getTranslateX(),
                                            (oldDestinationTransform.getTranslateY()/oldScale)
                                            -oldDestinationTransform.getTranslateY());
        
        AffineTransform newDestinationTransform = new AffineTransform();

        newDestinationTransform.translate(centerX,centerY);
        newDestinationTransform.scale(newScale,newScale);
        newDestinationTransform.translate(-centerX,-centerY);

        newDestinationTransform.concatenate(oldDestinationTransform);
        
        zoomPane.transformChanged(newDestinationTransform);
    }

Here's an applet showing it working:

ZoomApplet

Kick the code around yourself at https://tokenarranger.dev.java.net.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • If your project public? I can't access the applet's jar nor the project page.

    Posted by: gfx on January 26, 2006 at 08:22 PM

  • What version of the JRE do I need to run this applet? Both with 1.5.0_06-b05 and 1.4.2_05-b04 I get some exception..

    1.4: java.lang.ClassFormatError: net/walend/zoom/ZoomApplet (Bad magic number)

    1.5: java.lang.ClassFormatError: Incompatible magic value 169871904 in class file net/walend/zoom/ZoomApplet

    [rant]Oh, and the text format system thingy for these comments sucks, IMHO.[/rant]

    Posted by: arnovdk on January 27, 2006 at 01:01 AM

  • Affine Frustration Transformed to Applet Frustration...

    Gfx, looks like java.net's not letting my .jar out. I've sent email to their support team to sort things out.

    arnovdk, both of those are responses to an http 404 response, consistent with gfx's report. It's a JDK5 applet. If a 1.4 JVM loads the class, it'll give you a "ClassFormatError: Unsupported Version 49."

    I've gotten it to work on Safari (MacOS) and IE v6 (Windows), but not Firefox. Does Firefox not do applet tags?

    [rant]Amen, brother! How about straight [pre] text assumed?[/rant]

    Posted by: dwalend on January 27, 2006 at 05:13 AM

  • Got the applet working. Looks like the problem was that my tokenarranger project isn't public yet. So everything came to everyone else as 404, but it all worked for me after I logged in.

    I'm not sure what to do about tokenarranger not being public. I moved the applet and picture to a different spot, and it works fine. At least on Safari... Hate that "new fewer bugs" release.

    Posted by: dwalend on January 28, 2006 at 06:50 AM

  • I'd love to "kick the code around" myself - but while the applet now works, the site ( https://tokenarranger.dev.java.net ) does not work for a mere member of the public, regardless of logging in. Could you post the source jar/zip/whatever file somewhere offsite while that gets worked out?

    Posted by: kylecordes on January 29, 2006 at 12:59 PM

  • kylecordes, Looks like they've stopped hiding the project. Let me know if you can't see it. I'll try switching the applet back over in a few minutes.

    Posted by: dwalend on January 31, 2006 at 05:00 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds