Advertisement
Guest User

Drawing objects rotated, transformed and scaled and flipped

a guest
May 29th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. public abstract class DrawObject
  2. {
  3.     public float    TransformX = 0f, TransformY = 0f;
  4.     public float    Rotation = 0f;
  5.     public float    Scale = 1f;
  6.     public float    CenterX = 0f, CenterY = 0f;
  7. }
  8.  
  9. public class GraphicsHandler
  10. {
  11.     public static void DrawObject(DrawObject obj, Canvas canvas, Matrix matrix, float scaleX, float scaleY)
  12.     {
  13.        
  14.         if (obj == null)
  15.             return;
  16.  
  17.         DrawObject(obj, canvas, matrix, scaleX, scaleY, 1f, 1f);
  18.        
  19.         if (obj.DrawingMode == DrawMode.VMirror || obj.DrawingMode == DrawMode.VHMirror)
  20.             DrawObject(obj, canvas, matrix, scaleX, scaleY, -1f, 1f);
  21.         if (obj.DrawingMode == DrawMode.HMirror || obj.DrawingMode == DrawMode.VHMirror)
  22.             DrawObject(obj, canvas, matrix, scaleX, scaleY, -1f, -1f);
  23.         if (obj.DrawingMode == DrawMode.VHMirror)
  24.             DrawObject(obj, canvas, matrix, scaleX, scaleY, 1f, -1f);
  25.        
  26.     }
  27.    
  28.     // scaleX and scaleY are the scale of the screen. If the original size is 1000x1000 but this is rendered on a 500x500 screen
  29.         // the scale is 0.5 and 0.5
  30.         // sX and and sY are the flip values
  31.  
  32.     public static void DrawObject(DrawObject obj, Canvas canvas, Matrix matrix, float scaleX, float scaleY, float sx, float sy)
  33.     {
  34.         canvas.save();
  35.        
  36.         try
  37.         {
  38.             if (matrix == null)
  39.                 matrix = new Matrix();
  40.            
  41.             matrix.reset();
  42.             matrix.setScale(scaleX, scaleY);
  43.             matrix.postScale(sx, sy, (canvas.getWidth())*0.5f, (canvas.getHeight())*0.5f);
  44.             canvas.setMatrix(matrix);
  45.         }
  46.         catch (Exception ex) { }
  47.  
  48.         try
  49.         {
  50.             canvas.rotate(obj.Rotation, obj.CenterX + obj.TransformX, obj.CenterY + obj.TransformY);
  51.             canvas.translate(obj.TransformX, obj.TransformY);
  52.             canvas.scale(obj.Scale, obj.Scale, obj.CenterX, obj.CenterY);
  53.  
  54.             // each DrawObject handles it's own drawing: Rectangle, Path and suchs...
  55.             obj.Draw(canvas);
  56.         }
  57.         catch (Exception ex) { }
  58.        
  59.         canvas.restore();
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement