Results 1 to 3 of 3
  1. #1
    Vinvar is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Better way to zoom in/out

    Hi All,

    I have built a 2d tiled map enige with the ability to scroll left, right, up and down. Now, I would like to zoom as well. As in: when you use the mouse wheel, the map zoom in and zooms out.

    Currently, I have tried to manage this by resizing the tile size and then completely re do filling my array. I know/guess it would be better to scale the view. But I do not know how to do that. If I understand it correctly, I should use affine transform (scale). Can anybody help me, please?

    Hereby part of my code. It is a workable test case, with only the current way of zooming. So I did not include all the code for moving left, right, up and down, just for readability.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    
    import javax.swing.JFrame;
    
    public class Zooming {
    	public static void main(String args[]) {
    		new Game().start();
    	}
    
    	static class Game extends Canvas implements Runnable {
    		private static final long serialVersionUID = 1L;
    
    		// Offset for displaying objects
    		int xOffset = 16;
    		int yOffset = 16;
    		int zOffset = 16;
    
    		InputHandlerMouse IHM = new InputHandlerMouse();
    
    		// Core stuff
    		BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
    				BufferedImage.TYPE_INT_RGB);
    		JFrame frame;
    		public static boolean running = false;
    		public static final int WIDTH = 1024;
    		public static final int HEIGHT = WIDTH / 16 * 9;
    		public static final Dimension gameDim = new Dimension(WIDTH, HEIGHT);
    
    		public int tileAmountX = 160;
    		public int tileAmountY = 160;
    		public int tileSize = 16;
    
    		Tile tileArray1[][] = new Tile[tileAmountX][tileAmountY];
    
    		// Controls to zoom
    		public static boolean screenZoomO, screenZoomI;
    
    		public void run() {
    			while (running) {
    				tick();
    				render();
    			}
    
    		}
    
    		public synchronized void start() {
    			running = true;
    			Thread thread = new Thread(this);
    			thread.start();
    		}
    
    		public synchronized static void stop() {
    			running = false;
    			System.exit(0);
    		}
    
    		public Game() {
    			setMinimumSize(gameDim);
    			setMaximumSize(gameDim);
    			setPreferredSize(gameDim);
    			frame = new JFrame();
    
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			frame.setLayout(new BorderLayout());
    
    			frame.add(this, BorderLayout.CENTER);
    			frame.pack();
    
    			frame.setResizable(false);
    			frame.setLocationRelativeTo(null);
    			frame.setVisible(true);
    
    			this.addMouseWheelListener(IHM);
    
    			createTiles();
    
    			requestFocus();
    		}
    
    		// Filling the array with the tiles
    		private void createTiles() {
    			for (int x = 0; x < tileAmountX; x++) {
    				for (int y = 0; y < tileAmountY; y++) {
    					tileArray1[x][y] = new Tile(x * tileSize, y * tileSize,
    							this);
    				}
    			}
    		}
    
    		public void tick() {
    			for (int x = 0; x < tileAmountX; x++) {
    				for (int y = 0; y < tileAmountY; y++) {
    					tileArray1[x][y].tick(this);
    				}
    			}
    			moveMap();
    		}
    
    		private void moveMap() {
    			if (screenZoomO && zOffset >= 12) {
    				zOffset--;
    				tileSize = zOffset;
    				createTiles();
    				screenZoomO = false;
    			}
    			if (screenZoomI && zOffset <= 32) {
    				zOffset++;
    				tileSize = zOffset;
    				createTiles();
    				screenZoomI = false;
    			}
    		}
    
    		public void render() {
    			BufferStrategy bs = getBufferStrategy();
    			if (bs == null) {
    				createBufferStrategy(3);
    				return;
    			}
    
    			Graphics2D g = (Graphics2D) bs.getDrawGraphics();
    
    			g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    
    			for (int x = 0; x < tileAmountX; x++) {
    				for (int y = 0; y < tileAmountY; y++) {
    					// Render tiles in screen plus 2 tiles near the other end
    					// the
    					// edges
    					if (tileArray1[x][y].x >= 0 - tileSize * 2
    							&& tileArray1[x][y].x <= WIDTH + tileSize * 2
    							& tileArray1[x][y].y >= 0 - tileSize * 2
    							&& tileArray1[x][y].y <= HEIGHT + tileSize * 2) {
    						tileArray1[x][y].render(g);
    					}
    				}
    			}
    
    			g.dispose();
    			bs.show();
    
    		}
    
    		public static class InputHandlerMouse implements MouseWheelListener {
    			public void mouseWheelMoved(MouseWheelEvent m) {
    				int notches = m.getWheelRotation();
    				if (notches == -1) {
    					Game.screenZoomI = true;
    				} else if (notches == 1) {
    					Game.screenZoomO = true;
    				}
    			}
    		}
    	}
    
    	private static class Tile {
    		int x, y;
    		int oX, oY;
    		Game game;
    
    		public Tile(int x, int y, Game game) {
    			this.oX = x;
    			this.oY = y;
    
    			this.game = game;
    		}
    
    		public void tick(Game game) {
    			x = oX + game.xOffset;
    			y = oY + game.yOffset;
    		}
    
    		public void render(Graphics g) {
    			g.setColor(new Color(0, 255, 255));
    			g.fillRect(x, y, game.tileSize, game.tileSize);
    
    			g.setColor(Color.BLACK);
    			g.drawRect(x, y, game.tileSize, game.tileSize);
    		}
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,601
    Rep Power
    5

    Default Re: Better way to zoom in/out

    Have you simply tried using the Graphics2D.scale() method? Just specify the same value for x and y to maintain the aspect ratio. You can modify the scale parameters in your wheel listener.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Vinvar is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Better way to zoom in/out

    Sometimes the most simple answers will do. :)
    Thank you.

    I (stupidly) tried to scale it afterwards. It had to be done before... :$

    So for further referrence, the MoveMap() will be:
    Java Code:
    		private void moveMap() {
    			// Zoom out (max 50%)
    			if (screenZoomO && zOffset >= 50) {
    				zOffset--;
    				screenZoomO = false;
    			}
    			// Zoom in (max 200%)
    			if (screenZoomI && zOffset <= 200) {
    				zOffset++;
    				screenZoomI = false;
    			}
    		}
    and to the render() must be added:

    Java Code:
        ...
        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    			
        g.scale(zOffset * 0.01, zOffset * 0.01);
    
        for (int x = 0; x < tileAmountX; x++) {
            for (int y = 0; y < tileAmountY; y++)
                ...

Similar Threads

  1. Replies: 13
    Last Post: 08-30-2010, 07:55 PM
  2. How to Zoom in and Zoom out TYPE_USHORT_565_RGB image
    By Santhoshkumarp in forum AWT / Swing
    Replies: 0
    Last Post: 08-07-2010, 02:39 PM
  3. Zoom in and out
    By mickey in forum Java 2D
    Replies: 1
    Last Post: 06-15-2010, 11:39 PM
  4. Java Zoom in and Zoom out?
    By ps20090 in forum Java Applets
    Replies: 2
    Last Post: 09-18-2009, 12:04 PM
  5. zoom in swing
    By Alan in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 02:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •