Advertisement
JackWitherell

cubemethod

Apr 27th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. PGraphics cube;
  2. int scale=120;
  3.  
  4. void setup() {
  5.   size(150, 200);
  6.   noSmooth();
  7. }
  8.  
  9. PGraphics drawCube(int size, color rgb){
  10.   PGraphics temp=createGraphics(scale,scale+scale/3);
  11.   temp.beginDraw();
  12.   temp.stroke(0,0,0,0);
  13.   temp.fill(rgb);
  14.   temp.quad((size/2),0,                  //draw top of cube (top)(right)(bottom)(left)
  15.             size,(size/3),
  16.             (size/2),((size/3)*2),
  17.             0,(size/3));
  18.   temp.fill(color(red(rgb)-20,            //dim fill by 20
  19.                   green(rgb)-20,
  20.                   blue(rgb)-20),
  21.                   alpha(rgb));
  22.   temp.quad(0,(size/3),                  //draw left of cube (top left)(top right)(bottom right)(bottom left)
  23.             (size/2),((size/3)*2),
  24.             (size/2),(size/3)*4,
  25.             0,size);
  26.   temp.fill(color(red(rgb)-40,            //dim fill by 20
  27.                   green(rgb)-40,
  28.                   blue(rgb)-40),
  29.                   alpha(rgb));
  30.   temp.quad(size,(size/3),              //draw right of cube (top right)(bottom right)(bottom left)(top left)
  31.             size,size,
  32.             (size/2),(size/3)*4,
  33.             (size/2),((size/3)*2));
  34.   temp.fill(rgb);                         //restore fill for storage of rgb in the PGraphics object
  35.   temp.endDraw();
  36.   return temp;
  37. }
  38. boolean keyp=false;
  39. void keyPressed(){
  40.   if(key=='a'){
  41.     keyp=true;
  42.   }
  43. }
  44. void keyReleased(){
  45.   if(key=='a'){
  46.     keyp=false;
  47.   }
  48. }
  49.  
  50. void printCube(int x, int y){                
  51.   color temp=g.fillColor;             //store current fill and stroke temporarily
  52.   color stemp=g.strokeColor;
  53.   stroke(0,0,0,0);                    //stroke transparent
  54.   quad(x+(scale/2),y,                 //draw top of cube (top)(right)(bottom)(left)
  55.        x+scale,y+(scale/3),
  56.        x+(scale/2),y+((scale/3)*2),
  57.        x,y+(scale/3));
  58.   fill(color(red(g.fillColor)-20,     //dim fill by 20
  59.              green(g.fillColor)-20,
  60.              blue(g.fillColor)-20),
  61.              alpha(g.fillColor));
  62.   quad(x,y+(scale/3),                 //draw left of cube (top left)(top right)(bottom right)(bottom left)
  63.        x+(scale/2),y+((scale/3)*2),
  64.        x+(scale/2),y+(scale/3)*4,
  65.        x,y+scale);
  66.   fill(color(red(g.fillColor)-20,     //dim fill by 20
  67.              green(g.fillColor)-20,
  68.              blue(g.fillColor)-20),
  69.              alpha(g.fillColor));
  70.   quad(x+scale,y+(scale/3),           //draw right of cube (top right)(bottom right)(bottom left)(top left)
  71.        x+scale,y+scale,
  72.        x+(scale/2),y+(scale/3)*4,
  73.        x+(scale/2),y+((scale/3)*2));
  74.   fill(temp);                         //restore fill
  75.   stroke(stemp);                      //restore stroke
  76. }
  77.  
  78. void draw(){
  79.   background(200);
  80.   fill(30,200,200);
  81.   if(cube==null){cube=drawCube(scale,color(200,200,30));} //If cube doesn't exist, make it
  82.   if(!keyp){                  //If A button isn't pressed
  83.     image(cube,9,30);                 //Print Cube using New Method
  84.   }
  85.   else{
  86.     printCube(20,30);                 //Print cube using old method
  87.   }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement