Brush Design

Introduction

All brushes are extensions from the Brush class in Brush.pde. Each brush consists of a draw function, a paint function, and an update function. The draw function creates the shape of the brush on the display. The paint function is what changes the color of the overlay on top of the image. The update function is what gives the brush its shape.


Example

This is the source code for the circle brush. The functions are broken up to show what they do and the format.

Extension
class BrushCircle extends Brush{
    public BrushCircle(color col,EMImage image,int s){
        super(col,image,s);
    }  
                        

Draw Function

This create the brush on the screen in relation to the image.

public BrushCircle draw(){
    float zoom=this.img.getZoom();
    Pixel pixel = brushPosition();
    image(shape,(pixel.x*zoom+this.img.offsetX),(pixel.y*zoom+this.img.offsetY),shape.width*zoom,shape.height*zoom);
    return this;
} 
                        

Paint Function

The overlay is updated in this function based on the color, and brush shape and size.

public BrushCircle paint(EMImage img){
   Pixel pixel= brushPosition(); 
   this.img=img;
   for (int x=0;x < this.img.overlay.width&&x < shape.width;x++){
       for (int y=0;y < this.img.overlay.width&&y < shape.width;y++){
           if(erase){
               if(shape.get(x,y)!=color(0,0,0,0)){
                   if(this.img.overlay.get(this.img.layer,pixel.x+x,pixel.y+y)==c){
                       this.img.overlay.set(this.img.layer,pixel.x+x,pixel.y+y,color(0,0,0,0));
                        }
                    }
            }else{
                if(shape.get(x,y)!=color(0,0,0,0)){ 
                    this.img.overlay.set(this.img.layer,pixel.x+x,pixel.y+y,shape.get(x,y));
                }
            }
        }    
    }
    return this;
}
                        

Update function

The brush shape is defined in this function, this one uses pythagorus theorem to draw a circle based on the size of the slide bar in the UI panel.

    public BrushCircle update(){
       shape=createImage((int)size,(int)size,ARGB); 
       float ss=size*size/4;
       for(int x=0;x < shape.width;x++){
           for(int y=0;y < shape.height;y++){
               float posX=x-shape.width/2;
               float posY=y-shape.height/2;
               if (posX*posX+posY*posY < ss){
                   shape.set(x,y,c);
                }
            }
        }
        return this;
    }
}