HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Java 2D camera movement and zoom

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
zoomjavacameramovementand

Problem

I would appreciate some help with camera movement and zoom. Is there any easier way with cleaner code, or simply how can I improve it?

  • Window size: 800x600



  • Image size: 1200x900



This is my code so far (you can move with the camera only around the image and use zoom):

```
package game.engine.gui;

import game.engine.Engine;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.math.BigDecimal;

public class Camera {

public Point mouseLocation;
public boolean drag = false;
public boolean isEnabled = true;
public Rectangle view;
public int offsetX = 0;
public int maxOffsetX = 200;
public int maxOffsetXZoom = 200;
public int offsetY = 0;
public int maxOffsetY = 150;
public int maxOffsetYZoom = 150;
public float zoom = 1;
public float maxZoom = 1.2F;
public float minZoom = 0.7F;

public Camera() {
view = new Rectangle(0,0,Engine.window.getWidth(),Engine.window.getHeight());
}

public void draw(Graphics2D g)
{

}

public void update()
{

}

public void mousePressed(MouseEvent e)
{
if(e.getButton()==MouseEvent.BUTTON1)
{
mouseLocation = e.getPoint();
drag = true;
}
}

public void mouseReleased(MouseEvent e)
{
drag = false;
}

public void mouseMoved(MouseEvent e)
{

}

public void mouseDragged(MouseEvent e)
{
if(drag)
{
int newoffsetX = offsetX - (mouseLocation.x - e.getX());
int newoffsetY = offsetY - (mouseLocation.y - e.getY());

if(newoffsetX > maxOffsetXZoom) offsetX = maxOffsetXZoom;
else if(newoffsetX maxOffsetYZoom) offsetY = maxOffsetYZoom;
else if(newoffsetY minZoom) zoom -= 0.05F;
}
zoom = round(zoom, 2);
maxOffsetXZoom = (int) (maxOffsetX * zoom);
maxOffsetYZoom = (int) (maxOffsetY * zoom);
if(offsetX > maxOffsetXZoom) offsetX = maxOffsetXZoom;
else if(offsetX maxOffsetYZoom) offsetY = maxOffsetYZoom;
else if(off

Solution

I'm going to assume your indentation got messed up on posting, but just in case, you should indent all of the code inside of the Camera class an additional time.

First off, I like that you used constants at the top of your code! To ensure that they look and act like constants, you should make them private, static, final, and named in UPPERCASE_FORMAT. Furthermore, I would recommend separating your constants from your class variables (such as mouseLocation and offsetX).

I'm not entirely sure how your functions are being called. It looks like you are implementing MouseMotionListener, so I would definitely add that to your class declaration.

If that is true, then I don't see the purpose of the drag variable. The mouseDragged function is only going to get called if the mouse is actually being dragged, so assigning a boolean when the mouse is pressed and released appears redundant.

Also, I'd recommend considering whether you want to use BigDecimal. If round needs to be fast, then you don't want to use BigDecimal at all. There are other techniques for rounding to the nth digit that can be found here

Context

StackExchange Code Review Q#97020, answer score: 4

Revisions (0)

No revisions yet.