[Java] [Processing x Java] How to use the class

6 minute read

This article is for understanding the structure of the program through Processing. This time I will write about classes.

table of contents

  1. What is a class?
  2. Classes prepared in advance
  3. Class that you define yourself

0. What is a class?

0-0. What is a class

A template (type) containing related fields (variables) and methods (functions). It is often compared to recipes and blueprints.

0-1. Why Use Classes

The main reason is ① **Because the program is easier to read. ** ② **Because it can be reused. ** (Details on reuse will be given later)

0-2. Class types

There are two types of classes: “classes that are prepared in advance” and “classes that you create by yourself”.

The prepared classes are, specifically, String,PImage,PFont,PShape,PVector…

1.Prepared class

In Processing, a lot of classes are prepared in advance**. String,PImage,PFont,PShape,PVector… etc.

1-0. How to use the prepared classes

Since the class is just a template (type), it cannot be used as it is. So

① Materialize the class and create objects ② Put the contents in the object ** ③ Treat objects as variables

In that procedure, we will use the class (information inside).

Let’s take a look at a simple example.

string.java


// object declaration
String s1;
// object definition
s1 = "breakfast"
// use the object
println(s1.length());
9

This program is a program that acquires and displays the length of characters. Let’s take a closer look at each step.

** ① Create an object by embodying a class **

.java


// object declaration
String s1;

** ② Put the contents in the object. **

.java


// object definition
s1 = "breakfast"

** ③ Treat objects as variables **

.java


// use the object
println(s1.length());

All objects have class fields and methods.

Use the dot (.) to access the fields and methods of the object. The object itself is treated like a variable.

To use like a variable **Definition (Declaration → Initial Setting) → Use ** Is to be used like.

2.** Class that you define and create yourself **

In programming, you can create your own original class to simplify the program. First, let’s simplify the following bouncing ball display program.

bouncingball.java


float x = 30;
float y = 30;
float xspeed = 5;
float yspeed = 3;

void setup(){
  size(398,198);
}

void draw(){
  background(255);

  noStroke();
  fill(234,159,217);
  ellipse(x,y,50,50);
  
  x += xspeed;
  y += yspeed;
  
  if(x> width-20 || x <20){
    xspeed *= -1;
  }
  
  if(y> height-20 || y <20){
    yspeed *= -1;
  }
}

