[Processing × Java] How to use variables

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

スクリーンショット 2020-06-30 15.13.15.png

table of contents 0. What are variables

  1. Variable type
  2. Example of a program using variables

0. What are variables

Variables are like ** boxes for data **.

variable00.java


//Substitute 5 for the variable a.
int a = 5;
//Substitute 10 for the variable b.
int b = 10;

//a +b Display the result.
println(a + b);
15

Caution: When you use a variable in Java, you need to specify ** what kind of variable it is **. NG : a = 5 OK : int a = 5

Types include int for integers, float for floating point numbers, String for strings, and color for colors. Reference article: Java reference understood in the figure

Point: In the program, the equal sign (=) indicates ** substitution **. It's easy to understand if you think of it as a ** arrow pointing to the left **.

int a ⬅︎ 5

1. Variable type

Variables include ** Built-in variables ** and ** User-defined variables **.

variable01.java


size(600,400);
rect(0,0,width-100,height-100);

ex01.png

Point : Built-in variables Variables prepared in advance. width (= screen width), height (= screen height), mouseX (= mouse X coordinate), mouseY (= mouse Y coordinate) ... etc.

in this case, width = 600 height = 400 Can be used without having to define it yourself. lucky!

Point : User-defined variables Variables that you define and use. How to use: ① ** Declaration ** → ② ** Initial setting ** → ③ ** Use **

How to use (example) ① ** Declaration ** (I'm thinking of using such a variable!) int a; ② ** Initial setting ** (This variable should be set to this value!) a = 10; ③ ** Use ** (Then I will use it ...!) println(a);

2. Example of a program using variables

◯ Programs using mouseX and mouseY

variable02.java


//It is executed only once when the program starts.
void setup(){
//Determine the size of the screen
  size(600,400);
}

//infinite loop
void draw(){
//The area of the rectangle changes depending on the location of the mouse.
  rect(0,0,mouseX,mouseY);
}

スクリーンショット 2020-06-30 15.16.08.png

Point :  setup () repeats the process in {} only once. (Something like initial settings) draw () repeats the process in {} infinitely. (infinite loop)

rect (X coordinate at the upper left corner of the rectangle, Y coordinate at the upper left corner of the rectangle, horizontal side length, vertical side length);

◯ Program using mouseX and mouseY②

variable03.java


//Repeat only once at the beginning
void setup(){
//Determine the size of the screen
  size(510,510);
}
//infinite loop
void draw(){
  //Do not draw the edge
  noStroke();
  //Decide how many colors to paint in the figure →(R,G,B)
  fill(mouseX/2,mouseY/2,255);
  //Determine the coordinates, width, and height of the center of the circle.
  ellipse(mouseX,mouseY,30,30);
}

スクリーンショット 2020-06-30 15.13.15.png

◯ A program that defines variables by yourself and moves the circle horizontally

variable04.java


//Define a float type variable xpos.
//A variable that represents the position of x.
float xpos = 0;

void setup(){
  size(510,200);
  background(0);
}

void draw(){
  //Do not fill the inside of the circle.
  noFill();
  stroke(100,230,random(180,255));
  //The x coordinate of the circle is determined by xpos.
  //The size of the circle should be random.
  ellipse(xpos,height/2,random(100),random(100));
  //Increase xpos by 10.
  xpos += 10;

}

スクリーンショット 2020-07-02 9.20.22.png

◯ A program that defines variables by yourself and moves the circle horizontally ②

variable04.java


//Define a float type variable xpos.
//A variable that represents the position of x.
float xpos = 0;

void setup(){
  size(510,200);
}

void draw(){
  //Try repainting the background each time you loop.
  background(0);
  //Do not fill the inside of the circle.
  noFill();
  stroke(100,230,random(180,255));
  //The x coordinate of the circle is determined by xpos.
  //The size of the circle should be random.
  ellipse(xpos,height/2,random(100),random(100));
  //Increase xpos by 10.
  xpos += 10;

  //When the circle pops out of the screen, make it come out from the left again.
  if(xpos > width){
    xpos = 0;
  }
}

ezgif.com-gif-maker (1).gif

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 variables
How to use Java variables
[Processing × Java] How to use arrays
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Processing × Java] How to use the function
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
[Java] How to use Optional ①
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use join method
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
How to name variables in Java
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
[Processing × Java] How to use loop 2 --- Nested structure, coordinate conversion
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
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 use Java Scanner class (Note)
How to use environment variables in RubyOnRails
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] How to use the Calendar class
[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
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5