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 r,g,b
in the red-green-blue scale
background(r,g,b)
sets the color of the background to r,g,b
in red-green-blue scale
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
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
.
Keyboard input ( an interactive to what the Scanner class does ):
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
Pausing the program execution:
The delay
method takes a double
respresenting a time in seconds to pause the program
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.
Vector2D
classx
and y
componentsadd
method. angle
and magnitude
of a vector.multiply
a vector by a number to change it's magnitude.Let's modify the Robot class by adding the following attributes:
location
vector will represent the Robot's positionvelocity
vector will control the direction in which the robot will move in the next frame. acceleration
vector will control how the velocity changes, depending on external forces. And the following methods:
update
method will change the Robot's location, using its current velocity.applyForce
method will accumulate external forces that we apply to each robot. This will change the Robot's acceleration. update
method will change the Robot's location, using its current velocity. It will also update it's current velocity using using its current accelerationupdate
method will change the Robot's location, using its current velocity. It will also update it's current velocity using using its current accelerationapplyForce
method will accumulate external forces that we apply to each robot. This will change the Robot's acceleration. update
method will change the Robot's location, using its current velocity. It will also update it's current velocity using using its current accelerationapplyForce
method will accumulate external forces that we apply to each robot. This will change the Robot's acceleration. update
method will change the Robot's location, using its current velocity. It will also update it's current velocity using using its current accelerationapplyForce
method will accumulate external forces that we apply to each robot. This will change the Robot's acceleration. location
and velocity
of every robot, so that we can reload it when we start the program: We need to implement the toString
method of the Vector2D
and Robot
classes
We need to implement a loadFromString
method for the Robot
class
We will implement a method loadState
that will try opening a file to load the state of the robot world
We will implement a method saveState
that will open a file with the robot world state, and recreate it.
We will use the keyPressed
method to save the world state, when the user presses the 'S' key.
/