Processing in Java, using IntelliJ.

Download Report

Transcript Processing in Java, using IntelliJ.

Computer Science I
Sound.
Programming in Java (programming using Processing
IN Java, using IntelliJ IDE)
Classwork/Homework: copy your Processing projects
over into IntelliJ
Good news/Not-so-Good
• Good: Companies and organizations (for example,
group working on Processing) make
improvements, fix bugs, add features.
• Not-so-good: to get the latest thing, we need to
download, install, and, possibly, learn new things.
– Sometimes new features are not something we need
or want.
– Products get bigger and bigger.
In the past, problems re: Sound
• Problem was (is?) version of Processing did not
support Sound Library.
• Sound is a typical candidate for something more
complex to be added only if needed.
• Note: the Sound Library also includes low-level
code (aka native code) for Mac, Windows 32 and
64 bit, Linux 32 and 64.
• So, what is the situation today?
– Go to moodle and download Sound examples
– Note: must do procedure in Processing for CERTAIN
libraries.
Taking snapshot example
• Two Processing features
– Making a sound
– Storing the snapshot in the Sketch
• Be careful to not store too many….
Functions, Methods, Procedures
• Processing terminology is: function. A function is code
that has a name, specification of a return value, and
specification of parameters.
• Java terminology: there are no functions, only
methods, that are associated with classes.
• Some other languages distinguish between procedures:
code that does something and functions: code that
calculates something and returns a value.
• In Processing and Java: code that does not return
something, such as setup, draw, others, uses void to
indicate no return.
Exercise
• Write a function (call it collision)
– with parameters two floating point values
representing a position on the screen
– returning true if the value is "in" a rectangle indicating
by the values rx, ry, rw, rh where
• The rx is the horizontal position of the upper left corner and
ry the vertical position of the upper level corner
• The rw is the width and the rh is the height
• When a function returns a value such as a
Boolean (as opposed to the return value being
void), it can be used in an expression.
Solution
Boolean collision(float x, float y) {
return ((x>rx)&&(x<(rx+rw))
&&(y>ry)&&(y<(ry+rh)));
}
• Note: I could have used an if statement and return true
and return false.
• The call (invocation) of this would be
if (collision(cx,cy)) {
// ball center on rectangle,
// do something
}
Recap
• We have been doing Java, but using the
Processing Development Environment and
accessing libraries developed for Processing
• An integrated development environment provides
a way to
– write (EDIT) a program,
– COMPILE the program (translate into a language
suitable for the computer to run). If there are
syntactic errors, process ends.
– link to other code that the program needs.
– RUN (EXECUTE) the program.
Layers of software (programs)
Your program: your setup, draw, etc. Your
functions, your variables, your classes, etc.
Processing Development Environment: includes
invocation of setup, periodic invocation of draw,
when event happens, invocation of
mousePressed, etc.
Setting of width, mouseX, etc.
Java
Operating System: file system, input/output, etc.
Behind the scenes
• The Processing folks wrote Java code that
Invoked setup
Invoked draw repeatedly according to the
frameRate
Invoked mousePressed, etc. as necessary
• The PDE also has its way of adding image files.
JetBrain IntelliJ IDEA
• Many claim: it is the most Intelligent Java IDE
(Integrated Development Environment)
– Integrates editing, compiling, building (combining parts),
running, debugging
• Includes code completion
• Helps with missing curly brackets
• Can specify styles of coding
– Note: latest Processing does have more debugging, editing
features than previous.
• Free for students and faculty
• Available for OS X, Windows, Linux:
http://jetbrains.com
Preview: Layers of software
(programs)
•
Your program: your setup, draw, etc. Your
functions, your variables, your classes, etc.
Java code will be visible.
IntelliJ: Integrated Development Environment
Processing Libraries included
Java
Operating System: file system, input/output, etc.
Now
• Demonstrate JetBrains IntelliJ
– There are other Java Integrated Development
Environments, e.g., Eclipse
• You use it at least today.
• Note: Math/CS majors will need to do this in CS2,
so it is good to start now.
• Note: for next assignment, Virtual Something,
spend some time on planning independent of
implementation, including choice of
programming language!
IntelliJ IDEA on your computer
• May need to download and install the Java
Development Kit (JDK 8) from
http://www.oracle.com/technetwork/java/javase
/downloads/index.html
• Download and install the Ultimate Edition of the
Jetbrains IntelliJ
– https://www.jetbrains.com/idea/download/
• Apply for a free full license
https://www.jetbrains.com/shop/eform/students
using your Purchase email address.
IntelliJ IDEA Tutorials
• https://www.jetbrains.com/help/idea/2016.2/
discover-intellij-idea.html
• https://www.jetbrains.com/idea/documentati
on/
Configuring IDEA
• Getting comfy in IDEA IDE:
http://yfain.github.io/Java4Kids#_getting_co
mfy_in_idea_ide
• Just specify Project SDK (Standard
Development Kit, normally use 1.8)
File: Other Settings : Default Project Structure
Set options
Note
There are ways to set up
• Keyboard Shortcuts (go to Preferences)
• Specify Code Style (under Editor)
• Quickly rename identifier (e.g. variable name),
called Refactor
• Right click on identifier and go to its
declaration
You can explore this later.
Running Processing Project in IDEA
• Download and unzip the processingIdea.zip file
from Moodle.
• Open IntelliJ Idea, click Open
• Navigate to the processingIdea folder (the results
of unzipping).
• You are ready
– Need to add some standard boilerplace
– Copy over existing sketch or start to write Processing
• Make a small number of modification
– If you forget, IDEA will remind you.
Find folder
Java terminology
• Project
– Package
• Java Classes
– data folder
• Jars
– core.jar (Processing libraries)
– Other libraries
• External libraries
Another way to say it
•
•
•
•
A project is usually a collection of packages
A package is a group of related classes
Packages help to organize your code
Libraries are collections of "helper" code you can use in
your program. They usually have a .jar extension
• the data folder, as in Processing, contains your
resources (images, fonts, etc.)
– Use standard copy and paste to get files into the data
folder
– Notice all your classes will have access to the data folder.
You will not have to repeat Add file… when re-using an
image, font, etc.
Screen shot of my computer
To add a New Package
• Just right-click on the project folder, select
new==> package. The IDEA will create an empty
package folder. You don't have to do this for
your initial work.
To add a New Java Class
This is how you will create a new program.
• Right-click on the Package name, select
newJava class
Note: Right-click is control click
… give class a name
• Convention is to start with capital letter
Now, need to add [some] Java code
• Recall subclasses, your new class, say it is
called MyClass, will extend PApplet. IDEA tells
us that we need to import a library.
Importing Library (-ies)
• You can wait and respond to what IntelliJ says and add
import processing.core.PApplet;
Later, when you try to run, you may get an error and
another suggest. For example, I needed to add
import processing.core.Pimage;
• OR you can add an import statement with a wildcard,
which includes all of the Processing core:
import processing.core.*;
Need some more code:
package CourseWorkCS1;
import processing.core.*;
/**
* Created by jeaninemeyer on 12/21/15.
*/
public class BouncingThings extends PApplet
{
public static void main(String args[])
{
String[] appletArgs = new String[]{"CourseWorkCS1.BouncingThings"};
if (args != null)
{
PApplet.main(concat(appletArgs,args));
} else
{
PApplet.main(appletArgs);
}
}
… This is where to put old Processing code OR write new code
}
Add your Processing sketch
•
Need to make these modifications
– Add public before setup, draw, mousePressed, etc.
– Change any float constants .75 to .75f
Or use double datatype
– If you declared something of datatype color
color cl = color(255,0,0);
change to int cl = color(255,0,0);
– To convert float to integer: Math.round
•
Make sure this is all within the { } for the public class MyClass extends PApplet
So…
• My code defines the setup and draw methods
for BouncingThings, the subclass that extends
PApplet.
Run
To stop
• Click the stop button on the left side of the
console
New example: CardDeckRandomDraw
Planning
• Build the deck by combining the 4 suits with
the 13 named cards.
• Use keyPressed method to randomly pick from
the deck.
package CourseWorkCS1;
import processing.core.PApplet;
//modified by Jeanine on 3/11/2016 from CardDeck
public class CardDeckRandomDraw extends PApplet
{
String[] suit = {"Clubs", "Diamonds", "Hearts",
"Spades"};
String[] rank = {"Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"};
String[] deck = new String[rank.length * suit.length];
//The deck String array will be built
public static void main(String args[])
{
String[] appletArgs = new
String[]{"CourseWorkCS1.CardDeckRandomDraw"};
if (args != null)
{
PApplet.main(concat(appletArgs, args));
} else
{
PApplet.main(appletArgs);
}
}
String msg = " ";
public void setup()
{
size(900,600);
for (int i = 0; i < suit.length; i++)
{
for (int j = 0; j < rank.length; j++)
{
deck[rank.length * i + j] = rank[j] + " of " + suit[i];
}
}
for (int i = 0; i < deck.length; i++)
println(deck[i]);
}
public void draw()
{
background(200);
fill(0);
textSize(25);
text("Press any key for random card",30,30);
text(msg,40,100); //msg is changed in keyPressed
}
public void keyPressed(){
int choice = parseInt(random(52));
msg = " random choice is "+deck[choice];
println(msg);
}
} // closes the class
Note
• A PACKAGE is typically used for related
programs and one program can access the
others.
• This does mean that if there are compilation
errors in one, you can't try to work on another
one, even if for you there is no
interdependency.
• Solution: comment out problematic code.
Classwork/homework
• IntelliJ IDEA is set up on these computers.
• Do download from moodle the processingIdea
file and go through the steps to run an existing
Processing sketch
– Remember to add public to setup, draw, other
built-in Processing methods.
Preview
Two assignments
• Find article on computing in the news. Post a
summary and your comments.
• Next class: will describe and show my Virtual
Dog. Plan (do not start programming) your
virtual something.
– http://faculty.purchase.edu/jeanine.meyer/pet3.h
tml