Reputation: 37
I have two paints and set their colors to different colors. The problem is that the second paint overrides the first paint.
That's my code:
public class Score {
static Globals g = Globals.getInstance();
private Rect bounds = new Rect();
private Paint paintBG = new Paint();
private Paint paintFG = new Paint();
private int mid;
public Score(Paint paint) {
this.paintBG = paint;
this.paintFG = paint;
// PROBLEME
paintBG.setColor(Color.GRAY);
paintFG.setColor(Color.WHITE); // <-- this paint overrides the paint before
}
public void draw(Canvas canvas) {
String score = String.valueOf(g.getScore());
paintFG.getTextBounds(score, 0, score.length(), bounds);
mid = (canvas.getWidth() / 2) - (bounds.width() / 2);
// different "paints" but the same color
canvas.drawText(score, mid, 60, paintBG);
canvas.drawText(score, mid, 50, paintFG);
}
}
Best regards from germany. :)
Upvotes: 0
Views: 87
Reputation: 37655
amahfouz has explained the problem. One solution to the problem is to use Paint
's copy constructor.
public Score(Paint paint) {
paintBG = new Paint(paint);
paintFG = new Paint(paint);
paintBG.setColor(Color.GRAY);
paintFG.setColor(Color.WHITE);
}
Upvotes: 1
Reputation: 2398
This is expected. Both paints are referring to the same Paint object, the one passed as a parameter. So basically, both calls to setColor are modifying the same object and the last one is the one that sticks!
zum wohle!
Upvotes: 0