Advertisement
philipd83

decorator example

Feb 6th, 2023
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class MainLabel{
  2.     constructor(labelText,x,y){
  3.        
  4.         this.labelText_=labelText || "";
  5.         this.x_=x || 200;
  6.         this.y_=y || 200;
  7.         this.hasBorder_ = false;
  8.         this.hasThickRectBorder_= false;
  9.         this.hasDots_ = false;
  10.         this.width_ = this.labelText_.length * 10;
  11.         this.height_ = 40;
  12.        
  13.     }
  14.     get x(){
  15.         return this.x_;
  16.     }
  17.     get y(){
  18.         return this.y_;
  19.     }
  20.     get width(){
  21.         return this.width_;
  22.     }
  23.     get height(){
  24.         return this.height_;
  25.     }
  26.     get label(){
  27.         return this;
  28.     }
  29.     draw(){
  30.         strokeWeight(1);
  31.         stroke(0,0,0);
  32.         fill(0,0,0);
  33.         textAlign(CENTER,CENTER);
  34.         text(this.labelText_,this.x_,this.y_);
  35.    
  36.     }
  37.  
  38. }
  39. class component extends MainLabel{
  40.     constructor(label){
  41.         super();
  42.         this.label_ = label;
  43.     }
  44.     get label(){
  45.         return this.label_;
  46.     }
  47.  
  48.     get x(){
  49.         return this.label.x;
  50.     }
  51.     get y(){   
  52.         return this.label.y;
  53.     }
  54.     get width(){
  55.         return this.label.width+5;
  56.     }
  57.     get height(){
  58.         return this.label.height+5;
  59.     }
  60.    
  61. }
  62. class ThickBorderLabel extends component{
  63.     constructor(label){
  64.         super(label);
  65.     }
  66.    
  67.     draw(){
  68.        
  69.         rectMode(CENTER);
  70.         strokeWeight(3);
  71.         stroke(0,0,0);
  72.         noFill();
  73.         rect(this.x,this.y,this.width,this.height);
  74.         this.label.draw();
  75.  
  76.     }
  77.    
  78. }
  79. class ThinBorderLabel extends component{
  80.     constructor(label){
  81.         super(label);
  82.     }
  83.    
  84.     draw(){
  85.        
  86.         rectMode(CENTER);
  87.         strokeWeight(1);
  88.         stroke(0,0,0);
  89.         noFill();
  90.         rect(this.x,this.y,this.width,this.height)
  91.         this.label.draw();
  92.  
  93.     }
  94.    
  95. }
  96. class DotsBorderLabel extends component{
  97.     constructor(label){
  98.         super(label);
  99.     }
  100.    
  101.     draw(){
  102.         var x_ = this.x;
  103.         var y_ = this.y;
  104.         var width_ = this.width;
  105.         var height_ = this.height;
  106.         ellipseMode(CENTER);
  107.         strokeWeight(1);
  108.         fill(255,0,0);
  109.         stroke(255,0,0);
  110.         //above label
  111.         for(var i=0;i<width_/10 + 1;i++){
  112.             ellipse((x_-width_/2+i*10),(y_-height_/2),5,5);
  113.         }
  114.         //below label
  115.         for(var i=0;i<width_/10 +1;i++){
  116.             ellipse((x_-width_/2+i*10),(y_+height_/2),5,5);
  117.         }
  118.         //left of label
  119.         for(var i=0;i<height_/10-1;i++){
  120.             ellipse((x_-width_/2),(y_-height_/2+((i+1)*10)),5,5);
  121.         }
  122.         //right of label
  123.         for(var i=0;i<height_/10-1;i++){
  124.             ellipse((x_+(width_+6)/2),(y_-height_/2+((i+1)*10)),5,5);
  125.         }
  126.         this.label.draw();
  127.        
  128.  
  129.     }
  130.    
  131. }
  132.  
  133. var Label = function(labelText,x,y){
  134.     return new MainLabel(labelText,x,y);
  135. };
  136.  
  137. var labels=[];
  138. var thickBorderButton;
  139. var borderButton;
  140. var dotButton;
  141. var removeButton;
  142. var selectLabelRadio;
  143. var selected;
  144. var numLabels;
  145.  
  146. /*
  147.   these four functions are called when button is pressed
  148.   modify the "add" functions to apply decorator to the
  149.   selected label.  
  150. */
  151. function addThinBorder(){
  152.     labels[selected] = new ThinBorderLabel(labels[selected]);
  153. }
  154. function addThick(){
  155.     labels[selected] = new ThickBorderLabel(labels[selected]);
  156. }
  157. function addDots(){
  158.     labels[selected]= new DotsBorderLabel(labels[selected]);
  159. }
  160.  
  161.  
  162. /*
  163.   Challenge: removeLastBorder function.  If you add this feature
  164.   use this function to remove the most recent decorator
  165.   from the selected label
  166.  */
  167. function removeLastBorder(){
  168.     labels[selected] = labels[selected].label;
  169. }
  170.  
  171.  
  172. /*********************************************************/
  173. /* NOTHING BELOW THIS COMMENT IS TO BE MODIFIED          */
  174. /*********************************************************/
  175.  
  176. function setup(){
  177.     createCanvas(600,500); 
  178.  
  179.     thickBorderButton=createButton("Add Thick Border");
  180.     thickBorderButton.mousePressed(addThick);
  181.     thickBorderButton.position(10,550);
  182.     borderButton=createButton("Add Thin Border");
  183.     borderButton.mousePressed(addThinBorder);
  184.     borderButton.position(160,550);
  185.     dotButton=createButton("Add Dots Border");
  186.     dotButton.mousePressed(addDots);
  187.     dotButton.position(310,550);
  188.  
  189.     removeButton=createButton("Remove Last");
  190.     removeButton.mousePressed(removeLastBorder);
  191.     removeButton.position(460,550);
  192.  
  193.     selectLabelRadio=createRadio();
  194.     numLabels=5;
  195.     for(var i=0;i<numLabels;i++){
  196.         selectLabelRadio.option(""+i,"Label "+(i+1));
  197.         labels[i] = Label("Label "+(i+1), random(50,550), random(20,480));
  198.     }
  199.     selectLabelRadio.selected(0);
  200.  
  201. }
  202.  
  203.  
  204. /* This function is not to be modified*/
  205. function draw(){
  206.     selected=selectLabelRadio.value();
  207.     background(255,255,255);
  208.     for(var i=0;i<numLabels;i++){
  209.         labels[i].draw();
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement