Reputation: 45
I am creating a paint app.
In my program there is a variable named tool which stores the name of the tool being used.Initially it is set to free hand but when the tool is changed to any other tool and when I switch back the tool to free hand, then the screen get cleared with black color
.As a result I am unable to draw free hand but only a point appears following my finger.
I debugged the program and found that once the statement to clear the screen with black color is called, it gets called even when it not defined to be called.This is my piece of code:
if(tool.equals("freeHand")){
Log.d("free","move");
try{
canvas=null;
canvas = holder.lockCanvas();
synchronized (holder) {
//draw(canvas);
canvas2.drawBitmap(backup,0,0,paint);
canvas2.drawLine(startX, startY, moveX ,moveY, paint);
//backup=Bitmap.createBitmap(cache);
draw(canvas);
}
}finally{
if(canvas != null){
holder.unlockCanvasAndPost(canvas);
// backup=Bitmap.createBitmap(cache);
}
}
//canvas2.drawLine(startX, startY, moveX ,moveY, paint);
startX = moveX;
startY = moveY;
//backup=Bitmap.createBitmap(cache);
}
else{
if(!tool.equals("freeHand") ){
canvas2.drawColor(Color.BLACK);
Log.d("free", "bug");
}
if(tool=="line"){
try{
canvas=null;
canvas = holder.lockCanvas();
synchronized (holder) {
//draw(canvas);
canvas2.drawBitmap(backup,0,0,paint);
canvas2.drawLine(downX,downY,moveX,moveY, paint);
draw(canvas);
}
}finally{
if(canvas != null){
holder.unlockCanvasAndPost(canvas);
}
}
//canvas2.drawBitmap(backup, 0, 0, paint);
//canvas2.drawLine(downX,downY,moveX,moveY, paint);
}
if(tool == "strokeRect"){
try{
canvas=null;
canvas = holder.lockCanvas();
synchronized (holder) {
//draw(canvas);
canvas2.drawBitmap(backup,0,0,paint);
canvas2.drawCircle(moveX, moveY, 20, paint);
draw(canvas);
}
}finally{
if(canvas != null){
holder.unlockCanvasAndPost(canvas);
}
}
//canvas2.drawCircle(moveX, moveY, 20, paint);
}
if(tool == "fillRect"){
try{
canvas=null;
canvas = holder.lockCanvas();
synchronized (holder) {
//draw(canvas);
canvas2.drawBitmap(backup,0,0,paint);
canvas2.drawRect(downX, downY, moveX, moveY, paint);
draw(canvas);
}
}finally{
if(canvas != null){
holder.unlockCanvasAndPost(canvas);
}
}
//canvas2.drawRect(downX, downY, moveX, moveY, paint);
}
if(tool=="img"){
try{
canvas=null;
canvas = holder.lockCanvas();
synchronized (holder) {
//draw(canvas);
canvas2.drawBitmap(backup,0,0,paint);
canvas2.drawBitmap(img,moveX,moveY,paint);
//canvas2.drawBitmap(backup,0,0,paint);
draw(canvas);
}
}finally{
if(canvas != null){
holder.unlockCanvasAndPost(canvas);
}
}
}
}
I think that the statement:
canvas2.drawColor(Color.BLACK);
is causing the problem.
How do I get rid of unwanted call of the statement?
Upvotes: 0
Views: 77
Reputation: 13902
when you clear your screen using black color, it will clear everything on the screen, now even if you set the color to your original color, anything you drew earlier would not be visible, it will be overdrawn by the color.
when I switch back the tool to free hand, then the screen get cleared with black color
when you switch back , you have to set the color again, otherwise once set it will remain as it is
Upvotes: 0
Reputation: 14472
You cannot compare strings like this:
tool!="freeHand"
Strings are objects and !=
tests if two objects are not the same object.
Instead, use the equals()
method to test the contents of the strings:
!tool.equals("freeHand")
Because of "string interning", sometimes !=
and ==
will work but you should not rely on it.
http://en.wikipedia.org/wiki/String_interning
You might also consider not using strings. How about an enum?
public enum Tool {Freehand, Eraser, Paintbrush }
...
private Tool tool;
...
tool = Tool.Freehand;
...
if(tool == Tool.Freehand)
{
...
}
Much better...
Upvotes: 1