Results 1 to 2 of 2
  1. #1
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default [SOLVED]zooming and walking

    I have played a little with Graphics2D in java, but now i have stumbled upon a problem.

    In my test there is a player, which is just a black filled oval, which can move around with the wasd keys, i have placed several other blocks around him so that i can see that he moves around.
    What i tried to do is translate the graphics so that the center of the screen became the 0,0 point, from there i draw my squares and my player. When the player moves the translation of the G2D object is altered so the player stays in the center of the screen, this all works good, but I have also implemented a zoom and rotate function. And then comes the problem. When the zoom factor is 1.0 , so there is no zoom, everything works fine, when i move the player stays in the center of the screen and the player is the center of the rotation. but when i zoom in and then walk, the players moves away from the center, eventually is no longer on the screen because it moves to fast, and the players is also no longer the center point for rotation. When i zoom out and move the player moves away from the center, eventually is no longer on the screen because it moves to slow and the player is also no longer the center point for the rotation. The code that i use is this.
    Java Code:
    package test002;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
    import java.awt.geom.AffineTransform;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class Panel extends JPanel implements MouseWheelListener, KeyListener {
    
    	private double scale;
    	private int innit;
    	private boolean shiftOn;
    	private Player player;
    
    	public Panel() {
    		this.scale = 1.0;
    		this.innit = 1;
    		this.addMouseWheelListener(this);
    		this.addKeyListener(this);
    		this.setFocusable(true);
    		this.requestFocus();
    		this.shiftOn = false;
    		this.player = new Player(0, 0);
    	}
    
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D) g;
    		AffineTransform old = g2.getTransform();
    		
    		if(innit == 1)
    		{
    			innit = 0;
    			g2.translate(this.getWidth() / 2, this.getHeight() / 2);
    			System.out.println("Done");
    		}
    		
    		g2.translate( (this.getWidth() / 2) - (this.player.getX() + 10), (this.getHeight() / 2) - (this.player.getY() + 10));
    		g2.rotate(Math.toRadians(this.player.getRotation()), 25, 25);
    		g2.scale(scale, scale);
    		g2.setColor(Color.red);
    		g2.fillRect(-100, -50, 50, 50);
    
    		g2.setColor(Color.green);
    		g2.fillRect(60, 50, 50, 50);
    		
    		g2.setColor(Color.black);
    		g2.fillOval(this.player.getX(), this.player.getY(), 20, 20);
    
    		g2.setTransform(old);
    		g2.setColor(Color.black);
    		g2.drawString("Mouse wheel to zoom", 5, 15);
    		g2.drawString("Hold Shift + mousewheel to rotate", 5, 30);
    		g2.drawString("W/A/S/D to move", 5, 45);
    		g2.drawString("Zoom level: " + this.scale, 5, 60);
    		g2.drawString("Rotation degrees: " + this.player.getRotation(), 5, 75);
    		g2.drawString("X/Y: " + this.player.getX() + "/" + this.player.getY(), 5, 90);
    	}
    
    	@Override
    	public void mouseWheelMoved(MouseWheelEvent e) {
    		double trans = e.getWheelRotation();
    		if(!this.shiftOn)
    		{
    			this.scale -= (trans / 10);
    			if (this.scale < 0.1)
    				this.scale = 0.1;
    			if (this.scale > 2.0)
    				this.scale = 2.0;
    		}
    		else if(this.shiftOn)
    		{
    			this.player.setRotation((int) (this.player.getRotation() - (trans * 4)));
    			if(this.player.getRotation() > 360)
    				this.player.setRotation(0);
    			if(this.player.getRotation() < 0)
    				this.player.setRotation(360);
    		}
    		this.repaint();
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode() == KeyEvent.VK_SHIFT)
    			this.shiftOn = true;
    		if(e.getKeyCode() == KeyEvent.VK_W)
    			this.player.setY(this.player.getY() - 5);
    		if(e.getKeyCode() == KeyEvent.VK_S)
    			this.player.setY(this.player.getY() + 5);
    		if(e.getKeyCode() == KeyEvent.VK_A)
    			this.player.setX(this.player.getX() - 5);
    		if(e.getKeyCode() == KeyEvent.VK_D)
    			this.player.setX(this.player.getX() + 5);
    		
    		this.repaint();
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		if(e.getKeyCode() == KeyEvent.VK_SHIFT)
    		{
    			this.shiftOn = false;
    		}
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {}
    }
    the player:
    Java Code:
    package test002;
    
    public class Player {
    
    	private int x, y, rotation;
    	
    	public Player(int x, int y) {
    		this.x = x;
    		this.y = y;
    		this.rotation = 0;
    	}
    
    	public int getX() {
    		return x;
    	}
    
    	public void setX(int x) {
    		this.x = x;
    	}
    
    	public int getY() {
    		return y;
    	}
    
    	public void setY(int y) {
    		this.y = y;
    	}
    
    	public int getRotation() {
    		return rotation;
    	}
    
    	public void setRotation(int rotation) {
    		this.rotation = rotation;
    	}
    	
    	
    }
    What i think needs to be done, is that the zoom scale alters the translation, but i have no idea how.
    Do any of you guys know how to fix this?
    Last edited by L19htn1n9; 11-11-2012 at 05:37 PM. Reason: solved the problem

  2. #2
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default Re: zooming and walking

    nevermind i seem to have solved the problem by changing this line:
    Java Code:
    g2.translate( (this.getWidth() / 2) - (this.player.getX() + 10), (this.getHeight() / 2) - (this.player.getY() + 10));
    To this code:
    Java Code:
    g2.translate( (this.getWidth() / 2) - ((this.player.getX() + 10) * this.scale), (this.getHeight() / 2) - ((this.player.getY() + 10) * this.scale));

Similar Threads

  1. [NPC] Walking help...
    By santafan in forum Advanced Java
    Replies: 2
    Last Post: 11-29-2011, 02:42 AM
  2. Zooming and scaling problem
    By Koopa in forum Java 2D
    Replies: 1
    Last Post: 07-07-2011, 11:30 PM
  3. zooming in a 2D rts game in java
    By masternerdguy in forum Advanced Java
    Replies: 9
    Last Post: 01-04-2010, 07:31 PM
  4. Zooming in on fractal
    By Mr.Beans in forum Java 2D
    Replies: 1
    Last Post: 04-18-2009, 06:00 AM
  5. Zooming Canvas Area
    By Raghavansat in forum AWT / Swing
    Replies: 0
    Last Post: 02-04-2009, 09:35 AM

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
  •