Mouse wheel scrolling problem.

Hey guys, having trouble with a zoom method, can’t seem to wrap my head around how to get this working. If anyone could help me I would be very appreciative! :slight_smile:

Here is the method I am using, (I am using AffineTrasnform to achieve the “zoom” effect).


	public void zoom(Graphics2D g){
	
		int scale = 1;
		
		if (MouseWheelInputHandler.scrollAmount > 0){
			g.setTransform(tx);
			scale *= 0.1;
			tx.scale(scale, scale);
			System.out.println("Scroll up");
			MouseWheelInputHandler.scrollAmount = 0;
		} else if (MouseWheelInputHandler.scrollAmount < 0){
			g.setTransform(tx);
			scale *= -0.1;
			tx.scale(scale, scale);
			System.out.println("Scroll down");
			MouseWheelInputHandler.scrollAmount = 0;
		}
	}

where did you call the zoom method? beware that *= -1 can misleading.

I think the problem is I don’t know how to structure classes the correct way. The design behind the class structure and overall program structure.

Here’s my modified method, which is in a class called “Entity”, which basically handles everything that is displayed on the screen. So I thought by “zooming”, the entity class
all the subclasses and children of that class would be “zoomed” as well.

The zoom method just checks to see which way the mouse scroll wheel has been rotated and applies the appropriate scaling.

At the moment it only scales 1x.



import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class Entity{

	protected double x;
	protected double y;
	protected Sprite sprite;
	protected double dx;
	protected double dy;
	AffineTransform tx;

	public Entity(String ref, int x, int y){
		this.sprite = SpriteStore.get().getSprite(ref);
		this.x = x;
		this.y = y;
		tx = new AffineTransform();
	}

	public void move(int delta) {
		x += (delta * dx) / 1000;
		y += (delta * dy) / 1000;
	}

	public void zoom(Graphics2D g, int scrollAmount){
		if (scrollAmount > 0){
			g.setTransform(tx);
			g.scale(0.9, 0.9);
		} else if (scrollAmount < 0){
			g.setTransform(tx);
			g.scale(1.1, 1.1);
		}
	}

	public void setHorizontalMovement(double dx) {
		this.dx = dx;
	}

	public void setVerticalMovement(double dy) {
		this.dy = dy;
	}

	public double getHorizontalMovement() {
		return dx;
	}

	public double getVerticalMovement() {
		return dy;
	}

	public void draw(Graphics2D g) {
		zoom(g, MouseWheelInputHandler.scrollAmount);
		sprite.draw(g,(int) x,(int) y);
	}

	public int getX() {
		return (int) x;
	}

	public int getY() {
		return (int) y;
	}

	public int getHeight(){
		return sprite.getHeight();
	}

	public int getWidth(){
		return sprite.getWidth();	
	}
}

I’m on work now so I can’t look deeper. But for first aid, try to little debugging by implement zoom in/out using keyPress, one step per press. This is more accurate and able to prove that your code inside that if block works.

Thanks will give it a try! :slight_smile: