Home > database >  Different method behavior in child class from abstract parent class
Different method behavior in child class from abstract parent class

Time:07-21

I'm working on a drawing project, and I have certain tools (i.e. a drawing brush, an eraser, etc.) that are their own classes, each inheriting from an abstract Tool class with some basic properties and abstract/non-abstract methods. The main problem I am seeing is when I need to change the color for these tools, I want to figure out a way to do it for all tools, but also allow certain tools (like an Eraser) to remain unchanged since it's color should always just be the background color.

I am receiving the onColorChange(int newColor) method call from elsewhere, but this is the starting point for where I trigger the color change for the tools. Right now, it only changes the color for the active tool, but that means each tool has a different color set and it gets confusing.

I want a color change to trigger the setColor() method for all of the Tools, so I thought about putting the setColor() method as a static method in the Tool class, but that wouldn't allow me to override in the Eraser class to prevent the color being changed.

Anyone have any ideas on how to implement this? TLDR: I want the onColorChange() event to trigger a color change for all of the tools, but also I want to override that in the Eraser class so no color change takes place for that tool.

Currently, with my implementation, I have the following:

Tool currentTool;
Tool myBrush;
Tool myEraser;
Tool myPencil;

// This is called from elsewhere
pubic void setup() {
   myBrush = new Brush();
   myEraser = new Eraser();
   myPencil = new Pencil();
   currentTool = myBrush;
}

// This is called from elsewhere
public void onColorChange(int newColor) {
   currentTool.setColor(newColor);
}

Tool.java

public abstract class Tool {

    protected String toolName;

    protected Paint myPaint;

    public String getToolName() {
        return mToolName;
    }

    public int getColor() {
        return myPaint.getColor();
    }

    public Paint getPaintObject() {
        return myPaint;
    }

    protected abstract void setColor(int newColor);
}

Eraser.java

public class Eraser extends Tool {


    public Eraser() {
        toolName = "ERASER";


        myPaint = new Paint();
        mFgPaint.setColor(Color.parseColor("#FFFFFF"));
        mFgPaint.setAntiAlias(true);
        mFgPaint.setStrokeWidth(DEFAULT_WIDTH);
        mFgPaint.setStyle(Paint.Style.STROKE);
        mFgPaint.setStrokeJoin(Paint.Join.ROUND);
        mFgPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void setColor(int newColor) {
        // Empty, since I cannot change the paint color of the eraser
    }

}

Brush.java

public class Brush extends Tool {

   
    public Brush() {
        toolName = "BRUSH";
        myPaint = new Paint();
        setColor(Color.parseColor("#000000"));
        mFgPaint.setAntiAlias(true);
        mFgPaint.setStrokeWidth(DEFAULT_WIDTH);
        mFgPaint.setStyle(Paint.Style.STROKE);
        mFgPaint.setStrokeJoin(Paint.Join.ROUND);
        mFgPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void setColor(int newColor) {
        myPaint.setColor(newColor);
    }

Pencil.java

public class Pencil extends Tool {

   
    public Brush() {
        toolName = "PENCIL";
        myPaint = new Paint();
        setColor(Color.parseColor("#000000"));
        mFgPaint.setAntiAlias(true);
        mFgPaint.setStrokeWidth(DEFAULT_WIDTH);
        mFgPaint.setStyle(Paint.Style.STROKE);
        mFgPaint.setStrokeJoin(Paint.Join.ROUND);
        mFgPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void setColor(int newColor) {
        myPaint.setColor(newColor);
    }

CodePudding user response:

I would recommend to add a new abstract class ColorableTool extending Tool which stores the method setColor
Eraser will extend Tool while Brush and Pencil will extend ColorableTool
If your project goes more into polymorphism you should check interfaces for such behavior (in Java) ! :)

Edit 1

You might check your current tool is a ColorableTool using instanceof like this

if(currentTool instanceof ColorableTool) {
  // Do Stuff
}

EDIT 2

As you want to update every color I suggest to use a static paint, an instance of paint shared among the Tool (or ColorableTool if you implemented my solution). It should look like

public abstract class Tool {

    protected String toolName;
    static final Paint myPaint = new Paint(); // A shared paint between tools

    public String getToolName() {
        return toolName;
    }

    public int getColor() {
        return myPaint.getColor();
    }

    public Paint getPaintObject() {
        return myPaint;
    }

    public static void setColor(int newColor) {
        Tool.myPaint.setColor(newColor);
    }
}

Therefore calling Tool.setColor will update color for every tool !

  •  Tags:  
  • java
  • Related