I am creating a randomized "solar system" model (which I have mentioned in another question) and I'm trying to zoom in on the center of the screen. Here's the code i use to zoom in / out:
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_PLUS){
Camera.zoom++;
}
if(e.getKeyCode() == KeyEvent.VK_MINUS && Camera.zoom > 1){
Camera.zoom--;
}
}
Here is the code I use to render the planets, orbits and stars:
public void render(Graphics g){
//Draw orbit
g.setColor(Settings.ORBIT_COLOR);
g.drawOval((star.x - orbitRadius - Camera.xOffset) * Camera.zoom, (star.y - orbitRadius - Camera.yOffset) * Camera.zoom, (orbitRadius * 2) * Camera.zoom, (orbitRadius * 2) * Camera.zoom);
//Draw planet
g.setColor(color);
g.fillOval((x - radius - Camera.xOffset) * Camera.zoom, (y - radius - Camera.yOffset) * Camera.zoom, (radius * 2) * Camera.zoom, (radius * 2) * Camera.zoom);
//Render star
g.setColor(color);
g.fillOval((x - radius - Camera.xOffset) * Camera.zoom, (y - radius - Camera.yOffset) * Camera.zoom, (radius * 2) * Camera.zoom, (radius * 2) * Camera.zoom);
}
int Camera.zoom = 1;
int Camera.xOffset/yOffset are what I use to move the camera around.
My problem here is that this code zooms the camera in on the top left corner, not the middle, and I've tried adding to x and y offsets half of the window's width and height, respectively, but that didn't work either.