0

I have a JPanel, on which I draw multiple custom shapes (logical circuits and similar). I need to implement a zoom in and zoom out feature, which would zoom all the elements on the canvas.

I tried using scale() method on Graphics2D object, and the elements are getting resized properly, but the problem is that they are moving away from their original position with each zoom. I am aware that is happening because the coordinates are getting multiplied with scale factor and I guess that I am supposed to counter that with a translation of some sort, but I am not sure how. So basically, I suppose I should do something like:

    g2d.scale(scalefactor,scalefactor);
    g2d.translation(value1, value2);

    //draw elements

However, I am not sure how to determine those values for value1 and value2, which are used for translation.

Any idea?

  • 1
    Why do you not want the position to be scaled too? If you zoom in, the relative distance between objects should stay the same, so if you skip scaling positions, the result will simply be wrong. – isnot2bad Jan 12 '14 at 20:02
  • What I've done in the past is to scale the position as well, for [example](http://stackoverflow.com/questions/21070703/how-to-properly-refresh-image-in-jframe/21070799#21070799) – MadProgrammer Jan 12 '14 at 20:11
  • Well, the main reason why I dont want positions to be scaled is because I also have to be able to select those elements. I already have selection method coded, which works fine before scaling. But once I start scaling, selection no longer works :/ – Ognjen Koprivica Jan 12 '14 at 20:17

0 Answers0