JHotDraw Pattern Language

Using CreationTool


The most common way to add figures to a drawing is to create a tool in the application. JHotDraw uses a class called CreationTool to create tools that can be used to instantiate any kind of figure.

CreationTool is an implementation of the Prototype design pattern. CreationTool can be parameterised to create any kind of Figure. To create the tool call the CreationTool constructor and pass into it an instance (the Prototype) of the type of Figure you want to create.

   protected void createTools(Panel palette){
        super.createTools(palette);
            ... define other tools here
        CreationTool creationTool = new CreationTool(view(), new EllipseFigure());
        //add a button to the toolbar that represents the tool
     }

Tools are represented in the applications GUI as a button.

The CreationTool automatically provides the behaviour for creating the size of the new figure (i.e. when the tool is selected, using the mouse click to place the figure then drag to determine its width and height).

 


Copyright Douglas Kirk