Classes are one of the most important programming constructs of Java and many other programming languages. They are the abstract model or blueprint for an object. We can also understand classes as extended data types. Unlike the primitive data types we have already looked at, classes define both variables and functions. Programmers call class variables attributes, and class functions as methods, but otherwise they work pretty much the same to as the variables and functions we already know. But in In order to get access to these attributes and methods we need to user use a dot syntax: a , i.e. "." must be placed after the object name, followed by the name of the attribute or the method.
The structure of a class:
class className {
[Attributes - Variables]
[Constructor – Initialising function]
[Methods- Functions]
}
Constructor
The constructor is the initialisation function of a class. This function is called when an new instance (a new object) of a class is created. An object is created with the command 'new' and the subsequent call to the constructor.
...
The structure of the child class:
class className extends basicClass
{
[Attributes - Variables]
[Constructor – Initialising function]
[Methods- Functions]
}
Example
...
BallClass
Note, that this example file uses tabs and multiple files for the first time. These are just to help us organise our code. When we run our program , processing puts these all together p5.js access the classes and treats them as one long documenta part of the main code. As general rule, use a seperate file for each class, using the name of the class as the file name.
Code Block |
---|
BouncingBallvar ball1; BouncingBallvar ball2; PVector p1 = new PVector(); PVector p2 = new PVector(); boolean var p1; var p2; var drag = false; voidfunction setup() { sizecreateCanvas(800,600); p1= createVector(); p2= createVector(); ball1 = new BouncingBall(100,100); ball2 = new BouncingBall(50,50); shapeModeimageMode(CENTER); smooth(); } voidfunction draw() { background(255); fill(255,255,255,60); rect(0,0,width,height); if(drag) { // draw the direction of shoot p1.set(ball1._pos.x,ball1._pos.y,0); line(p1.x,p1.y,p2.x,p2.y); } // draw the balls ball1.draw(); ball2.draw(); } voidfunction mousePressed() { drag = true; p1.set(ball1._pos.x,ball1._pos.y,0); p2.set(mouseX,mouseY,0); } voidfunction mouseDragged() { p2.set(mouseX,mouseY,0); } voidfunction mouseReleased() { drag = false; // shooting direction calculation. PVectorvar dir = PVectorp5.Vector.sub(p2,p1); // shorten the length dir.mult(0.09); //the ball is given a new direction ball1.set(p1,dir,0.993); ball2.set(p1p2,dir,0.95); } |
BouncingBall
Code Block |
---|
class BouncingBall { PVector _pos; PVector _dir; float _dampV; PShape _shape; int _w; int _h; // constructor BouncingBallconstructor(int shapeWidth,int shapeHeight) { this._pos = new PVectorcreateVector(width/2, height/2); this._dir = new PVectorcreateVector(0,0); this._dampV = 1; this._w = shapeWidth; this._h = shapeHeight; this._shape = loadShapeloadImage("ball1ball.svgpng"); } // set the new position + direction + dampening void set(PVector pos,PVector dir,float dampV) { this._pos = pos.get(); this._dir.add(dir); this._dampV = dampV; } // update the current position void calcPos() { // curent position shifted this._pos.add(this._dir); // movement vector modified this._dir.mult(this._dampV); // test horisontal if(this._pos.x + this._w/2 > width) { this._dir.x *= -1; this._pos.x = width - this._w/2; } else if(this._pos.x - this._w/2 < 0) { this._dir.x *= -1; this._pos.x = this._w/2; } // test vertical if(this._pos.y + this._h/2 > height) { this._dir.y *= -1; this._pos.y = height - this._w/2; } else if(this._pos.y - this._h/2 < 0) { this._dir.y *= -1; this._pos.y = this._h/2; } } // draw the ball void draw() { this.calcPos(); shapeimage(this._shape, this._pos.x,this._pos.y, this._w,this._h); } } |
Download Example
Exercise 12 A
- Modify the program, so that several balls can be placed independently of each other
Exercise 12 B
- Extend the class again to make a third kind of ball. Modify its attributes and methods to create new kinds of behavior
Example BallKlasse BallClass Extended
Code Block |
---|
class BallEx extends BouncingBall { BallExconstructor(int size) { super(size,size); } voidfunction draw() { super.draw(); line(0, 0 , this._pos.x,this._pos.y); } } |