Drawing pretty stuff
Visualizing data
Plotting mathematical functions
Simulating Newton's gravitation laws
Simulating Artificial Intelligence ( Little robots following a path while avoiding each other )
It facilitates building graphical user interfaces ( GUI ), by abstracting some of the lower level concepts from Java
point(x,y)
draws a point centered at pixel coordinates (x,y)
strokeWeight(pixels)
lets you set the size of the point
line(x1,y1,x2,y2)
draws a line segment from (x1,y1) to (x2,y2)
rect(x,y,w,h)
draws a rectangle whose top left corner is at (x,y) and its size is w by h pixels
ellipse(x,y,w,h)
draws an ellipse centered at (x,y) with a size of w by h pixels
stroke(val)
sets the color of lines to val
in grayscale
fill(val)
sets the color inside a shape to val
in grayscale
background(val)
sets the color of the background to val
in grayscale
stroke(r,g,b)
sets the color of lines to r,g,b
in the red-green-blue scale
fill(r,g,b)
sets the color inside a shape to val
in the red-green-blue scale
background(r,g,b)
sets the color of the background to val
in red-green-blue scale
In Processing this is managed by two methods
The setup
method specifies what will happen at the beginning of the program, and will be executed only once
The draw
method specifies what will happen continuously, during your program's execution. This is known as the drawing loop.
In Processing this is managed by two methods
The setup
method specifies what will happen at the beginning of the program, and will be executed only once
The draw
method specifies what will happen continuously, during your program's execution. This is known as the drawing loop.
The speed of the drawing loop can be set with the frameRate(fps)
, where fps
is the desired number of frames per second
The simplest kind of interaction that we get from Processing is keyboard and mouse input
The mousePressed
global variable is a boolean that is set to true
when the user clicks a mouse button. Before every call to the draw
method it is set to false
by default.
The global variables mouseX
and mouseY
give you the pixel coordintates of the mouse pointer in the canvas. These are of type int
.
The keyPressed
method gets executed whenever the user presses a key
You can get the ASCII value of the key that was pressed using the global variable key
, of type char
A Robot
should be drawn as an ellipse. It has a position on the screen in pixel coordinates (x
,y
), and a diameter
attribute. It will also have a speed
attribute (in pixels per second).
Every time the user clicks on the window, a new Robot
will be drawn at the location of the click.
Every time the user presses x, a Robot
will be selected at random, and killed.
Robot
instances are scared of the last rule, so they should be shaking around their current location
Some additional rules
All the Robot
instances are attracted
towards the location of the mouse pointer.
Robot
instances hate each other: add a repelling
force between Robot
instances that keeps them apart.
/