class Rectangle
{ // do not forget the brackets!
float x;
float y;
float width;
float height;
// the point of the constructor is to initialized the values
Rectangle(float newX, float newY, float newWidth, float newHeight)// here there is no semicolon
{
x= newX;
y= newY;
width=newWidth;
height=newHeight;
// it could be x_ instead of newX,.is just a name
}
void displayRectangle()// I am drawing the class
{
rect (x,y,width,height);// it could e draw with a polygon but of course using the processing function it's easier
}
}
class Circle
{
float x;
float y;
float radio;
Circle (float newX, float newY, float newR)
{
x=newX;
y=newY;
radio=newR;
}
void displayCircle ()
{
ellipse (x-radio,y-radio,radio*2,radio*2);
}
}
class Door
{
Rectangle door;
Rectangle window;
Door (float x,float y, float width, float height)
{
door = new Rectangle (x,y,width,height);//size and location of the door
window = new Rectangle (x + width/4.0,y + height/8.0,width/2.0, height/3.0);
}
void displayDoor ()
{
door.displayRectangle ();
window.displayRectangle ();
}
}
class Wheel
{
Circle tire;
Circle rim;
float rotationAngle;
float rotationSpeed;
Wheel (float x,float y, float radio)
{
tire = new Circle (x,y,radio);
rim = new Circle (x,y, radio/2.0);
rotationAngle = 0;
rotationSpeed = 0;
}
void displayWheel ()
{
tire.displayCircle ();
rim.displayCircle ();
pushMatrix();
translate(rim.x, rim.y);
rotate(rotationAngle);
line(-rim.radio, 0, rim.radio, 0);
line(0, - rim.radio, 0, rim.radio);
popMatrix();
}
}
class Car
{
Rectangle Body;
Door doorLeft;
Door doorRight;
Wheel wheelLeft;
Wheel wheelRight;
float x, y;
Car(float paramX,float paramY,float width,float height)
{
x = paramX;
y = paramY;
Body= new Rectangle (0,height/2,width,height/2);
doorLeft= new Door (width/4.0, height/8,width/4.0,height*7/8);
doorRight= new Door (width/2.0, height/8,width/4.0,height*7/8);
wheelLeft= new Wheel (width*2/8, height,width/10);
wheelRight= new Wheel (width*6/8, height,width/10);
}
void displayCar ()
{
stroke(0, 0, 0);
fill(255, 255, 255);
pushMatrix();
translate(x, y);
Body.displayRectangle();
doorLeft.displayDoor ();
doorRight.displayDoor ();
wheelLeft.displayWheel();
wheelRight.displayWheel();
popMatrix();
}
void setVelocity(float velocity)
{
wheelLeft.rotationSpeed = velocity;
wheelRight.rotationSpeed = velocity;
}
float getVelocity()
{
return wheelLeft.rotationSpeed;
}
float getWidth()
{
return Body.width;
}
void moveCar()
{
x += (wheelLeft.rotationSpeed * wheelLeft.tire.radio);
wheelLeft.rotationAngle += wheelLeft.rotationSpeed;
wheelRight.rotationAngle += wheelRight.rotationSpeed;
}
}
class RGBA
{
int R;
int G;
int B;
int A;
RGBA(int newR, int newG, int newB, int newA)
{
R = newR;
G = newG;
B = newB;
A = newA;
}
RGBA(int newR, int newG, int newB)
{
R = newR;
G = newG;
B = newB;
A = 255;
}
void setFill()
{
fill(R, G, B, A);
}
void setStroke()
{
stroke(R, G, B, A);
}
}
class Button
{
float x;
float y;
float width;
float height;
RGBA fillColor;
RGBA fillColorHover;
RGBA fillColorDown;
RGBA strokeColor;
RGBA strokeColorHover;
RGBA strokeColorDown;
RGBA textColor;
RGBA textColorHover;
RGBA textColorDown;
int strokeSize;
String label;
PFont font;
int fontSize;
Button(String newLabel, float newX, float newY, float newWidth, float newHeight)
{
x = newX;
y = newY;
width = newWidth;
height = newHeight;
fillColor = new RGBA(192, 192, 192);
fillColorHover = new RGBA(32, 32, 128);
fillColorDown = new RGBA(0, 0, 0);
strokeColor = new RGBA(0, 0, 0);
strokeColorHover = new RGBA(255, 255, 255);
strokeColorDown = new RGBA(255, 255, 255);
textColor = new RGBA(0, 0, 0);
textColorHover = new RGBA(255, 255, 255);
textColorDown = new RGBA(255, 255, 255);
strokeSize = 1;
label = newLabel;
font = loadFont("Arial.vlw");
fontSize = 16;
}
void draw()
{
if (isClicked())
{
// down state
fillColorDown.setFill();
strokeColorDown.setStroke();
}
else if (isMouseOver())
{
// hover state
fillColorHover.setFill();
strokeColorHover.setStroke();
}
else
{
// idle state
fillColor.setFill();
strokeColor.setStroke();
}
strokeWeight(strokeSize);
rect(x, y, width, height);
if (isClicked())
{
// down state
textColorDown.setFill();
}
else if (isMouseOver())
{
// hover state
textColorHover.setFill();
}
else
{
// idle state
textColor.setFill();
}
textFont(font, fontSize);
float widthOfLabel = textWidth(label);
text(label, x + ((width - widthOfLabel) / 2), y + ((height - fontSize) / 2) + fontSize);
}
boolean isMouseOver()
{
if (mouseX >= x && mouseX < x + width &&
mouseY >= y && mouseY < y + height)
return true;
else return false;
}
boolean isClicked()
{
if (isMouseOver() && mousePressed && mouseButton == LEFT) return true;
else return false;
}
}
Car c1;
Button startButton;
Button stopButton;
Button reverseButton;
Button fasterButton;
void setup()
{
ellipseMode(CORNER);
size(1000,1000);
c1 = new Car(100, 150, 300, 75);
startButton = new Button("Start!", 50, 50, 200, 50);
stopButton = new Button("Stop!", 275, 50, 200, 50);
reverseButton = new Button("Reverse!", 500, 50, 200, 50);
fasterButton = new Button("Faster!", 725, 50, 200, 50);
}
void draw ()
{
background (255,255,255);
if (startButton.isClicked())
c1.setVelocity(0.1);
if (stopButton.isClicked())
c1.setVelocity(0.0);
if (reverseButton.isClicked())
c1.setVelocity(-0.1);
if (fasterButton.isClicked())
{
if (c1.getVelocity() > 0)
{
c1.setVelocity(c1.getVelocity() + 0.01);
}
else if (c1.getVelocity() < 0)
{
c1.setVelocity(c1.getVelocity() - 0.01);
}
}
if (c1.x > width)
c1.x = 0;
else if (c1.x < -c1.getWidth())
c1.x = width - c1.getWidth();
if (c1.x > width - c1.getWidth())
{
pushMatrix();
translate(-width, 0);
c1.displayCar();
popMatrix();
}
else if (c1.x < 0)
{
pushMatrix();
translate(width, 0);
c1.displayCar();
popMatrix();
}
c1.displayCar();
c1.moveCar();
startButton.draw();
stopButton.draw();
reverseButton.draw();
fasterButton.draw();
}