AffineTransform...

Ok, this function has bothered me, because it doesnt make too much sense, especially when im using an external class to manage my images ect.

I need to be able to scale my map tiles due to zooming in, zooming out ect, but AffineTransform doesnt make complete sense to me. i have searched the forums and google, but I need persoanl assistance.

Asking an actual question might help. :wink:

If you need scaling, why not just use Graphics2D.scale(double,double)? Call it before doing your drawing commands, and all of your drawing will be scaled by the factor your desire. This will turn into an AffineTransform behind the scenes, but you don’t need to master matrix multiplication to do scaling.

ok the Graphics2D.scale thing work, BUT it does it to the ENTIRE screen, as i found out, the work around is the scale it again, but it doesnt work that way,
unless i go below 1.0 to scale the interface to stay normal. So i must learn AffineTransform.

here is a method that i want to zoom into ONLY the tiles, yet the does nothing. i dont understand how the identity object is supposed to know what images to use?

i know this doesnt work at all, because i cant even zoom in at all now, because it doesnt know what to scale with… i have to use AffineTransform so i dont scale the interface WITH the tiles.

Actually this method is used to understand what tile to draw according the array, and then draw it at the proper position(it works fine in that sense).

/**
	 * Draw this tile to the graphics context provided
	 * 
	 * @param g The graphics context on which to draw
	 */
	public void draw(Graphics2D g, boolean showCoords, boolean showBoundrys, double zoom) {
		
		for (int i = 0; i < this.MAP_WIDTH; i++) {
			for (int j = 0; j < this.MAP_HEIGHT; j++) {
					
				if (this.tile[i][j] == NODRAW) { this.tileVis[i][j] = HIDDEN; }
				
				if (this.tileVis[i][j] == VISIBLE) {[b]AffineTransform identity = new AffineTransform();[/b]
					if (this.tile[i][j] == LIGHT_GRASS) { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\lightGrass.png","TILE"); }
					if (this.tile[i][j] == DARK_GRASS)  { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\darkGrass.png","TILE");  }
					if (this.tile[i][j] == DIRT)        { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\dirt.png","TILE");       }
					if (this.tile[i][j] == SAND)        { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\sand.png","TILE");       }
					if (this.tile[i][j] == WATER)       { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\water.png","TILE");      }
					if (this.tile[i][j] == YELLOW_GRASS){ this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\yellowGrass.png","TILE");      }
					if (this.tile[i][j] == ROCK_GRASS)  { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\rockGrass.png","TILE");      }
					if (this.tile[i][j] == GY_GRASS)    { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\gyGrass.png","TILE");      }
					if (this.tile[i][j] == GRASS_2)    { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\grass2.png","TILE");      }
					if (this.tile[i][j] == DARK_SAND)    { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\darkSand.png","TILE");      }
					if (this.tile[i][j] == RED_SAND)    { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\redSand.png","TILE");      }
					if (this.tile[i][j] == BRICK)    { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\brick.png","TILE");      }
					if (this.tile[i][j] == TRACK)    { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\track.png","TILE");      }
					if (this.tile[i][j] == FARM)    { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\farm.png","TILE");      }
					[b]AffineTransform transform = new AffineTransform();
					transform.setTransform(identity);
					transform.scale(this.zoomLevel, this.zoomLevel); [/b]
					
					this.tileGraphic.draw(g, (int)this.xPos+(this.tileGraphic.getWidth()*i), (int)this.yPos+(this.tileGraphic.getHeight()*j));
					
					if (showCoords) { 
						coordDraw = "(" + i + "," + j + ")";
						g.drawString(coordDraw,getTileXPos(i)+(TILE_SIZE/2)-coordDraw.length()*3+(int)this.xPos,getTileYPos(j)+(TILE_SIZE/2)+(int)this.yPos+3); 
					}
					if (showBoundrys) { 
						g.drawRect(getTileXPos(i-1)+(int)this.xPos+TILE_SIZE,getTileYPos(j-1)+(int)this.yPos+TILE_SIZE,TILE_SIZE,TILE_SIZE); 
					}
				}
			}
		}
	}

Each Graphics2D object has a current transformation. By default this is an identity transformation, i.e. do nothing. This transformation is represented by an instance of AffineTransform. You can modify this transformation implictly using methods like Graphics2D#scale, Graphics2D#translate, Graphics2D#shear, … or explicitly set a transformation using Graphics2D#setTransform.
The problem in your code is that you do

AffineTransform transform = new AffineTransform();
transform.setTransform(identity);
transform.scale(this.zoomLevel, this.zoomLevel);

but after that you don’t do anything with the transformation. Depending on what you’re trying to exactly you’ll have to pass this transformation to the Graphics2D object somewhere using one of the methods I mentioned above.
If you want some more info on the mathematics behind all this stuff Martin Baker’s math site is a pretty good starting point. I would recommend you make sure you thoroughly understand this stuff. It will all make a lot more sense then…