![ezgif.com-gif-maker (5).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/654004/c499ecb4-c455-fff3-b310-(9c5d7187ef45.gif)

To simplify the program, ① ModularizationReusable I will do that.

① Modularization

bouncingball_module.java



float x = 30;
float y = 30;
float xspeed = 5;
float yspeed = 3;

void setup(){
  size(398,198);
}

void draw(){
  background(255);
  
  display();
  move();
  edges();
}


// Function to display the ball
void display(){
  fill(234,159,217);
  noStroke();
  ellipse(x,y,30,30);
}

// Function to move the ball
void move(){
  x += xspeed;
  y += yspeed;
}

// Function to bounce the ball
void edges(){
  if(x> width-15 || x <15){
    xspeed *= -1;
  }
  if(y> height-15 || y <15){
    yspeed *= -1;
  }
}

Point: By making a single function, the inside of the draw() function will be cleaner and the program will be easier to understand.

② Make it reusable

Reusable means multiple outputs are possible.

bouncingball_reuse.java


// object declaration
Ball b1;
Ball b2;

void setup(){
  size(600,400);
  // define the object
  b1 = new Ball(50,50,7,5);
  b2 = new Ball(400,50,-7,-5);
}

void draw(){
  background(255);
  // use object b1
  b1.display();
  b1.move();
  b1.edges();
  
  // use object b2
  b2.display();
  b2.move();
  b2.edges();
}

// define the class
class Ball{
  // Declare the variable (field) to use
  int x;
  int y;
  float xspeed;
  float yspeed;
  
  //Ball class constructor
  Ball(int xpos,int ypos,float xvelocity,float yvelocity){
    x = xpos;
    y = ypos;
    xspeed = xvelocity;
    yspeed = yvelocity;
  }
  
  // Function (method) to display the ball
  void display(){
    fill(234,159,217);
    noStroke();
    ellipse(x,y,30,30);
  }
  
  // Function to move the ball (method)
  void move(){
    x += xspeed;
    y += yspeed;
  }
  
  // Function (method) to bounce the ball
  void edges(){
    if(x> width-15 || x <15){
      xspeed *= -1;
    }
    if(y> height-15 || y <15){
      yspeed *= -1;
    }
  }
}

Reusable procedure

① Create a class framework

.java


class Ball{

}

② Insert a modularized function

(The contents are removed to make the structure easier to understand.)

.java


class Ball{

  // Function (method) to display the ball
  void display(){
  }
  // Function to move the ball (method)
  void move(){
  }
  // Function (method) to bounce the ball
  void edges(){
  }
}

③ Prepare variables (fields) used for functions (methods).

.java



// Declare the variable (field) to use
int x;
int y;
float xspeed;
float yspeed;

// define the class
class Ball{

  // Function (method) to display the ball
  void display(){
  }
  // Function to move the ball (method)
  void move(){
  }
  // Function (method) to bounce the ball
  void edges(){
  }
}

④ Declare the object.

.java


// Declare the object b1 to use.
Ball b1;
// Declare the object b2 to use.
Ball b2;

void setup(){
}
void draw(){
}

class Ball{
}

⑤ Put contents in the object

.java


Object declaration
Ball b1;
Ball b2;

void setup(){
  //Substitute an instance of Ball class in object b1.
  // Instance is a concrete form of class.
  // Object b1 is treated as a variable.
  b1 = new Ball();
  b2 = new Ball();
}
void draw(){
}

class Ball{
}

Point :b1⬅︎ new Ball() The ** instance Ball() of the Ball class is assigned to the **objectb1.

⑥ Use objects.

  • All objects have fields (variables) and methods (functions) that the class has. Use a dot (.) to access your own fields and methods.

.java


Ball b1;
Ball b2;

void setup(){
  b1 = new Ball();
  b2 = new Ball();
}
void draw(){

  // Use dot (.) to access the method in the object.
  b1.display();
  b1.move();
  b1.edges();

  // Use dot (.) to access the method in the object.
  b2.display();
  b2.move();
  b2.edges();
}

class Ball{
}

⑦ Create a class constructor

Constructors are what you need to create multiple objects from a class.The object is for accessing the functions in the class.

.java


// object declaration
// There is nothing inside yet
Ball b1;
Ball b2;

void setup(){
  // object creation (definition)
  //Substitute an instance that embodies the class into the created object
  b1 = new Ball(50,50,7,5);
  b2 = new Ball(400,50,-7,-5);
}
void draw(){

  // Use dot (.) to access the method in the object.
  b1.display();
  b1.move();
  b1.edges();

  // Use dot (.) to access the method in the object.
  b2.display();
  b2.move();
  b2.edges();
}

class Ball{
  // Declare the variable (field) to use
  int x;
  int y;
  float xspeed;
  float yspeed;

  //Ball class constructor
  //Data type variable
  Ball(int xpos,int ypos,float xvelocity,float yvelocity){
    x = xpos;
    y = ypos;
    xspeed = xvelocity;
    yspeed = yvelocity;
  }

  void display(){}
  void move(){}
  void edges(){}
}

Point: Constructor The constructor connects the instance with the field.

(In this case) 7 = float xvelocity = float xspeed When the program is executed, xspeed is set to 7.

Point: Instance A reification of an object. In this case, Ball(50,50,7,5) is one of the class instances. Ball(400,50,-7,-5) is also an instance of the class. ** Multiple instances can be created from the same class. **

Finally

Thank you for reading. We would appreciate your comments and suggestions for improving the article.

*This article is based on Daniel Shefman’s youtube “Coding Train”.