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?