Ch1 Fundamentals Graphics

Download Report

Transcript Ch1 Fundamentals Graphics

Programming
• What is a program?
– A set of instructions
– Understood by a computer
What does a program look like?
• An algorithm is a set of instructions designed to
accomplish a specific goal
• For a temperature f in Fahrenheit
•
•
•
•
Subtract 32 from f.
Divide f by 1.8.
Display the value of f.
[Converts to Celsius]
• Don’t need to understand the goal to follow
instructions
Programming Languages
• A language is a tool with which we tell a
computer an algorithm
• Compilers translate from one computer
language to another
• Each language has own advantages and
disadvantages
The Java Language
• Widely Used
• Can be compiled on many computer systems
• Object Oriented (we’ll learn what this means
later)
Event Driven Programming
• Old programs would start with all input at
once and run until completion
• Event: An action, a mouse click, an item
selected
• Using events made easy in Java
A First Program
import objectdraw.*;
import java.awt.*;
public class TouchyWindow extends WindowController {
public void onMousePress ( Location point ) {
new Text("I’m touched", 40, 50, canvas );
}
public void onMouseRelease( Location point ) {
canvas.clear();
}
}
What do all these words
mean?
• Let’s break the program apart
Some of the words
• Class
– A set, collection, group, or configuration
containing members regarded as having
certain attributes or traits in common.
(American Heritage Dictionary)
– Very similar meaning in Java
Software Libraries
•
Reuse code already written
•
extends
– Builds upon an already written class
•
import
– Allows the use of instructions written
elsewhere
Parts of a Class
• Body
– Enclosed in the curly braces found on the line
“public class…”
• Methods:
public void onMousePress ( Location point ) {
new Text( "I’m touched", 40, 50, canvas );
}
The Life of a Program
• Write the Program
• Translate the program into a language the
computer understands
• Run the Program
Integrated Development
Environments
• Eclipse
– Professional Programming Tool
• TouchyWindow in Eclipse
Integrated Development
Environments
• BlueJ
– Designed to teach Java
• TouchyWindow in BlueJ
Integrated Development
Environments
• Eclipse and BlueJ:
– Available on Windows, MacOS, Unix
– Free and downloadable
More Programming
• Normal vs Graphics Coordinate Systems
Graphical Objects
•
•
•
•
new Text ( "I’m Touched", 40, 50, canvas );
new an instruction that tells we want to
construct a new object
Text the object we want to construct
(…) comma delineated “parameters” that tell
how to construct the object
; semicolons are important too!
– every command semicolon terminated
Graphical Objects
new Text ( "I’m Touched", 40, 50, canvas );
Events
public void onMousePress( Location point )
– Will run the code when mouse button is
pressed
• onMouseRelease:
– Runs code when the buttons released
• Many more mouse event handling
methods
The begin Method
public void begin(){
…
}
• begin method
– Executed (run) exactly once for each program
– Executed early in program’s life
Graphical Objects
new Line( 100, 150, 200, 0 canvas);
Graphical Objects
• New FilledRect( 10, 20, 30, 40, canvas );
– Creates a 30x40 rectangle
– Upper left corner at (10,20)
• new FilledOval( 10, 20, 30, 40, canvas);
– Imagine a 30x40 rectangle at (10,20)
– Draws the largest oval that can fit inside the
imaginary rectangle
Mistakes? Impossible!
• Sadly, computers read what is written, not
the intention.
• Errors will often generate complaints from
the IDE.
• Some complaints are more informative
than others