Paint color ignored from inside object instance.

Problems with Canvas, OpenGL, etc...

Paint color ignored from inside object instance.

Postby devlin » Wed Feb 01, 2012 12:41 pm

I made a simple text emitter class that can have different colors and fade out in time.

My problem is all the instances keep drawing the text using the same color even when I assigned different colors to different instances. The canvas passed to the object is a canvas made from a bitmap which is used as a frame buffer.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.graphics.Canvas;
  2. import android.graphics.Paint;
  3. import android.graphics.Paint.Align;
  4. import android.graphics.Paint.Style;
  5. import android.graphics.Rect;
  6.  
  7. public class ETFader{
  8.         public static final int VERTICAL_BASELINE = 0;
  9.         public static final int VERTICAL_TOP = 2;
  10.         public static final int VERTICAL_CENTER = 4;
  11.         public static final int VERTICAL_BOTTOM = 8;
  12.  
  13.         public String text = "";
  14.         public float x = 0;
  15.         public float y = 0;
  16.         public int w = 0;
  17.         public int h = 0;
  18.         public boolean visible = true;
  19.         public boolean alive = true;
  20.         public boolean started = false;
  21.        
  22.         private float alpha = 255;
  23.         private int vAlign = VERTICAL_CENTER;
  24.         private long frameOffset = 0;
  25.         private long frameStart = 0;
  26.         private long frameDelay = 10;
  27.         private Canvas canvas = null;
  28.         private Paint paint = null;
  29.         private Paint outline = null;
  30.         private int color1 = 0;
  31.         private int color2 = 0;
  32.         private Rect bounds = null;
  33.        
  34.         public ETFader(Canvas c, String txt, float sx, float sy, float sz, int col1, int col2) {
  35.                 canvas = c;
  36.                 text = txt;
  37.                 x = sx;
  38.                 y = sy;
  39.                 alpha = 255;
  40.                 alive = false;
  41.                 started = false;
  42.                 frameOffset = 1000;
  43.                 color1 = col1;
  44.                 color2 = col2;
  45.                
  46.                 paint = new Paint();
  47. //              paint.setTypeface(Fonts.item("black"));
  48.                 paint.setTextSize(sz);
  49.                 paint.setAntiAlias(true);
  50.                 paint.setTextAlign(Align.CENTER);
  51.                 paint.setColor(color1);
  52.  
  53.                 outline = new Paint();
  54. //              outline.setTypeface(Fonts.item("black"));
  55.                 outline.setTextSize(sz);
  56.                 outline.setAntiAlias(true);
  57.                 outline.setTextAlign(Align.CENTER);
  58.                 outline.setStyle(Style.STROKE);
  59.                 outline.setStrokeWidth(3);
  60.                 outline.setColor(color2);
  61.                
  62.                 bounds = new Rect();
  63.                 paint.getTextBounds(text, 0, text.length(), bounds);
  64.                 w = bounds.width();
  65.                 h = bounds.height();
  66.         }
  67.        
  68.         public void show(String txt, float sx, float sy, long start) {
  69.                 frameStart = System.currentTimeMillis();
  70.                 visible = true;
  71.                 text = txt;
  72.                 x = sx;
  73.                 y = sy;
  74.                 alpha = 255;
  75.                 alive = true;
  76.                 started = false;
  77.                 frameOffset = start;
  78.                 paint.setAlpha(255);
  79.                 outline.setAlpha(255);
  80.                 paint.getTextBounds(text, 0, text.length(), bounds);
  81.                 w = bounds.width();
  82.                 h = bounds.height();
  83.         }
  84.        
  85.         public void destroy() {
  86.                 visible = false;
  87.                 alive = false;
  88.                 paint = null;
  89.                 outline = null;
  90.                 canvas = null;
  91.         }
  92.        
  93.         public void update() {
  94.                 if (!visible) return;
  95.                 if (!alive) return;
  96.                 if (!started){
  97.                         if ((System.currentTimeMillis() - frameStart) < frameOffset) return;
  98.                         frameStart = System.currentTimeMillis();
  99.                         started = true;
  100.                 }
  101.                 if ((System.currentTimeMillis() - frameStart) < frameDelay) return;
  102.                 frameStart = System.currentTimeMillis();
  103.                 if (alpha > 1){
  104.                         alpha *= 0.5;
  105.                         outline.setAlpha((int)alpha);
  106.                         paint.setAlpha((int)alpha);
  107.                 } else {
  108.                         outline.setAlpha(0);
  109.                         paint.setAlpha(0);
  110.                         alive = false;
  111.                 }
  112.         }
  113.        
  114.         public float getY(float sy){
  115.                 if (vAlign == VERTICAL_TOP){
  116.                         return sy - bounds.top;
  117.                 }
  118.                 if (vAlign == VERTICAL_CENTER){
  119.                         return (sy - bounds.top) - (this.h / 2);
  120.                 }
  121.                 if (vAlign == VERTICAL_BOTTOM){
  122.                         return sy - (this.h + bounds.top);
  123.                 }
  124.                 return sy;
  125.         }
  126.  
  127.         public void draw() {
  128.                 draw(0, 0);
  129.         }
  130.  
  131.         public void draw(float mx, float my) {
  132.                 if (canvas == null) return;
  133.                 if (!visible) return;
  134.                 if (!alive) return;
  135.                 float sx = mx + x;
  136.                 float sy = my + getY(y);
  137.                 float sx1 = sx + w;
  138.                 float sy1 = sy + h;
  139.                
  140.                 //if off screen don't draw
  141.                 if (sx > Settings.targetW) return;
  142.                 if (sx1 < 0) return;
  143.                 if (sy > Settings.targetH) return;
  144.                 if (sy1 < 0) return;
  145.  
  146.                 outline.setColor(color2);
  147.                 canvas.drawText(text, sx, sy, outline);
  148.                 paint.setColor(color1);
  149.                 canvas.drawText(text, sx, sy, paint);
  150.         }
  151. }
  152.  
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


Initialize the objects like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 txtPass = new ETFader(canvas, "Pass", scx, 30, 64, Color.parseColor("#FF660000"), Color.parseColor("#FFFF3300"));
  2.                 txtTurn = new ETFader(canvas, "Left Player's Turn", scx, 60, 48, Color.parseColor("#FF666600"), Color.parseColor("#FFCCFF00"));
  3.                 txtRound = new ETFader(canvas, "Round 1", scx, 90, 64, Color.parseColor("#FFFF3300"), Color.parseColor("#FFCCFF00"));
  4.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Int the main loop they are drawn like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 txtRound.draw();
  2.                 txtTurn.draw();
  3.                 txtPass.draw();
  4.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Any thoughts why the colors doesn't get applied?
devlin
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Feb 28, 2011 10:29 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 3 guests