2

I'm working on my personal project using JAVA swing.

The project is about drawing a map on a window.

enter image description here

The map is zoomable using affinetransform. The problem I'm having here is whenever I zoom in or out the map also shifts instead of zooming in/out on the point of the map that is at the center of the screen

private void updateAT()
{
    Dimension d = panel.getSize();
    int panelW = d.width;
    int panelH = d.height;

    Rectangle2D r = regionList[0].get("Victoria").getShape().getBounds2D();

    scaleX = (panelW/r.getWidth()) * zoom;
    scaleY = (panelH/r.getHeight()) * zoom;

    AffineTransform goToOrigin = AffineTransform.getTranslateInstance(-r.getMinX(), -r.getMinY());

    AffineTransform pan = AffineTransform.getTranslateInstance(panX, panY);

    AffineTransform scaleAndFlip = AffineTransform.getScaleInstance(scaleX, -scaleY);

    //AffineTransform mirror_y = new AffineTransform(1, 0, 0, -1, 0, panelH);

    AffineTransform centre = AffineTransform.getTranslateInstance(panelW/2, panelH/2);
    centre.translate(-((r.getWidth()*scaleX)/2), ((r.getHeight()*scaleY)/2));

    world2pixel.setToIdentity();
    //world2pixel.concatenate(mirror_y);
    world2pixel.concatenate(pan);
    world2pixel.concatenate(centre);
    world2pixel.concatenate(scaleAndFlip);
    world2pixel.concatenate(goToOrigin);
}
Eugene Yu
  • 3,708
  • 4
  • 21
  • 27
  • 1
    I'm not sure if this will fix the issue, but I would work with a single transform and apply each action against it, rather then concatenating it at the end, I would also play around with the ordering...take a look at `AffineTransform#translate` and `AffineTransform#scale` ... That many translations are probably not a great idea either :P – MadProgrammer Oct 23 '12 at 04:39
  • It will be necessary to concatenate a few transforms for this. 1) Move the 'center' to the origin. 2) Scale the size. 3) Shift it back to the original center point. Or much the same as ..beaten by @MadProgrammer :) – Andrew Thompson Oct 23 '12 at 04:40
  • @AndrewThompson My experience with `AffineTransform` is pretty limited (to `getScaledInstance` or `getRotateInstance`), so feel free to elaborate ;) – MadProgrammer Oct 23 '12 at 04:43
  • Huhh.. had never realized that [Port Phillip Bay](https://maps.google.com.au/maps?oe=utf-8&client=firefox-a&q=Port+Phillip+Bay&ie=UTF-8&hq=&hnear=0x6ad5d5bd6735d413:0x2a0456754b3a0ea0,Port+Phillip+Bay&gl=au&ei=pSCGUOv5JamRiQeevIGYCw&ved=0CLQBELYD) was so huge. It looks as if someone took a chunk out of Victoria! – Andrew Thompson Oct 23 '12 at 04:45
  • 1
    Now I actually glance at the snippet I realize you are already aware of concatenation of `AffineTransform` instances as mentioned by @Mad & myself. For better help, post a short, complete example. It will probably need to use a simpler shape than Victoria (e.g. concentric circles). – Andrew Thompson Oct 23 '12 at 05:06
  • Thanks guys. Haven't figured out the problem yet but I'll repost or comment on this one when I figure... – Eugene Yu Oct 23 '12 at 13:41

1 Answers1

4

This example centers and scales a Shape, p3, by

  • translating a point of symmetry to the origin,

  • scaling the Polygon and then

  • translating back to the panel's center.

Note that the concatenated operations are performed in the (apparent) reverse of the declaration order, as discussed here.

at.setToIdentity();
at.translate(w / 2, h / 2);
at.scale(scale, scale);
at.translate(-p3x[5] + 10, -p3y[5]); 
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • This actually worked. Thanks trashgod! I posted two questions and I got answered for both questions from you! thanks heaps :D – Eugene Yu Nov 01 '12 at 20:09