Changing the "origin point" so it becomes the center of the "zoom" is basically just the process of subtract half of d
from the center point.
So, assuming the the center point is 28
((25 / 2) + 15
), you would simply then subtract d / 2
(25 / 2) from this point, 28 - (25 / 2) = 15
or near enough...
I modified the paintComponent
method for testing, so the rectangle is always at the center of the panel, but you can supply arbitrary values in place of the originX
and originY
@Override
public void paintComponent(Graphics g)//paints circle on the screen
{
super.paintComponent(g); //prepares graphic object for drawing
int originX = getWidth() / 2;
int originY = getHeight() / 2;
int x = originX - (d / 2);
int y = originY - (d / 2);
System.out.println(x + "x" + y);
g.fillRect(x, y, d, d); //paints rectangle on screen
//x , y, width, height
}
As for scaling an image, you should look at Graphics#drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
, beware though, this will scaling the image to the absolute size, it won't keep the image ratio.
A better solution might be to use a double value of between 0 and 1 and multiple the various elements by this value to get the absolute values you want