[Processing × Java] How to use the class

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

table of contents 0. What is a class?

  1. Pre-prepared class
  2. Classes that you define and create

0. What is a class?

0-0. ** What is a class **

A template (type) that contains 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 becomes easier to read. ** ** ② ** Because it can be reused. ** ** (Details on reuse later)

0-2. ** Class type **

There are two types of classes: "** pre-prepared classes " and " self-defined classes **".

The classes prepared in advance are, specifically, String, PImage, PFont, PShape, PVector ... etc.

1. ** Pre-prepared classes **

In Processing, ** many 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

① Make the class concrete ** Create an object ** ② ** Put the contents in the object ** ③ Treat the object as a ** variable **

In the procedure, we will use the class (information in it).

Let's look at a simple example.

string.java


//Declaration of object
String s1;
//Object definition
s1 = "breakfast"
//Use of objects
println(s1.length());
9

This program is a program that acquires and displays the character length. Let's take a closer look at each step.

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

//Declaration of object
String s1;

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

//Object definition
s1 = "breakfast"

** ③ Treat the object as a variable **

//Use of objects
println(s1.length());

Every object has ** fields and methods ** that the class has.

To access the fields and methods of an object, use dots (.). The object itself is treated like a variable.

To use it like a variable ** Definition (declaration-> initial setting)-> use ** It means to use it like this.

2. ** Classes you define and create **

Programming allows you to create your own original classes to simplify your program. Initially, we will simplify the program that displays the following bouncing balls.

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

To simplify the program ① ** Modularize ** ② ** Make it reusable ** I will do that.

① Modularize

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 combining ** into one function **, the inside of the draw () function will be clean and the program will be easy to understand.

② Make it reusable

Reusable means that ** multiple outputs are possible **.

bouncingball_reuse.java


//Declaration of object
Ball b1;
Ball b2;

void setup(){
  size(600,400);
  //Define an 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 a class
class Ball{
  //Variables to use(field)Declare
  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 to display the ball(Method)
  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 to bounce the ball(Method)
  void edges(){
    if(x > width-15 || x < 15){
      xspeed *= -1;
    }
    if(y > height-15 || y < 15){
      yspeed *= -1;
    }
  }
}

Steps to make it reusable

① Create a class framework

class Ball{

}

② Insert a modularized function

(The contents are removed so that the structure is easy to understand.)

class Ball{

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

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


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

//Define a class
class Ball{

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

④ Declare the object.

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

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

class Ball{
}

⑤ Put the contents in the object

Declaration of object
Ball b1;
Ball b2;

void setup(){
  //Assign an instance of the Ball class to object b1.
  //An instance is a concrete example of a class.
  //Object b1 is treated as a variable.
  b1 = new Ball();
  b2 = new Ball();
}
void draw(){
}

class Ball{
}

Point :b1⬅︎new Ball() You are assigning a ** instance ** Ball () of the Ball class to a ** object ** b1.

⑥ Use an object.

Ball b1;
Ball b2;

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

  //Dot(.)Use to access the methods inside the object.
  b1.display();
  b1.move();
  b1.edges();

  //Dot(.)Use to access the methods inside the object.
  b2.display();
  b2.move();
  b2.edges();
}

class Ball{
}

⑦ Create a class constructor

The constructor is what you need to create multiple objects from your class. The object is for accessing the functions in the class.

//Declaration of object
//There is nothing inside yet
Ball b1;
Ball b2;

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

  //Dot(.)Use to access the methods inside the object.
  b1.display();
  b1.move();
  b1.edges();

  //Dot(.)Use to access the methods inside the object.
  b2.display();
  b2.move();
  b2.edges();
}

class Ball{
  //Variables to use(field)Declare
  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 and the field.

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

** Point **: Instance The materialization of the 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. ** You can create multiple instances from the same class. ** **

Finally

Thank you for reading. We appreciate your opinions and suggestions in order to make the article even better.

Recommended Posts

[Processing × Java] How to use the class
[Java] How to use the File class
[Java] How to use the HashMap class
[Processing × Java] How to use the function
[Java] How to use the Calendar class
How to use java class
How to use the wrapper class
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
How to use class methods [Java]
[Processing × Java] How to use arrays
[Java] How to use Math class
[Java] How to use the hasNext function
[Java] How to use the toString () method
Studying how to use the constructor (java)
How to use Java Scanner class (Note)
[Java] How to use Map
[Java] How to use FileReader class and BufferedReader class
[Java] How to use Map
[Java] How to use Thread.sleep to pause the program
How to use java Optional
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use the replace () method (Java Silver)
How to use Java variables
[Java] How to use Optional ①
[Java] How to use Calendar class and Date class
[Java] How to use compareTo method of Date class
How to use the link_to method
How to use Java HttpClient (Get)
How to use the include? method
How to use the form_with method
How to use Java HttpClient (Post)
[Java] How to use join method
How to decompile java class files
[JavaFX] [Java8] How to use GridPane
[Java] How to use List [ArrayList]
How to use classes in Java?
How to use Java lambda expressions
How to use Java enum type
How to get the class name / method name running in Java
[Processing × Java] How to use loop 2 --- Nested structure, coordinate conversion
Multilingual Locale in Java How to use Locale
How to use submit method (Java Silver)
[Rails] How to use the map method
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
How to install the legacy version [Java]
How to get the date in java
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
To use the "java" command line tool ... How to avoid popping up