Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  AffineTransform...  (Read 872 times)
0 Members and 1 Guest are viewing this topic.
Offline DarkMortar

Junior Member




Java Padawan


« Posted 2006-12-12 07:47:48 »

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.

Offline pepijnve

Junior Member




Java games rock!


« Reply #1 - Posted 2006-12-12 08:36:57 »

Asking an actual question might help. Wink
Offline purpleguitar

Junior Member





« Reply #2 - Posted 2006-12-12 13:55:49 »

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.
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline DarkMortar

Junior Member




Java Padawan


« Reply #3 - Posted 2006-12-13 02:46:38 »

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).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
/**
    * 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);
               }
            }
         }
      }
   }

Offline pepijnve

Junior Member




Java games rock!


« Reply #4 - Posted 2006-12-13 09:44:38 »

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
1  
2  
3  
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...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (88 views)
2013-05-17 21:29:12

alaslipknot (96 views)
2013-05-16 21:24:48

gouessej (128 views)
2013-05-16 00:53:38

gouessej (123 views)
2013-05-16 00:17:58

theagentd (131 views)
2013-05-15 15:01:13

theagentd (119 views)
2013-05-15 15:00:54

StreetDoggy (161 views)
2013-05-14 15:56:26

kutucuk (184 views)
2013-05-12 17:10:36

kutucuk (185 views)
2013-05-12 15:36:09

UnluckyDevil (191 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.248 seconds with 20 queries.