Loading...
Logo
Processing Forum
(I shortended some of the classes in this example)

I have a basic class:

Copy code
  1. public class Tool {

  2.       public Tool() {

  3.       }
  4. }

If i want to create a pen tool or selection tool i need to extend Tool
Copy code
  1. public class PenTool extends Tool {

  2. public PenTool() {

  3. }
  4. }

Then i have a class ToolHandler,
this is a method from that class (where the problem is i assume)

Copy code
  1. public boolean setActiveTool(Class<Tool> tool) {
  2. for(Tool t : tools) {
  3. if(t.getClass() == tool) {
  4. activeTool = t;
  5. return true;
  6. }
  7. }
  8. return false;
  9. }


In processing i want to use this:
Copy code
  1. ToolHandler toolHandler = new ToolHandler(this);

  2. toolHandler.setActiveTool(PenTool);

notice that i pass the Class as a parameter and not a instance.

However processing says "cannot find anything named "PenTool""

Is what i want possible? I don't want to use Enums for reasons i'm not gonna explain here.

Replies(10)

In this case, the function expects an instance of Class, which is a recursive representation of a class. However, you are sending a class itself, which is not an instance. Your Processing code would need to build a Class object and pass that.
You should change the method to

Copy code
  1. public boolean setActiveTool(Tool tool) {
  2.       for(Tool t : tools) {
  3.             if(t.getClass() == tool.getClass()) {
  4.                   activeTool = t;
  5.                   return true;
  6.             }
  7.       }
  8.       return false;
  9. }
Remember a Tool reference can refer to an object of type Tool OR an object of any class that inherits from Tool, so in this case it can be a PenTool

So tools is a collection of Tool or Tool sub-class objects.

You also need to clarify the reason for having tools is it
  1. to make sure that tool is of type Tool or a sub-class
  2. to ensure that tool is the same class as one of the existing objects in tools
  3. to make sure the object tool is the same object as one of those in tools
because it will make a difference to the code inside this method. Currently you are doing (2)

HTH


thanks, i made the changes.

this works:

Copy code
  1.   PenTool pt = new PenTool();
  2.   toolHandler.setActiveTool(pt);

but this doen't,

Copy code
  1.   toolHandler.setActiveTool(PenTool);

i still get "cannot find anything named "PenTool""
That is because you have not clearly stated the problem  which is why I asked about the role of tools.

Try this

Copy code
  1. public boolean setActiveTool(Class c) {
  2.       for(Tool t : tools) {
  3.             if(t.getClass() == c) {
  4.                   activeTool = t;
  5.                   return true;
  6.             }
  7.       }
  8.       return false;
  9. }
and

Copy code
  1. toolHandler.setActiveTool(PenTool.getClass());

It will find the first object in tools that is of type PenClass and make that active

thanks.

about:

Copy code
  1. PenTool.getClass()

that returns the class,

but what is PenTool itself then?
PenTool is the name of a class and thats it.
Copy code

  1.   toolHandler.setActiveTool(PenTool);

i still get "cannot find anything named "PenTool""
Try to put "public" before your class definitions!
This works:
Copy code
  1. void setup(){
  2.   ToolHandler th = new ToolHandler();
  3.   th.activateTool(new RubberTool());
  4. }

  5. public class PenTool extends Tool{
  6.   void write() {
  7.     println("lalalal");
  8.   }
  9. }

  10. class RubberTool extends Tool{
  11.   void delete() {
  12.     println("zschsch");
  13.   }
  14. }

  15. public class ToolHandler {
  16.   Tool tool;
  17.   void activateTool(Tool tool) {
  18.     if (tool instanceof PenTool) {
  19.       this.tool = tool;
  20.       ((PenTool)tool).write();
  21.     }
  22.     else if(tool instanceof RubberTool){
  23.       this.tool = tool;
  24.       ((RubberTool)tool).delete();
  25.     }
  26.   }
  27. }

  28. abstract class Tool {}



Or if you reall need to use classes:
Copy code
  1. ArrayList<Class<? extends Tool>> tools;

  2. void setup(){
  3.   tools = new ArrayList<Class<? extends Tool>>();
  4.   tools.add(RubberTool.class);
  5.   tools.add(PenTool.class);
  6.   
  7.   ToolHandler th = new ToolHandler();
  8.   th.activateTool(tools.get(1));
  9. }

  10. public class PenTool extends Tool{
  11.   void write() {
  12.     println("lalalal");
  13.   }
  14. }

  15. class RubberTool extends Tool{
  16.   void delete() {
  17.     println("zschsch");
  18.   }
  19. }

  20. public class ToolHandler {
  21.   Tool tool;
  22.   void activateTool(Class<? extends Tool> toolClass) {
  23.     if (toolClass == PenTool.class) {
  24.       tool = new PenTool();
  25.       ((PenTool)tool).write();
  26.     }
  27.     else if(toolClass == RubberTool.class){
  28.       tool = new RubberTool();
  29.       ((RubberTool)tool).delete();
  30.     }
  31.   }
  32. }

  33. abstract class Tool {
  34. }


thanks, last one was really learnfull.