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

    Default Scroll max to end of image

    Hi All,

    As for my last question. I was trying to scale my screen so the user can zoom in and out. By solving this problem a new one got created. If I zoom in or out, moving within the screen to the right stops to early or does not stop at the end of the screen.

    Problem seems to be getting the correct max value for the xOffset. Where xOffset is used to determine how far the camera is allowed to pan left or right. If I do not apply zoom (zOffset = 100) everything is fine. But if I zoom in (zOffset > 100), I can pan way to far to the right. If I zoom out (zOffset < 100), I cannot pan all the way to the right end of the image.
    This is how I calculate how far the screen is allowed to pan to the right:
    Java Code:
    ...
    xOffset >= -((tileSize * zOffset * 0.01) * tileAmountX - WIDTH
    ...
    If this is the problem, I tried finding the algorithm but I cannot find it. Can somebody help me, please?

    Whole code with the problem. Code is a workable test:
    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.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    
    import javax.swing.JFrame;
    
    public class ScaledScroll {
    	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 = 100;
    
    		InputHandlerKey IHK = new InputHandlerKey();
    
    		// 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 = 75;
    		public int tileAmountY = 60;
    		public int tileSize = 16;
    
    		Tile tileArray1[][] = new Tile[tileAmountX][tileAmountY];
    
    		// Controls to move and to zoom
    		public static boolean screenLeft, screenRight, 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.addKeyListener(IHK);
    
    			createTiles();
    
    			requestFocus();
    		}
    
    		// Filling the array with the tiles
    		private void createTiles() {
    			for (int y = 0; y < tileAmountY; y++) {
    				for (int x = 0; x < tileAmountX; x++) {
    					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() {
    			// Move to left
    			if (screenLeft && xOffset < 15) {
    				xOffset++;
    			}
    			// Move to right
    			if (screenRight
    					&& xOffset >= -((tileSize * zOffset * 0.01) * tileAmountX - WIDTH)) {
    				xOffset--;
    			}
    
    			// Zoom out (max 50%)
    			if (screenZoomO && zOffset >= 50) {
    				zOffset--;
    				screenZoomO = false;
    			}
    			// Zoom in (max 200%)
    			if (screenZoomI && zOffset <= 200) {
    				zOffset++;
    				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);
    
    			g.scale(zOffset * 0.01, zOffset * 0.01);
    
    			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
    					// of the edges
    					if (tileArray1[x][y].x >= 0 - tileSize * 2
    							&& tileArray1[x][y].x * zOffset * 0.01 <= WIDTH
    									+ (tileSize * 2)
    							& tileArray1[x][y].y >= 0 - tileSize * 2
    							&& tileArray1[x][y].y * zOffset * 0.01 <= HEIGHT
    									+ tileSize * 2) {
    						tileArray1[x][y].render(g);
    					}
    				}
    			}
    
    			g.dispose();
    			bs.show();
    
    		}
    
    		public static class InputHandlerKey implements KeyListener {
    			public void keyPressed(KeyEvent e) {
    				int keyCode = e.getKeyCode();
    				// Move
    				if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT)
    					Game.screenLeft = true;
    				if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT)
    					Game.screenRight = true;
    				// Zoom
    				if (keyCode == KeyEvent.VK_Q)
    					Game.screenZoomI = true;
    				if (keyCode == KeyEvent.VK_E)
    					Game.screenZoomO = true;
    			}
    
    			public void keyReleased(KeyEvent e) {
    				int keyCode = e.getKeyCode();
    
    				// Stop moving
    				if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT)
    					Game.screenLeft = false;
    				if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT)
    					Game.screenRight = false;
    				// Zoom
    				if (keyCode == KeyEvent.VK_Q)
    					Game.screenZoomI = false;
    				if (keyCode == KeyEvent.VK_E)
    					Game.screenZoomO = false;
    			}
    
    			public void keyTyped(KeyEvent arg0) {
    
    			}
    		}
    	}
    
    	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
    6,226
    Rep Power
    13

    Default Re: Scroll max to end of image

    Try this:

    Java Code:
    if (screenRight
           && xOffset >= -((tileSize * zOffset * 0.01) * tileAmountX - WIDTH)/(zOffset*.01)) {
               xOffset--;
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Scroll max to end of image

    That works :D So simple, yet so hard for me to figure out... Thank you very much!!

Similar Threads

  1. Having a problem with a scroll game background image
    By footballdude2k3 in forum Java 2D
    Replies: 3
    Last Post: 09-16-2011, 05:57 PM
  2. Help with Scroll Bars
    By Abion47 in forum AWT / Swing
    Replies: 5
    Last Post: 03-02-2011, 04:44 AM
  3. Scroll Bar Issue
    By vils11111 in forum NetBeans
    Replies: 7
    Last Post: 05-09-2010, 02:27 PM
  4. Focus scroll bar
    By BlackTea in forum SWT / JFace
    Replies: 0
    Last Post: 02-25-2010, 06:16 PM
  5. problem with scroll bar
    By r.gnanaprakasam in forum AWT / Swing
    Replies: 0
    Last Post: 04-22-2009, 01:24 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
  •