Don't like ads? PRO users don't see any ads ;-)
Guest

Car-Button Example

By: a guest on May 9th, 2012  |  syntax: Java  |  size: 7.15 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class Rectangle
  2. { // do not forget the brackets!
  3.         float x;
  4.         float y;
  5.         float width;
  6.         float height;
  7.        
  8.        
  9.         // the point of the constructor is to initialized the values
  10.         Rectangle(float newX, float newY, float newWidth, float newHeight)// here there is no semicolon
  11.         {
  12.                 x= newX;
  13.                 y= newY;
  14.                 width=newWidth;
  15.                 height=newHeight;
  16.                 // it could be x_ instead of newX,.is just a name
  17.         }
  18.  
  19.         void displayRectangle()// I am drawing the class
  20.         {
  21.                 rect (x,y,width,height);// it could e draw with a polygon but of course using the processing function it's easier
  22.         }
  23. }
  24.  
  25. class Circle
  26. {
  27.         float x;
  28.         float y;
  29.         float radio;
  30.        
  31.         Circle (float newX, float newY, float newR)
  32.                 {
  33.                 x=newX;
  34.                 y=newY;
  35.                 radio=newR;
  36.                 }
  37.        
  38.         void displayCircle ()
  39.         {
  40.                 ellipse (x-radio,y-radio,radio*2,radio*2);
  41.         }
  42. }
  43.  
  44. class Door
  45. {  
  46.         Rectangle door;
  47.         Rectangle window;
  48.        
  49.         Door (float x,float y, float width, float height)
  50.         {
  51.               door = new Rectangle (x,y,width,height);//size and location of the door
  52.               window = new Rectangle (x + width/4.0,y + height/8.0,width/2.0, height/3.0);
  53.         }
  54.        
  55.         void displayDoor ()
  56.         {
  57.           door.displayRectangle ();
  58.           window.displayRectangle ();
  59.         }
  60. }
  61.  
  62. class Wheel
  63. {
  64.         Circle tire;
  65.         Circle rim;
  66.         float rotationAngle;
  67.         float rotationSpeed;
  68.        
  69.         Wheel (float x,float y, float radio)
  70.         {
  71.             tire = new Circle (x,y,radio);
  72.             rim = new Circle (x,y, radio/2.0);
  73.             rotationAngle = 0;
  74.             rotationSpeed = 0;
  75.         }
  76.        
  77.         void displayWheel ()
  78.         {
  79.           tire.displayCircle ();
  80.           rim.displayCircle ();
  81.          
  82.           pushMatrix();
  83.             translate(rim.x, rim.y);
  84.             rotate(rotationAngle);
  85.             line(-rim.radio, 0, rim.radio, 0);
  86.             line(0, - rim.radio, 0, rim.radio);
  87.           popMatrix();
  88.         }
  89. }
  90.  
  91. class Car
  92. {
  93.         Rectangle Body;
  94.         Door doorLeft;
  95.         Door doorRight;
  96.         Wheel wheelLeft;
  97.         Wheel wheelRight;
  98.                 float x, y;
  99.        
  100.         Car(float paramX,float paramY,float width,float height)
  101.         {
  102.                         x = paramX;
  103.                         y = paramY;
  104.                
  105.              Body= new Rectangle (0,height/2,width,height/2);  
  106.             doorLeft= new Door (width/4.0, height/8,width/4.0,height*7/8);
  107.             doorRight= new Door (width/2.0, height/8,width/4.0,height*7/8);
  108.             wheelLeft= new Wheel (width*2/8, height,width/10);
  109.             wheelRight= new Wheel (width*6/8, height,width/10);
  110.            
  111.         }
  112.        
  113.         void displayCar ()
  114.         {
  115.                         stroke(0, 0, 0);
  116.                         fill(255, 255, 255);
  117.                
  118.                         pushMatrix();
  119.                                 translate(x, y);
  120.                                 Body.displayRectangle();
  121.                                 doorLeft.displayDoor ();
  122.                                 doorRight.displayDoor ();
  123.                                 wheelLeft.displayWheel();
  124.                                 wheelRight.displayWheel();
  125.                         popMatrix();
  126.         }
  127.        
  128.         void setVelocity(float velocity)
  129.         {
  130.                         wheelLeft.rotationSpeed = velocity;
  131.                         wheelRight.rotationSpeed = velocity;
  132.         }
  133.                
  134.                 float getVelocity()
  135.                 {
  136.                         return wheelLeft.rotationSpeed;
  137.                 }
  138.                
  139.                 float getWidth()
  140.                 {
  141.                         return Body.width;
  142.                 }
  143.                                                
  144.                 void moveCar()
  145.                 {
  146.                         x += (wheelLeft.rotationSpeed * wheelLeft.tire.radio);
  147.                        
  148.                         wheelLeft.rotationAngle += wheelLeft.rotationSpeed;
  149.                         wheelRight.rotationAngle += wheelRight.rotationSpeed;
  150.                 }
  151. }
  152.  
  153. class RGBA
  154. {
  155.         int R;
  156.         int G;
  157.         int B;
  158.         int A;
  159.        
  160.         RGBA(int newR, int newG, int newB, int newA)
  161.         {
  162.                 R = newR;
  163.                 G = newG;
  164.                 B = newB;
  165.                 A = newA;
  166.         }
  167.        
  168.         RGBA(int newR, int newG, int newB)
  169.         {
  170.                 R = newR;
  171.                 G = newG;
  172.                 B = newB;
  173.                 A = 255;
  174.         }
  175.        
  176.         void setFill()
  177.         {
  178.                 fill(R, G, B, A);
  179.         }
  180.        
  181.         void setStroke()
  182.         {
  183.                 stroke(R, G, B, A);
  184.         }
  185. }
  186.  
  187. class Button
  188. {
  189.         float x;
  190.         float y;
  191.         float width;
  192.         float height;
  193.        
  194.         RGBA fillColor;
  195.         RGBA fillColorHover;
  196.         RGBA fillColorDown;
  197.        
  198.         RGBA strokeColor;
  199.         RGBA strokeColorHover;
  200.         RGBA strokeColorDown;
  201.        
  202.         RGBA textColor;
  203.         RGBA textColorHover;
  204.         RGBA textColorDown;
  205.        
  206.         int strokeSize;
  207.        
  208.         String label;
  209.         PFont font;
  210.         int fontSize;
  211.        
  212.         Button(String newLabel, float newX, float newY, float newWidth, float newHeight)
  213.         {
  214.                 x = newX;
  215.                 y = newY;
  216.                 width = newWidth;
  217.                 height = newHeight;
  218.                
  219.                 fillColor = new RGBA(192, 192, 192);
  220.                 fillColorHover = new RGBA(32, 32, 128);
  221.                 fillColorDown = new RGBA(0, 0, 0);
  222.                
  223.                 strokeColor = new RGBA(0, 0, 0);
  224.                 strokeColorHover = new RGBA(255, 255, 255);
  225.                 strokeColorDown = new RGBA(255, 255, 255);
  226.                
  227.                 textColor = new RGBA(0, 0, 0);
  228.                 textColorHover = new RGBA(255, 255, 255);
  229.                 textColorDown = new RGBA(255, 255, 255);
  230.                
  231.                 strokeSize = 1;
  232.                
  233.                 label = newLabel;
  234.                
  235.                 font = loadFont("Arial.vlw");
  236.                 fontSize = 16;
  237.         }
  238.        
  239.         void draw()
  240.         {
  241.                 if (isClicked())
  242.                 {
  243.                         // down state
  244.                         fillColorDown.setFill();
  245.                         strokeColorDown.setStroke();
  246.                 }
  247.                 else if (isMouseOver())
  248.                 {
  249.                         // hover state
  250.                         fillColorHover.setFill();
  251.                         strokeColorHover.setStroke();
  252.                 }
  253.                 else
  254.                 {
  255.                         // idle state
  256.                         fillColor.setFill();
  257.                         strokeColor.setStroke();
  258.                 }
  259.                
  260.                 strokeWeight(strokeSize);              
  261.                 rect(x, y, width, height);
  262.                
  263.                 if (isClicked())
  264.                 {
  265.                         // down state
  266.                         textColorDown.setFill();
  267.                 }
  268.                 else if (isMouseOver())
  269.                 {
  270.                         // hover state
  271.                         textColorHover.setFill();
  272.                 }
  273.                 else
  274.                 {
  275.                         // idle state
  276.                         textColor.setFill();
  277.                 }
  278.                
  279.                 textFont(font, fontSize);
  280.                
  281.                 float widthOfLabel = textWidth(label);
  282.                
  283.                 text(label, x + ((width - widthOfLabel) / 2), y + ((height - fontSize) / 2) + fontSize);
  284.         }
  285.        
  286.         boolean isMouseOver()
  287.         {
  288.                 if (mouseX >= x && mouseX < x + width &&
  289.                         mouseY >= y && mouseY < y + height)
  290.                         return true;
  291.                 else return false;
  292.         }
  293.        
  294.         boolean isClicked()
  295.         {
  296.                 if (isMouseOver() && mousePressed && mouseButton == LEFT) return true;
  297.                 else return false;
  298.         }
  299. }
  300.            
  301. Car c1;
  302. Button startButton;
  303. Button stopButton;
  304. Button reverseButton;
  305. Button fasterButton;
  306.  
  307. void setup()
  308. {
  309.         ellipseMode(CORNER);
  310.         size(1000,1000);
  311.                
  312.         c1 = new Car(100, 150, 300, 75);
  313.         startButton = new Button("Start!", 50, 50, 200, 50);
  314.         stopButton = new Button("Stop!", 275, 50, 200, 50);
  315.         reverseButton = new Button("Reverse!", 500, 50, 200, 50);
  316.         fasterButton = new Button("Faster!", 725, 50, 200, 50);
  317. }
  318.  
  319. void draw ()
  320. {
  321.         background (255,255,255);
  322.                
  323.         if (startButton.isClicked())
  324.                 c1.setVelocity(0.1);
  325.                
  326.         if (stopButton.isClicked())
  327.                 c1.setVelocity(0.0);
  328.                
  329.         if (reverseButton.isClicked())
  330.                 c1.setVelocity(-0.1);
  331.                
  332.         if (fasterButton.isClicked())
  333.         {
  334.                 if (c1.getVelocity() > 0)
  335.                 {
  336.                         c1.setVelocity(c1.getVelocity() + 0.01);
  337.                 }
  338.                 else if (c1.getVelocity() < 0)
  339.                 {
  340.                         c1.setVelocity(c1.getVelocity() - 0.01);
  341.                 }
  342.         }
  343.                
  344.         if (c1.x > width)
  345.                 c1.x = 0;
  346.         else if (c1.x < -c1.getWidth())
  347.                 c1.x = width - c1.getWidth();
  348.                
  349.         if (c1.x > width - c1.getWidth())
  350.         {
  351.                 pushMatrix();
  352.                         translate(-width, 0);
  353.                         c1.displayCar();
  354.                 popMatrix();
  355.         }
  356.         else if (c1.x < 0)
  357.         {
  358.                 pushMatrix();
  359.                         translate(width, 0);
  360.                         c1.displayCar();
  361.                 popMatrix();
  362.         }
  363.        
  364.         c1.displayCar();
  365.         c1.moveCar();
  366.        
  367.         startButton.draw();
  368.         stopButton.draw();
  369.         reverseButton.draw();
  370.         fasterButton.draw();
  371. }